docker部署jenkins和使用教程
安装 Jenkins
使用 docker 安装
- 拉取最新版镜像
docker pull jenkins/jenkins:lts
- 创建 Jenkins 目录
# 创建 jenkins 目录
mkdir -p /opt/docker/jenkins
# 进入jenkins 目录
cd /opt/docker/jenkins
- 安装启动 jenkins 容器
docker run -d \
--name jenkins \
--restart=always \
--privileged=true \
-p 8080:8080 \
-v /opt/docker/jenkins:/var/jenkins_home \
-v /opt/apache-maven-3.8.6:/usr/local/maven \
-v /opt/jdk8/jdk1.8.0_333:/usr/local/java/jdk1.8.0_333 \
-v /opt/gradle-6.3:/usr/local/gradle-6.3 \
-v /etc/localtime:/etc/localtime \
-e TZ=Asia/Shanghai \
-e JENKINS_OPTS="--prefix=/jenkins" \
jenkins/jenkins:lts
- --name:容器名称
- --privileged=true:使container内的root拥有真正的root权限
- -p 8080:8080:宿主机端口映射容器端口
- -e TZ=Asia/Shanghai 指定时区
- -e JENKINS_OPTS="--prefix=/jenkins" 指定jenkins访问的前缀,方便ng反向代理
- -v /docker/jenkins:/var/jenkins_home:挂载 jenkins_home 目录到宿主机
- -v /opt/apache-maven-3.6.3:/usr/local/maven:挂载 maven 安装目录
- -v /opt/jdk-1.8/jdk1.8.0_191:/usr/local/java/jdk1.8.0_291:挂载 jdk 安装目录
- -v /opt/gradle-6.3:/usr/local/gradle-6.3:挂载 gradle 安装目录
apache-maven下载地址:https://maven.apache.org/download.cgi
gradle-6.3:https://services.gradle.org/distributions/
配置maven变量和gradle
/etc/profile
在最下方配置
#maven环境变量配置
export MAVEN_HOME=/opt/apache-maven-3.8.6
export PATH=$PATH:$MAVEN_HOME/bin
#gradle环境变量配置
export GEADLE_HOME=/opt/gradle-6.3
export PATH=$PATH:$GEADLE_HOME/bin
别忘了source /etc/profile 让profile生效
maven验证:mvn -v
gradle验证:gradle -v
- 查看 jenkins 启动情况
docker ps -a | grep jenkins
- jenkins 目录授权
# 用户组改变
chown -R 1000:1000 /opt/docker/jenkins
- 启动 jenkins 容器
docker start jenkins
配置 Jenkins
基础配置
1. 浏览器访问 jenkins
https://jenkins.ysrmt.cn/
2. 输入管理员密码,密码保存路径:/var/jenkins_home/secrets/initialAdminPassword
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
3. 安装插件
步骤:选择插件来安装 -> 取消所有勾选的插件 -> 安装
说明:等后面更换插件源(国内镜像),再手动安装需要的插件
4. 创建管理员账号
账号/密码:javadev/123456
5. 进入首页
6. 更换插件源
- 镜像站点
tencent https://mirrors.cloud.tencent.com/jenkins/plugins/
huawei https://mirrors.huaweicloud.com/jenkins/plugins/
ustc https://mirrors.ustc.edu.cn/jenkins/plugins/
清华大学镜像:https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins
Manage Jenkins -> Manage Plugins -> Advanced -> Updatge Site
华为镜像是最快的
安装所需插件
- Gradle
- Git
- docker-build-step
- Publish Over SSH
- Role-based Authorization Strategy
- maven integration
- Locale 中文汉化语言包插件Localization: Chinese (Simplified)
全局工具配置
Manage Jenkins -> Global Tool Configuration
JDK 配置
- Name:jdk 版本
- JAVA_HOME:jenkins 容器内的 JAVA_HOME 路径 /usr/local/java/jdk1.8.0_333
Gradle 配置
- name:gradle 版本
- GRADLE_HOME:jenkins 容器内的 GRADLE_HOME 路径
Maven 配置
- Name:maven 版本
- MAVEN_HOME:jenkins 容器内的 MAVEN_HOME路径 /usr/local/maven
系统配置
docker 构建配置
先安装 docker-build-step 插件
Manage Jenkins -> Configure System -> Docker Builder
开启 Docker 远程访问
查看默认配置参数并筛选关键信息“ExecStart”
cat /usr/lib/systemd/system/docker.service | grep ExecStart
修改配置文件:
vi /usr/lib/systemd/system/docker.service
添加远程访问参数:
-H tcp://0.0.0.0:2375
最终效果:
修改后,重载配置文件:
systemctl daemon-reload && systemctl restart docker
jenkins 中 docker 构建配置
SSH 服务器配置
实现要在jenkins上实现要安装 Publish over SSH 插件
Manage Jenkins -> Configure System -> Publish over SSH -> SSH Servers
服务器上创建 Remote Directory
mkdir -p /home/mscm
jenkins 配置 SSH Servers
- Name:SSH Server 名称(自定义)
- Hostname:服务器主机名(域名/ip)
- Username:登录账号
- Remote Directory:docker 挂载的远程文件夹 ,这个文件夹也要事先在服务器上创建成功
- Passphrase / Password:登录密码
测试下
凭证配置
Manage Jenkins -> Manage Credentials -> Stores scoped to Jenkins -> global -> Add Credentials
GItlab 凭证配置
配置 gitlab 连接凭证
- Username:gitlab登录账号
- Password:账号密码
- Description:gitlab凭据
凭证使用:
构建任务时,Source Code Management -> Repository URL(填写 gitlab项目 URL) -> Credentials
docker 凭证配置
宿主机登录 docker
docker login --username=名家课堂 registry-vpc.cn-hangzhou.aliyuncs.com
登录成功:
配置 docker 连接凭证
- Username:docker 登录账号
- Password:账号密码
- Description:docker登录账号
凭证使用:
构建任务时,Build -> Execute Docker command -> Registry credentials
用户权限管理插件(可选)
启用 Role-Based Strategy
Manage Jenkins -> Configure Global Security -> Role-Based Strategy
配置角色
Manage Jenkins -> Configure Global Security -> Manage and Assign Roles
添加账号
Manage Jenkins -> Configure Global Security -> Manage Users
构建任务
New Item -> 输入任务名称(项目名称) -> 选择 Freestyle project
General
Source Code Management
pom.xml在检出的目录的相对位置
Build - Add build step -Invoke Gradle script
Build - Add build step -Invoke Gradle script
Build - Add build step -Invoke Gradle script
Post-build Actions - Add Server
Exec command 示例
echo "=======配置信息======================"
image_name=xxxxxx.cn-hangzhou.aliyuncs.com/xxxxx/test-applet-web
container_name=test-applet-web
image_tag=1.0.$BUILD_NUMBER
port=10086
user_dir=/home/mscm
mkdir -p $user_dir/$container_name
echo "=======停止容器,并删除================="
docker stop $container_name
docker rm $container_name -f
echo "=======启动容器======================"
docker run -it --name $container_name \
-p $port:$port \
--privileged=true \
-m 1024m \
--restart=always \
-v $user_dir/$container_name/output:/usr/local/app/output \
-v $user_dir/$container_name/dump:/usr/local/app/dump \
-d $image_name:$image_tag
echo "=======启动成功========================"
echo "=======查看镜像======================"
docker images |awk '/test-applet-web/{print}'
echo "=======删除多余的镜像,保存1个最近的镜像====="
docker images |awk '/test-applet-web/{print $3}' | sed -n '2,$p' |xargs -i docker rmi {}
nginx配置反向代理
server {
listen 80;
server_name www.xxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#配置中心
location /jenkins/ {
proxy_pass http://10.100.232.171:8081/jenkins/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
文章转载自公众号:IT学习道场