温馨提示×

jenkins在centos如何代理

小樊
41
2026-10-15 02:10:52
栏目: 智能运维

在 CentOS 上让 Jenkins 通过代理(Proxy)访问外网(比如下载插件、更新等),通常有两种场景:


一、Jenkins 本身走 HTTP/HTTPS 代理(最常用)

适用于:

  • 公司内网
  • Jenkins 需要访问互联网但只能通过代理

方法 1:通过 Jenkins Web 界面配置(推荐)

  1. 登录 Jenkins
  2. 进入:
    Manage Jenkins → Plugins → Advanced
    
    或老版本:
    Manage Jenkins → Configure System
    
  3. 找到 HTTP Proxy Configuration
  4. 填写:
    • Proxy Host
    • Proxy Port
    • 如有账号:
      • Proxy Username
      • Proxy Password
  5. 点击 Save

✅ 适用于插件下载、更新中心


方法 2:修改 Jenkins 启动参数(全局代理)

编辑 Jenkins 启动文件(CentOS 7):

vi /etc/sysconfig/jenkins

添加 JVM 参数:

JENKINS_JAVA_OPTIONS="-Dhttp.proxyHost=proxy.example.com \
-Dhttp.proxyPort=8080 \
-Dhttps.proxyHost=proxy.example.com \
-Dhttps.proxyPort=8080"

如果是 CentOS 8 / systemd:

systemctl edit jenkins

添加:

[Service]
Environment="JAVA_OPTS=-Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.example.com -Dhttps.proxyPort=8080"

重启 Jenkins:

systemctl restart jenkins

二、Jenkins Agent / Shell 命令走代理

如果你在 构建任务 中执行命令(如 curl、git、docker)需要代理:

方式 1:在 Jenkins Job 中设置环境变量

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

方式 2:系统级代理(CentOS)

vi /etc/profile.d/proxy.sh

内容:

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

执行:

source /etc/profile.d/proxy.sh

三、Git 使用代理(常见坑)

git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy http://proxy.example.com:8080

四、如果 Jenkins 做反向代理(Nginx)

如果你是 用 Nginx 代理 Jenkins(不是 Jenkins 走代理):

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

总结

需求 方法
Jenkins 下载插件走代理 Web 配置 / JVM 参数
Shell 命令走代理 环境变量
Git 走代理 git config
外部访问 Jenkins Nginx 反向代理

如果你能告诉我:

  • Jenkins 版本
  • CentOS 版本
  • 是 Jenkins 走代理 还是 代理 Jenkins

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

0 踩