温馨提示×

Debian Jenkins如何配置代理

小樊
42
2026-09-17 05:29:37
栏目: 智能运维

Debian 上运行的 Jenkins 配置代理,通常分为 3 种常见场景。下面按使用频率从高到低说明。


一、Jenkins 通过 HTTP/HTTPS 代理访问外网(最常见)

适用于:

  • Jenkins 本身在防火墙/内网中
  • 需要访问 Maven 仓库、Docker Hub、GitHub 等

1️⃣ 通过 Jenkins Web 界面配置(推荐)

  1. 登录 Jenkins
  2. 进入
    Manage Jenkins → System
  3. 找到 HTTP Proxy Configuration
  4. 填写:
    • Proxy Host:proxy.example.com
    • Port:8080
    • 如有账号:
      • User name
      • Password
    • No Proxy Host:
      localhost,127.0.0.1,.example.com
      
  5. 保存

✅ 立即生效


2️⃣ 通过系统环境变量配置(全局)

编辑:

sudo nano /etc/default/jenkins

添加:

export http_proxy=http://proxy.example.com:8080
export https_proxy=http://proxy.example.com:8080
export no_proxy="localhost,127.0.0.1"

重启:

sudo systemctl restart jenkins

二、Jenkins Agent(节点)通过代理连接 Controller

适用于:

  • Agent 在隔离网络中
  • Controller 在公网或不同网段

方式 1:Web Socket(推荐,最简单)

Controller 端

  • Manage Jenkins → Security
  • 启用 Enable agents to connect via WebSocket

Agent 端

  • 使用 jenkins-agent.jar
  • 启动参数中加入代理:
java \
  -Dhttp.proxyHost=proxy.example.com \
  -Dhttp.proxyPort=8080 \
  -Dhttps.proxyHost=proxy.example.com \
  -Dhttps.proxyPort=8080 \
  -jar agent.jar \
  -url http://jenkins.example.com \
  -secret xxx \
  -name agent1

方式 2:SSH / JNLP + 代理

如果是 JNLP:

java -Dhttps.proxyHost=proxy.example.com -Dhttps.proxyPort=8080 -jar agent.jar ...

三、Jenkins 构建任务中使用代理(Pipeline)

1️⃣ 在 Pipeline 中设置环境变量

pipeline {
    agent any
    environment {
        http_proxy = 'http://proxy.example.com:8080'
        https_proxy = 'http://proxy.example.com:8080'
        no_proxy = 'localhost,127.0.0.1'
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
}

2️⃣ Docker 构建使用代理

docker.build("myimage", "--build-arg http_proxy=http://proxy:8080")

四、常见问题排查

❌ Jenkins 无法联网

curl -I https://repo.maven.apache.org

❌ Plugin 无法安装

  • 检查 Update Site 是否可达
  • 检查代理是否拦截 HTTPS

❌ Agent 连不上

  • 确认 WebSocket 已开启
  • 确认代理允许长连接

五、总结

场景 配置位置
Jenkins 访问外网 Web UI / 环境变量
Agent 连接 Jenkins JVM 参数 / WebSocket
构建中使用代理 Pipeline / Docker

如果你能说明:

  • Jenkins 版本
  • 是 Controller 还是 Agent
  • 用的是 Docker / systemd / Kubernetes

我可以给你更精确的配置示例

0