一、下载
查看服务器JDK版本跟Jenkins版本,Jenkins
官网下载
https://mirrors.jenkins.io/war-stable/
清华源选择稳定版下载
https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/
https://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/2.346.1/
Jenkins官网
https://mirrors.jenkins-ci.org/
如果版本选择的太新,安装报错(Java1.8.0_381+Apache Maven 3.8.7 +Jenkins 2.346.1)
https://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/2.346.1/
wget https://get.jenkins.io/war-stable/2.346.1/jenkins.war --no-check-certificate
使用docker安装启动
docker启动jenkins
docker run -d -p 8000:8080 -p 50000:50000 --name jenkins --privileged=true -v /app/jenkins:/var/jenkins_home jenkins/jenkins:2.346.3
docker启动jenkins并使用宿主机docker,时间使用上海时间
docker run -d -u root -p 8001:8080 -p 50000:50000 --restart=always --name jenkins -v /var/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/bin/docker jenkins/jenkins:2.346.3
二、启动
nohup java -server -Xms2048m -Xmx2048m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -jar jenkins.war --httpPort=10240 &
指定端口 后台启动 丢弃所有输出
nohup java -server -Xms2048m -Xmx2048m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -jar jenkins.war --httpPort=8000 &
jenkins默认工作目录
/root/.jenkins
查看初始密码
cat /root/.jenkins/secrets/initialAdminPassword
三、Jenkins设置
1.插件配置
Jenkins默认安装插件已经有了Git
推荐插件
Locale
Git Parameter
Publish Over SSH
系统中配置Publish Over SSH,配置服务器密码,测试是否能连接
重启
http://xxxxx:8000/restart
从已经安装好的jenkins插件压缩或者去插件官网下载,拷贝到新的容器中,解压
2.Jenkins时区
四、Jenkins容器集成harbor
3.1确认harbor的内网ip
10.10.10.1 harbor.test.com
3.2宿主机配置访问harbor执行
//将harbor服务端的主机域名进行映射
[root@localhost ~]# vim /etc/hosts
10.10.10.1 harbor.test.com //添加harbor服务端的IP地址与主机名
//测试是否能与harbor端ping通
[root@localhost ~]# ping -c 2 harbor.test.com
PING harbor_node01.example.com (192.168.92.130) 56(84) bytes of data.
64 bytes from harbor_node01.example.com (192.168.92.130): icmp_seq=1 ttl=64 time=0.322 ms
64 bytes from harbor_node01.example.com (192.168.92.130): icmp_seq=2 ttl=64 time=0.696 ms
--- harbor.test.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1028ms
rtt min/avg/max/mdev = 0.322/0.509/0.696/0.187 ms
//使用insecure-registries参数添加http支持
[root@localhost ~]# vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
"insecure-registries":["harbor.test.com"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
//登录到harbor
[root@localhost ~]# docker login harbor.test.com
Username: admin //输入harbor的用户名
Password: //输入harbor的密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
3.3将host配置到jenkins容器中
echo "10.10.10.1 harbor.test.com" >> /etc/hosts
测试是否登录
docker login harbor.test.com
五、Jenkins构建模版
后端Jenkins构建项目模版
echo '当前 WORKSPACE 工作目录'
pwd
#进入对应的工程附录
cd $WORKSPACE
#设置 java编译环境
export MVN_HOME=/var/jenkins_home/maven
export PATH=$MVN_HOME/bin:$PATH
echo 'mvn 打包'
mvn -v
mvn clean install -Dmaven.test.skip=true -Pdev
echo '构建镜像'
#docker-compose 构建镜像
#docker build
#如果没有输入过密码则使用docker login -u admin -p Harbor123 test.harbor.com
echo '登录harbor'
docker login test.harbor.com
echo "镜像打Tag"
docker tag 镜像名称(最好使用项目名称) test.harbor.com/harbor中仓库名/项目名称:latest
docker push test.harbor.com/harbor中仓库名/项目名称:latest
echo "镜像上传完成"
前端Jenkins构建项目模版
#导入node12版本
export NODE_HOME=/var/jenkins_home/node12
export PATH=$NODE_HOME/bin:$PATH
#查看当前项目node版本
npm -v
node -v
#cd到前端目录下
cd ruoyi-ui
#下载依赖
npm install --registry=https://registry.npmmirror.com
#打包
npm run build:stage
publish over ssh指定服务器停止删除旧的容器,构建新的镜像,并启动
cd /app/ruoyi
rm -rf ruoyi-admin.jar Dockerfile
cp /var/jenkins_home/workspace/Gitee-ruoyi-vue/ruoyi-admin/target/ruoyi-admin.jar /app/ruoyi/ruoyi-admin.jar
cp /var/jenkins_home/workspace/Gitee-ruoyi-vue/Dockerfile /app/ruoyi/Dockerfile
docker stop app
docker rm app
cp ruoyi-admin.jar app.jar
docker build -f /app/ruoyi/Dockerfile -t app:v1 .
docker run -d --name app -p 8080:8080 app:v1
ruoyi-Vue前后端分离,Jenkins示例
echo '后端构建'
#设置 java编译环境
export MVN_HOME=/var/jenkins_home/maven
export PATH=$MVN_HOME/bin:$PATH
mvn -v
mvn clean package
echo '前端构建'
export NODE_HOME=/var/jenkins_home/node12
export PATH=$NODE_HOME/bin:$PATH
cd ruoyi-ui
npm install --registry=https://registry.npmmirror.com
npm run build:stage
publish over ssh
六、Jenkins部署项目集成pnpm
echo "export PATH=$PATH:/var/jenkins_home/node-v16.14.0-linux-x64" >> /etc/profile
source /etc/profile
ln -sf /var/jenkins_home/node-v16.14.0-linux-x64/bin/npm /usr/local/bin/
ln -sf /var/jenkins_home/node-v16.14.0-linux-x64/bin/npm /usr/bin/
ln -sf /var/jenkins_home/node-v16.14.0-linux-x64/bin/node /usr/local/bin/
ln -sf /var/jenkins_home/node-v16.14.0-linux-x64/bin/pnpm /usr/bin/
七、Nginx反向代理Jenkins
--env JENKINS_OPTS="--prefix=/jenkins"
重新创建容器
docker run -d -u root -p 8001:8080 -p 50000:50000 --restart=always --name jenkins -v /var/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/bin/docker --env JENKINS_OPTS="--prefix=/jenkins" jenkins/jenkins:2.346.3
一旦加了参数,则访问路由必须包含配置的prefix:jenkins
常见报错
1.缺少依赖
根据提示安装对应的依赖
yum -y install libgcc_s.so.1