From d84a3172be6cbd3376001da0b08e8fde66042b7d Mon Sep 17 00:00:00 2001 From: Kenneth Gudel Date: Mon, 4 Feb 2019 10:58:23 -0500 Subject: [PATCH 1/2] enable branch deployment using docker log and git commit as env variable in pod to force deployment --- Jenkinsfile | 49 +++++++++++++------ .../hmda-pub-ui/templates/deployment.yaml | 3 ++ kubernetes/hmda-pub-ui/values.yaml | 1 + 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4b30da57..7d3967a7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,10 +6,25 @@ volumes: [ hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'), ]) { node('buildDockerContainer') { - def repo = checkout scm - def gitCommit = repo.GIT_COMMIT - def gitBranch = repo.GIT_BRANCH - def shortGitCommit = "${gitCommit[0..10]}" + sh "env | sort" + + def repo = checkout scm + def gitBranch = repo.GIT_BRANCH + def gitCommit = repo.GIT_COMMIT + def shortCommit = repo.GIT_COMMIT[0..7] + def isDeployPR = sh(returnStdout: true, script: "git log -1").contains("[deploy pr]") + def gitTagged = env.TAG_NAME != null + + if (gitBranch == "master") { + env.DOCKER_TAG = "latest" + } else if (gitTagged) { + env.DOCKER_TAG = env.TAG_NAME + } else { + env.DOCKER_TAG = env.BRANCH_NAME + } + + println "DOCKER_TAG: ${env.DOCKER_TAG}, TAG_NAME: ${env.TAG_NAME}, gitbranch: ${gitBranch}, commitId: ${commitId}, isDeployPR: ${isDeployPR}" + stage('Build And Publish Docker Image') { container('docker') { @@ -19,16 +34,16 @@ volumes: [ usernameVariable: 'DTR_USER', passwordVariable: 'DTR_PASSWORD']]) { withCredentials([string(credentialsId: 'internal-docker-registry', variable: 'DOCKER_REGISTRY_URL')]){ sh "docker build --rm -t=${env.DOCKER_HUB_USER}/hmda-pub-ui ." - if (env.TAG_NAME != null || gitBranch == "master") { - if (gitBranch == "master") { - env.DOCKER_TAG = "latest" - } else { - env.DOCKER_TAG = env.TAG_NAME - } + if (gitTagged || gitBranch == "master" || isDeployPR) { + //Push to Dockerhub sh """ docker tag ${env.DOCKER_HUB_USER}/hmda-pub-ui ${env.DOCKER_HUB_USER}/hmda-pub-ui:${env.DOCKER_TAG} docker login -u ${env.DOCKER_HUB_USER} -p ${env.DOCKER_HUB_PASSWORD} docker push ${env.DOCKER_HUB_USER}/hmda-pub-ui:${env.DOCKER_TAG} + """ + + //Push to Internal Docker Repo + sh """ docker tag ${env.DOCKER_HUB_USER}/hmda-pub-ui:${env.DOCKER_TAG} ${DOCKER_REGISTRY_URL}/${env.DOCKER_HUB_USER}/hmda-pub-ui:${env.DOCKER_TAG} docker login ${DOCKER_REGISTRY_URL} -u ${env.DTR_USER} -p ${env.DTR_PASSWORD} docker push ${DOCKER_REGISTRY_URL}/${env.DOCKER_HUB_USER}/hmda-pub-ui:${env.DOCKER_TAG} @@ -42,14 +57,16 @@ volumes: [ } stage('Deploy') { - if (env.BRANCH_NAME == 'master') { + if (env.BRANCH_NAME == 'master' || isDeployPR) { container('helm') { sh "helm upgrade --install --force \ - --namespace=default \ - --values=kubernetes/hmda-pub-ui/values.yaml \ - --set image.tag=latest \ - hmda-pub-ui \ - kubernetes/hmda-pub-ui" + --namespace=default \ + --values=kubernetes/hmda-pub-ui/values.yaml \ + --set commitId=$shortCommit \ + --set image.pullPolicy=Always \ + --set image.tag=${env.DOCKER_TAG} \ + hmda-pub-ui \ + kubernetes/hmda-pub-ui" } } } diff --git a/kubernetes/hmda-pub-ui/templates/deployment.yaml b/kubernetes/hmda-pub-ui/templates/deployment.yaml index eb147ffd..a91dfe1f 100644 --- a/kubernetes/hmda-pub-ui/templates/deployment.yaml +++ b/kubernetes/hmda-pub-ui/templates/deployment.yaml @@ -26,6 +26,9 @@ spec: containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + env: + - name: GIT_COMMIT + value: {{ .Values.commitId }} volumeMounts: - name: tz-config mountPath: /etc/localtime diff --git a/kubernetes/hmda-pub-ui/values.yaml b/kubernetes/hmda-pub-ui/values.yaml index ac9282f6..5d91da80 100644 --- a/kubernetes/hmda-pub-ui/values.yaml +++ b/kubernetes/hmda-pub-ui/values.yaml @@ -3,6 +3,7 @@ # Declare variables to be passed into your templates. replicaCount: 1 +commitId: '' image: repository: hmda/hmda-pub-ui From 4deb77b2d3e8dd88c6f354c819706278b42f23de Mon Sep 17 00:00:00 2001 From: Kenneth Gudel Date: Mon, 4 Feb 2019 11:13:04 -0500 Subject: [PATCH 2/2] update variable name --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 7d3967a7..9d71e3f7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -23,7 +23,7 @@ volumes: [ env.DOCKER_TAG = env.BRANCH_NAME } - println "DOCKER_TAG: ${env.DOCKER_TAG}, TAG_NAME: ${env.TAG_NAME}, gitbranch: ${gitBranch}, commitId: ${commitId}, isDeployPR: ${isDeployPR}" + println "DOCKER_TAG: ${env.DOCKER_TAG}, TAG_NAME: ${env.TAG_NAME}, gitbranch: ${gitBranch}, shortCommit: ${shortCommit}, isDeployPR: ${isDeployPR}" stage('Build And Publish Docker Image') {