diff --git a/.ci/Jenkinsfile b/.ci/Jenkinsfile index 3a796c8ac0..61b7658858 100644 --- a/.ci/Jenkinsfile +++ b/.ci/Jenkinsfile @@ -90,6 +90,38 @@ pipeline { } } } + /** + Publish Docker images. + */ + stage('Publish Docker image'){ + when { + not { changeRequest() } + } + environment { + DOCKER_IMG_TAG = "${env.DOCKER_IMG}:${env.GIT_BASE_COMMIT}" + DOCKER_IMG_TAG_BRANCH = "${env.DOCKER_IMG}:${env.BRANCH_NAME}" + } + steps { + cleanup() + pushDockerImage() + } + } + /** + Publish PR Docker images. + */ + stage('Publish PR Docker image'){ + when { + changeRequest() + } + environment { + DOCKER_IMG_TAG = "${env.DOCKER_IMG_PR}:${env.GIT_BASE_COMMIT}" + DOCKER_IMG_TAG_BRANCH = "${env.DOCKER_IMG_PR}:${env.BRANCH_NAME}" + } + steps { + cleanup() + pushDockerImage() + } + } } post { cleanup { @@ -104,3 +136,28 @@ def cleanup(){ } unstash 'source' } + + +def pushDockerImage(){ + dir("${BASE_DIR}"){ + dockerLogin(secret: "${env.DOCKER_REGISTRY_SECRET}", + registry: "${env.DOCKER_REGISTRY}") + sh(label: 'Build Docker image', + script: """docker build \ + -t ${env.DOCKER_IMG_TAG} \ + --label BRANCH_NAME=${env.BRANCH_NAME} \ + --label GIT_SHA=${env.GIT_BASE_COMMIT} \ + --label GO_VERSION=${env.GO_VERSION} \ + --label TIMESTAMP=\$(date +%Y-%m-%d_%H:%M) \ + . + """) + retryWithSleep(retries: 3, seconds: 5, backoff: true){ + sh(label: 'Push Docker image sha', + script: "docker push ${env.DOCKER_IMG_TAG}") + sh(label: 'Re-tag Docker image', + script: "docker tag ${env.DOCKER_IMG_TAG} ${env.DOCKER_IMG_TAG_BRANCH}") + sh(label: 'Push Docker image name', + script: "docker push ${env.DOCKER_IMG_TAG_BRANCH}") + } + } +}