jenkins-pipeline 构建自动化流水项目实践
构建自己的jenkins镜像
FROM jenkinsci/blueocean:latest
#安装jdk
ADD jdk-8u45-linux-x64.tar.gz /opt/jdk
ENV JAVA_HOME /opt/jdk/jdk1.8.0_45
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
##安装maven
ADD apache-maven-3.6.3-bin.tar.gz /opt/maven
ENV MAVEN_HOME /opt/maven/apache-maven-3.6.3
ENV PATH $PATH:$MAVEN_HOME/bin
jenkins启动
docker run -d -u root -p 8082:8080 -v /opt/data/jenkins_home:/var/jenkins_home -v /opt/data/maven_conf:/opt/maven/apache-maven-3.6.3/conf -v /root/.m2:/root/.m2 -v /root/.ssh:/root/.ssh -v /var/run/docker.sock:/var/run/docker.sock ttj-jenkins
构建项目的Dockerfile
FROM openjdk:8-jdk
LABEL maintainer=ttj
#启动自行加载 服务名-prod.yml配置
ENV PARAMS="--server.port=8080 --spring.profiles.active=dev"
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY target/*.jar /app.jar
EXPOSE 8080
# hospital-manage
ENTRYPOINT ["/bin/sh","-c","java -Dfile.encoding=utf8 -Djava.security.egd=file:/dev/./urandom -jar /app.jar ${PARAMS}"]
jenkinsfile
pipeline {
agent any
environment {
harbor_url = 'registry.cn-qingdao.aliyuncs.com'
harbor_project = 'ttj_mall'
currentProjectName = 'mall-admin'
IMAGE_NAME = 'mall-admin'
IMAGE_TAR = 'mall-admin.tar'
harbor_auth = 'cd4b7466-35cb-4e8d-8a49-dc999d36c6f0'
SSH_NAME = 'dev_server'
SSH_DIR = '/opt/pro/vue'
tag = 'latest'
currentProjectPort = '8089'
activeProfile = 'test'
}
stages {
stage('拉取代码') {
agent none
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/release']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '45bf0388-516f-41b2-804c-9a630ed46e6d', url: 'http://****.git']]])
sh 'scp deployCluster.sh root@172.31.0.5:/opt'
}
}
stage('项目编译') {
agent none
steps {
sh 'mvn clean package -Dmaven.test.skip=true'
}
}
stage('构建镜像') {
parallel {
stage('构建mall-admin镜像') {
agent none
steps {
sh 'ls mall-admin/target'
sh 'docker build -t ${IMAGE_NAME} -f mall-admin/Dockerfile ./mall-admin/'
sh "docker save ${IMAGE_NAME} > ${IMAGE_TAR}"
}
}
}
}
stage('镜像推送') {
agent none
steps {
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'docker tag ${IMAGE_NAME}:${tag} ${harbor_url}/${harbor_project}/${IMAGE_NAME}'
//登录到Harbor
sh "docker login -u ${username} -p ${password} ${harbor_url}"
sh 'docker push ${harbor_url}/${harbor_project}/${IMAGE_NAME}'
}
}
}
stage("部署到服务器") {
steps {
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "sh /opt/deployCluster.sh ${harbor_url} ${harbor_project} ${currentProjectName} ${tag} ${currentProjectPort} ${username} ${password} ${activeProfile}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '/opt/deployCluster.sh')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
post {
always {
// 清理临时容器和镜像
sh "docker ps -a|grep Exited|awk '{print \$1}'|xargs -I {} docker rm {}"
sh "docker images|grep '<none>'|awk '{print \$3}'|xargs -I {} docker image rm {} > /dev/null 2>&1 || true"
cleanWs()
}
}
}
deployCluster.sh
#!/bin/sh
#接收外部参数
harbor_url=$1
harbor_project_name=$2
project_name=$3
tag=$4
port=$5
userName=$6
password=$7
profile=$8
imageName=$harbor_url/$harbor_project_name/$project_name:$tag
echo "$imageName"
#查询容器是否存在,存在则删除
containerId=`docker ps -a | grep -w $project_name:$tag | awk '{print $1}'`
if [ "$containerId" != "" ] ; then
#停掉容器
docker stop $containerId
#删除容器
docker rm $containerId
echo "成功删除容器"
fi
#查询镜像是否存在,存在则删除
imageId=`docker images | grep -w $project_name | awk '{print $3}'`
if [ "$imageId" != "" ] ; then
docker rmi -f $imageId
echo "成功删除镜像"
fi
docker login -u $userName -p $password $harbor_url
# 下载镜像
docker pull $imageName
# 启动容器
docker run -di -p $port:$port $imageName $profile
docker logout $harbor_url
echo "容器启动成功"