最近在向pipeline转变, 感觉部署shell控制灵活.,
1.首先jenkins 安装 Zentimestamp 插件后, 该插件允许自定义Jenkins BUILD_TIMESTAMP变量的日期和时间模式。 docker build中使用时间戳做tag
2. 安装 Timestamper 插件, 将时间戳添加到控制台输出
3. 可以把变量提出来使用 参数化构建过程----字符参数
4. 打开部署机api端口,实现远程调用 -H=unix:///var/run/docker.sock -H=tcp://0.0.0.0:6066 注意安全问题.
pipeline { agent any options { timestamps() } parameters { string(name: 'GIT_BRANCH', defaultValue: 'sdjz_cloud_v1.0.0b', description: 'sdjz_cloud_v1.0.0b branch') } environment { DOCKER_REGISTRY = "192.168.20.11" PROJECT= 'testjiaozhou' APP_NAME = 'admin' DEPLOY_HOST = '192.168.20.157:6066' MAVEN = "/data/maven/bin" JAVAIMAGE = "192.168.20.11/source/tomcat8_jdk1.8:201909101627" } stages { stage('Checkout') { steps { git branch: "${params.GIT_BRANCH}", credentialsId: '1111111-2222222-4855-9de1-3333333', url: 'https://code.aliyun.com/acecga-h2/api.git' } } stage('Build jar') { steps { echo "1. Maven Build project jar" sh '${MAVEN}/mvn -Dmaven.test.skip=true clean install' } } stage('Replace Dockerfile') { steps { sh """ mkdir -p deployment/ mv -f target/*.jar deployment/${APP_NAME}.jar /bin/cp /data/deployment_git/k8s-template/jar/Dockerfile deployment/ /bin/cp /data/deployment_git/k8s-template/jar/start.sh deployment/ sed -i "s#IMAGE#${JAVAIMAGE}#g" deployment/Dockerfile sed -i "s#containerport#${PORT}#g" deployment/Dockerfile sed -i "s#jenkins#${APP_NAME}#g" deployment/Dockerfile """ } } stage('Docker Build') { steps { sh """ docker build -t ${DOCKER_REGISTRY}/${PROJECT}/${APP_NAME}:${BUILD_NUMBER} deployment/ docker push ${DOCKER_REGISTRY}/${PROJECT}/${APP_NAME}:${BUILD_NUMBER} docker rmi ${DOCKER_REGISTRY}/${PROJECT}/${APP_NAME}:${BUILD_NUMBER} rm -f deployment/*.war """ } } stage('Deploy') { steps { input message: 'Are you sure Deployment?', ok: 'Yes' sh""" docker -H ${DEPLOY_HOST} rm -f ${APP_NAME} | true docker -H ${DEPLOY_HOST} run -d --name ${APP_NAME} -p ${PORT}:8080 ${DOCKER_REGISTRY}/${PROJECT}/${APP_NAME}:${BUILD_NUMBER} """ } } } post { always { emailext body: """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p> <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""", subject: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", to: ' fengjian@senyint.com' } } }