简体   繁体   English

JENKINS:在标记时使用来自 github 的 webhook 构建/标记 docker 图像以注册

[英]JENKINS : Build/Tag docker Image to registry with webhook from github when tags

I have this pipeline in the root of my github project, with the Dockerfile:我在我的 github 项目的根目录中有这个管道,带有 Dockerfile:

pipeline {
  environment {
    def registry = 'registry/mydocker'
    def registryCredential = 'Docker'
    def dockerImage = ''
  }
  agent {
    kubernetes {
      yamlFile 'DockerPod.yaml'
    }
  }
  stages {
    stage('Slack Notification') {
      steps{
        slackSend (color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
      }
    }
    stage('Application Code Checkout from Git') {
      steps{
        node('master') {
          echo 'Pulling...' + env.GIT_BRANCH
          echo 'Pulling...' + env.VERSION
          echo 'Pulling...' + env.TAG_NAME
          checkout scm
        }
      }
    }
    stage('Building image') {
      steps{
        container('docker') {
          script {
            sh('test=${GIT_BRANCH##*/}')
            withCredentials([string(credentialsId: 'github_access_token', variable: 'TOKEN')]) {
              dockerImage = docker.build("${registry}:${test}","-f ./Dockerfile ./ --no-cache --build-arg VERSION=${test} --build-arg TOKEN=${TOKEN}")
            }
            docker.withRegistry('https://registry-1.docker.io/v2/', registryCredential) {
              dockerImage.push()
            }
          }
        }
      }
    }
  }
  post {
    success {
      slackSend (color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
    }
    failure {
      slackSend (color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
    }
  }
}

My pipeline have this configuration:我的管道有这样的配置:

refspecs == +refs/tags/*:refs/remotes/origin/tags/*

I don't understand the principe of "tag" with scm configuration.我不明白带有 scm 配置的“标签”的原理。

My objective is: when a new tag is comming to github, a webhook execute this pipeline, and i generate the docker image with the same tag.我的目标是:当一个新标签进入 github 时,一个 webhook 执行这个管道,我生成具有相同标签的 docker 图像。

Here you should use Credential Plugin for Jenkins for storing your "registryCredential" variable.在这里,您应该使用 Jenkins 的 Credential Plugin 来存储您的“registryCredential”变量。 You should not write like this.你不应该这样写。

For Docker image build with the tag, you can use: docker build -t <service-name>:"$TAG_NAME" .对于带有标签的 Docker 镜像构建,您可以使用: docker build -t <service-name>:"$TAG_NAME"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM