Fork me on GitHub

jenkinsFile打tag脚本简介

pipeline {
  agent {
    node {
      label 'maven'
    }

  }
  stages {
  # 指定测试通过版本打tag成镜像推送到服务器
    stage('Build and Push') {
      steps {
        container('maven') {
          withCredentials([usernamePassword(credentialsId : '镜像私服账号' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
            sh '''#登录Harbor
			# 镜像私服账号名为凭证名,$DOCKER_PASSWORD,$DOCKER_USERNAME为凭证中配置参数
echo "$DOCKER_PASSWORD" | docker login 镜像地址 -u "$DOCKER_USERNAME" --password-stdin'''
            sh '''#获取镜像
IMAGE=镜像地址/工程目录/jxcc-report-server
#sourcetag为指定的测试环境通过测试的镜像版本号,如SNAPSHOT-TEST-1
IMAGE_SOURCETAG=${IMAGE}:${sourcetag}
echo "Get image: ${IMAGE_SOURCETAG}"
docker pull ${IMAGE_SOURCETAG}
#tag为将该测试通过的镜像打包tag的版本,如0.0.1
IMAGE_TAG=${IMAGE}:${tag}
echo "To tag image:IMAGE_TAG"
# 打tag
docker tag ${IMAGE_SOURCETAG} ${IMAGE_TAG}
# tag推送
docker push ${IMAGE_TAG}'''
          }

        }

      }
    }

  }
}
posted @ 2022-08-05 09:05  doMakeBetter  阅读(327)  评论(0编辑  收藏  举报