From b93a64fe973100768e856a5cd7720075b8f08ee8 Mon Sep 17 00:00:00 2001
From: Viktor Vorobev <vorobev@in.tum.de>
Date: Tue, 22 Jun 2021 13:50:40 +0000
Subject: [PATCH] Merged in NRRPLT-8297-proper-image-tag (pull request #33)
[NRRPLT-8297] Use branch name as docker image tag for CI/CD.
Approved-by: Ugo Albanese
---
Jenkinsfile | 25 ++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/Jenkinsfile b/Jenkinsfile
index 9738080..4c90936 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -5,6 +5,21 @@
// https://www.jenkins.io/doc/book/pipeline/shared-libraries/
@Library('nrp-shared-libs@master') _
+// Before starting pipeline, we try to get the proper image tag
+def DEFAULT_BRANCH = 'development'
+// selectTopicBranch function is used to choose the correct branch name as topic
+// the function is defined in shared libs
+//
+// In case there is a PR for a branch, then Jenkins runs a pipeline for this pull request, not for the branch,
+// even if there are new commits to the branch (until PR is not closed). The BRANCH_NAME variable in this case is something like PR-###
+// The name of the branch which is merged is stored in CHANGE_BRANCH variable. Thus, we should choose CHANGE_BRANCH as topic
+//
+// If there is a branch without PR, then Jenkins creates build for it normally for every push and the branch name is stored in BRANCH_NAME variable.
+// CHANGE_BRANCH is empty in this case. Thus, we choose BRANCH_NAME as topic for branches without PR.
+def TOPIC_BRANCH = selectTopicBranch(env.BRANCH_NAME, env.CHANGE_BRANCH)
+// We try to pull the image with the topic name, or use default tag otherwise
+def IMG_TAG = checkImageTag("${TOPIC_BRANCH}", "${DEFAULT_BRANCH}")
+
pipeline {
environment {
USER_SCRIPTS_DIR = "user-scripts"
@@ -14,15 +29,19 @@ pipeline {
// GIT_CHECKOUT_DIR is a dir of the main project (that was pushed)
GIT_CHECKOUT_DIR = "${env.VIRTUAL_COACH_DIR}"
- TOPIC_BRANCH = selectTopicBranch(env.BRANCH_NAME, env.CHANGE_BRANCH)
- DEFAULT_BRANCH = 'development'
+ // That is needed to pass the variables into environment with the same name from
+ // Jenkins global scope (def ..=..)
+ TOPIC_BRANCH = "${TOPIC_BRANCH}"
+ DEFAULT_BRANCH = "${DEFAULT_BRANCH}"
CODE_COVERAGE_LINE = 74
}
agent {
docker {
+ label 'ci_label'
+
// NEXUS_REGISTRY_IP and NEXUS_REGISTRY_PORT are Jenkins global variables
- image "${env.NEXUS_REGISTRY_IP}:${env.NEXUS_REGISTRY_PORT}/nrp:development"
+ image "${env.NEXUS_REGISTRY_IP}:${env.NEXUS_REGISTRY_PORT}/nrp:${IMG_TAG}"
args '--entrypoint="" -u root --privileged'
}
}
--
GitLab