jenkins-pipeline 構建自動化流水專案實踐
阿新 • • 發佈:2022-05-07
構建自己的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/settings.xml:/opt/maven/apache-maven-3.6.3/conf/settings.xml -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 = 'mall' currentProjectName = 'mall-admin' DOCKERHUB_NAMESPACE = 'mall' 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 [email protected]:/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 "容器啟動成功"