forked from cfpb/hmda-data-browser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
76 lines (67 loc) · 3.18 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
podTemplate(label: 'buildDockerContainer', containers: [
containerTemplate(name: 'docker', image: 'docker', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'helm', image: 'lachlanevenson/k8s-helm', ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
]) {
node('buildDockerContainer') {
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}, shortCommit: ${shortCommit}, isDeployPR: ${isDeployPR}"
stage('Build And Publish Docker Image') {
container('docker') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'dockerhub',
usernameVariable: 'DOCKER_HUB_USER', passwordVariable: 'DOCKER_HUB_PASSWORD']]) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'hmda-platform-jenkins-service',
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-data-browser ."
if (gitTagged || gitBranch == "master" || isDeployPR) {
//Push to Dockerhub
sh """
docker tag ${env.DOCKER_HUB_USER}/hmda-data-browser ${env.DOCKER_HUB_USER}/hmda-data-browser:${env.DOCKER_TAG}
docker login -u ${env.DOCKER_HUB_USER} -p ${env.DOCKER_HUB_PASSWORD}
docker push ${env.DOCKER_HUB_USER}/hmda-data-browser:${env.DOCKER_TAG}
"""
//Push to Internal Docker Repo
sh """
docker tag ${env.DOCKER_HUB_USER}/hmda-data-browser:${env.DOCKER_TAG} ${DOCKER_REGISTRY_URL}/${env.DOCKER_HUB_USER}/hmda-data-browser:${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-data-browser:${env.DOCKER_TAG}
docker image prune -f
"""
}
}
}
}
}
}
stage('Deploy') {
if (env.BRANCH_NAME == 'master' || isDeployPR) {
container('helm') {
sh "helm upgrade --install --force \
--namespace=default \
--values=kubernetes/hmda-data-browser/values.yaml \
--set commitId=$shortCommit \
--set image.pullPolicy=Always \
--set image.tag=${env.DOCKER_TAG} \
hmda-data-browser \
kubernetes/hmda-data-browser"
}
}
}
}
}