实验架构:
192.168.0.96 gitlab
192.168.0.97 jenkins、tomcat
192.168.0.98 harbor、docker集群、git、jdk、maven
1、先把单个jenkins做成可以部署到docker集群中。参照:https://www.cnblogs.com/effortsing/p/10468840.html
2、192.168.0.98作为jenkins-slave节点进行试验,注意:192.168.0.98不需要安装jenkins,只通过master节点上添加node就可以搞定
2.1、 安装smart jenkins 插件,如图所示:
3、jenkins-slave节点上安装git、jdk、maven。
git安装:用yum安装git后就不用管了
jdk安装:安装包要解压到/usr/local下
maven安装:安装包要解压到/usr/local下
4、配置jenkins-slave节点上的git拉取代码:添加ssh-key公钥到gitlab
参照:https://www.cnblogs.com/effortsing/p/10012158.html
5、192.168.0.97(jenkins主机)配置免密登录192.168.0.98主机
6、jenkins-slave主机配置免密登录自己,因为pipline脚本里面需要把容器部署到本机上
7、在jenkins主机上添加一个凭证(登录192.168.0.98的用户名密码:root/123)
8、在jenkins主机上找到系统管理---节点管理--新建新节点--名称(192.168.0.98);远程工作目录:/usr/local/;host:192.168.0.98,如下图:
9、给slave节点添加工具位置(maven、jdk),如下图:
10、slave正常状态如下图所示:
11、pipline脚本如下:
node ("192.168.0.98") {
stage('git checkout'){
echo 'git clone'
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c32a3a24-2c9d-4c8d-b456-9d6d0badf3a2', url: 'http://192.168.0.96:8081/zihao/test.git']]])
}
stage('maven build'){
echo 'maven build'
sh '''
export JAVA_HOME=/usr/local/jdk1.8.0_171/
/usr/local/apache-maven-3.5.4/bin/mvn clean package -Dmaven.test.skip=true
'''
}
stage('docker build and push images'){
echo 'docker build and push images'
sh '''
REPOSITORY=192.168.0.98:5000/library/solo/solo:${Tag}
cat >Dockerfile<<EOF
FROM 192.168.0.98:5000/library/tomcat-85:latest
RUN rm -rf /usr/local/tomcat/webapps/ROOT/
COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["./bin/catalina.sh", "run"]
EOF
docker build -t $REPOSITORY .
docker login -u admin -p Harbor12345 192.168.0.98:5000
docker push $REPOSITORY
'''
}
stage('deploy'){
echo 'deploy'
sh '''
REPOSITORY=192.168.0.98:5000/library/solo/solo:${Tag}
cat >deploy.sh<<EOF
#!/bin/bash
ssh root@192.168.0.98 'docker container run -d --name blog-solo-2 -v /opt:/opt -p 89:8080 $REPOSITORY'
EOF
chmod +x deploy.sh
sh deploy.sh
'''
}
}
12、开始构建如下图所示:
13、构建成功入下图:
参照文档:http://www.cnblogs.com/zndxall/p/8297356.html