本文共 4482 字,大约阅读时间需要 14 分钟。
Jenkins 是一款流行的持续集成(CI)工具,广泛应用于软件开发的自动化构建和测试过程中。本文将详细介绍 Jenkins 的安装与配置方法,涵盖 Linux 环境下的安装、Docker 容器部署以及插件下载加速等内容。
在 Linux 环境下安装 Jenkins 需要完成以下几个步骤:
Jenkins 运行时需要安装 Git 和 JDK 8。可以通过以下命令安装:
sudo yum install git java-1.8.0-openjdk
安装 Jenkins 有两种方式:安装指定版本或安装最新版本。
下载并安装指定版本的 Jenkins RPM:
wget https://pkg.jenkins.io/redhat-stable/jenkins-2.222.4-1.1.noarch.rpmsudo rpm -ivh jenkins-2.222.4-1.1.noarch.rpm
添加 Jenkins YUM 仓库并安装最新版本:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.reposudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.keysudo yum install jenkins -y
启动 Jenkins 服务并设置开机启动:
sudo systemctl start jenkinssudo systemctl enable jenkinssudo systemctl daemon-reload
Jenkins 默认运行在 8080 端口,需要允许该端口通过防火墙:
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanentsudo firewall-cmd --zone=public --add-service=http --permanentsudo firewall-cmd --reload
使用 Docker 容器部署 Jenkins 服务有以下两种方式:直接使用 docker run 命令,或通过 docker-compose 命令。
运行 Jenkins 容器并挂载必要的数据卷:
# 拉取指定版本 Jenkins 容器docker pull jenkins/jenkins:2.222.4-lts-centos7# 创建容器网络(可选)docker network create jenkins_network# 运行 Jenkins 容器docker run --name jenkins-in-docker \ --network jenkins_network \ --publish 8080:8080 --publish 50000:50000 \ --volume jenkins_home:/var/jenkins_home \ --volume /usr/bin/docker:/usr/bin/docker \ --volume /var/run/docker.sock:/var/run/docker.sock \ jenkins/jenkins:2.222.4-lts-centos7
docker 组。docker.sock 和 docker 客户端需挂载到宿主机。通过 docker-compose 运行 Jenkins 服务,推荐使用镜像 jenkins/jenkins:2.222.4-lts-centos7。
创建 infrastructure-docker-compose.yml:
version: '3.7'services: jenkins-in-docker: image: jenkins/jenkins:2.222.4-lts-centos7 container_name: jenkins-in-docker privileged: true restart: unless-stopped ports: - 8080:8080 - 50000:50000 environment: JAVA_OPTS: "-Xmx2048m -Xms1024m -Djava.security.egd=file:/dev/./urandom -Duser.timezone=Asia/Shanghai -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8" volumes: - /usr/bin/docker:/usr/bin/docker - /var/run/docker.sock:/var/run/docker.sock - ./docker_data/jenkins/2.222.4/jenkins_home:/var/jenkins_home - ./docker_data/jenkins/2.222.4/jenkins_home_bak:/var/jenkins_home_bak networks: - infrastructure_network deploy: resources: limits: memory: 4096M cpus: '2'networks: infrastructure_network: external: true
运行 docker-compose 脚本:
# 创建启动脚本 startup-infrastructure-docker-compose.sh#!/usr/bin/env bashdocker_network=infrastructure_networkdocker_compose_file=infrastructure-docker-compose.ymlecho "The compose file as below:"docker-compose -f ${docker_compose_file} confignw=$(docker network ls | grep ${docker_network} | awk '{print $1}')if [[ ${nw} ]]; then docker-compose -f ${docker_compose_file} --compatibility up --remove-orphans -delse docker network create ${docker_network} && docker-compose -f ${docker_compose_file} --compatibility up --remove-orphans -dfi 运行脚本:
chmod +x startup-infrastructure-docker-compose.sh./startup-infrastructure-docker-compose.sh
Jenkins 插件默认从国外镜像源下载,速度较慢。以下是优化方法:
编辑 /usr/share/jenkins/ref/plugins/updates/default.json,将 mirrors.jenkins-ci.org 替换为国内镜像源:
{ "mirrors": { "jenkins-ci.org": "https://mirrors.tuna.tsinghua.edu.cn/jenkins", "www.google.com": "https://mirrors.tuna.tsinghua.edu.cn/google" }} 安装 Nginx 并配置反向代理:
sudo yum install nginxsudo systemctl start nginxsudo systemctl enable nginx# 添加 hosts 记录(需权限)sudo nano /etc/hosts127.0.0.1 updates.jenkins-ci.org
修改 Nginx 配置文件:
sudo nano /etc/nginx/sites-available/default
添加以下内容:
location /download/plugins/.* { proxy_next_upstream http 502 504 error timeout invalid_header; proxy_set_header Host mirrors.tuna.tsinghua.edu.cn; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; rewrite /download/plugins(.*) /jenkins/plugins/$1 break; proxy_pass https://mirrors.tuna.tsinghua.edu.cn;} 在 Jenkins 中设置国内镜像站点:
通过以上方法,可以快速搭建一个高效的 Jenkins CI 环境。对于 Docker 容器中的 Jenkins,确保容器内 docker 组设置正确,以便正常执行 Docker 命令。如有问题,可参考 Jenkins 中文社区的相关解答。
转载地址:http://cpwaz.baihongyu.com/