Jenkins+Docker+Git+Harbor流水线打包
环境:
CentOS Linux release 7.6.1810 (Core)
192.168.247.214 Jenkins+docker
192.168.247.215 git+harbor
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Step1: 安装jdk环境(214)
下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html
tar -xf jdk-8u171-linux-x64.tar.gz
mv jdk1.8.0_171/ /usr/local/java
cat >>/etc/profile < JAVA_HOME=/usr/local/java
> PATH=$PATH:$JAVA_HOME/bin
> EOF
source /etc/profile
[root@localhost ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
Step2:安装Jenkins
tomcat下载地址:https://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gz
Jenkins下载地址:http://mirrors.jenkins.io/war-stable/latest/jenkins.war
wget https://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gz
tar -xf apache-tomcat-8.5.38.tar.gz
mv apache-tomcat-8.5.38 /usr/local/tomcat_jenkins
wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
mv jenkins.war /usr/local/tomcat_jenkins/webapps/
bash /usr/local/tomcat_jenkins/bin/startup.sh
访问地址:http://192.168.247.214:8080/jenkins/
Step3:安装docker(2台都装)
官方文档地址:https://docs.docker.com/install/linux/docker-ce/centos/
安装国内加速器: curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
$ yum install docker-ce -y
$ sudo systemctl start docker && systemctl enable docker
Step4:安装docker-compose
下载地址:https://github.com/docker/compose/releases
curl -L https://github.com/docker/compose/releases/download/1.24.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
Step5:安装Harbor
Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源Docker Distribution。作为一个企业级私有Registry服务器,Harbor提供了更好的性能和安全。提升用户使用Registry构建和运行环境传输镜像的效率。Harbor支持安装在多个Registry节点的镜像资源复制,镜像全部保存在私有Registry中,确保数据和知识产权在公司内部网络中管控。另外,Harbor也提供了高级的安全特性,诸如用户管理,访问控制和活动审计等。
下载Harbor地址:https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.4.tgz
tar -xf harbor-offline-installer-v1.7.4.tgz
cd harbor
#vim harbor.cfg
hostname = 192.168.247.215
./prepare
./install.sh
Harbor仓库的stop与start:
进入Harbor目录执行如下命令即可:
docker-compose stop/start
访问地址:http://192.168.247.215
默认用户密码是:admin/Harbor12345
Step6:配置jenkins
1、创建pipeline项目
2、参数化构建
3、编写pipeline脚本
模板:
node (){ //指定Slave标签
//拉取代码
stage('Git Checkout') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:mtr-sscp/sscp-antipass.git']]])
}
//代码编译
stage('Maven Build') {
sh '''
export JAVA_HOME=/usr/local/java
/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true
'''
}
stage('Build and Push Image') {
sh '''
TIME=$(date +%Y%m%d%H%M)
echo ${TIME} >TIME.txt
REPOSITORY=172.31.182.143/dev/sscp-antipass:${Tag}
cd target
cat >> Dockerfile < 。
# 该命令将复制指定的 到容器中的 。 其中 可以是Dockerfile所在目录的一个相对路径;也可以是一个 URL;还可以是一个 tar 文件(自动解压为目录)。
ADD sscp-antipass.jar /app.jar
# 从命令本身看,是为了执行一个touch命令,前面的add命令把jar复制过去,后面touch命令的作用是修改这个文件的访问时间和修改时间为当前时间。
RUN bash -c 'touch /app.jar'
# container启动时执行的命令,但是一个Dockerfile中只能有一条ENTRYPOINT命令,如果多条,则只执行最后一条
ENTRYPOINT ["java","-jar","/app.jar"]
EOF
docker build -t $REPOSITORY .
docker login -u admin -p Harbor%12345 172.31.182.143
docker push $REPOSITORY
'''
}
stage('Deploy to Docker') {
sh '''
REPOSITORY=172.31.182.143/dev/sscp-antipass:${Tag}
docker rm -f antipass |true
docker image rm $REPOSITORY |true
docker login -u admin -p Harbor%12345 172.31.182.143
docker run -p 8100:8100 -v /home/sscp/sscp-antipass/dockerDeploy/logs:/logs -d --name antipass $REPOSITORY
'''
}
}
Git Checkout在Pipeline Syntax自动生成。
6、完成构建
发布到K8s pipeline模板:
node (){ //指定Slave标签
//拉取代码
stage('Git Checkout') {
checkout([$class: 'GitSCM', branches: [[name: '*/k8s']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:mtr-sscp/sscp-transaction.git']]])
}
//代码编译
stage('Maven Build') {
sh '''
export JAVA_HOME=/usr/local/java
/usr/local/maven/bin/mvn clean package -Pk8s -Dmaven.test.skip=true
'''
}
stage('Build and Push Image') {
sh '''
TIME=latest
echo ${TIME} >TIME.txt
REPOSITORY=172.31.182.143/k8s/sscp-transaction:${TIME}
cp Dockerfile target/
cd target
docker build -t $REPOSITORY .
docker login -u admin -p Harbor%12345 172.31.182.143
docker push $REPOSITORY
'''
}
stage('Deploy to k8s') {
sh 'K8S_MASTER="172.31.182.152";APP="sscp-transaction";ssh ${K8S_MASTER} kubectl delete deployment.apps/${APP};ssh ${K8S_MASTER} istioctl kube-inject -f /root/sscp/${APP}/${APP}.yaml "|" kubectl apply -f -'
}
}