This repository has been archived by the owner on Jul 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
106 lines (103 loc) · 4.02 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!groovy
// Build and push docker images by branches and tags
// Required: "Basic Branch Build Strategies Plugin" and "GitHub SQS plugin"
// GitHub Behaviours: Discover tags
// GitHub Behaviours: Clean Before Checkout
// GitHub Build strategies: Regular Branches
// GitHub Build strategies: Tags
// Scan Repository Triggers Periodically if no hooks
properties([disableConcurrentBuilds()])
def getSlackJobNameMsg () {
return "<${env.BUILD_URL}console|#${env.BUILD_NUMBER}>"
}
def getSlackProjectNameMsg () {
return "<${REPO_URL}/tree/${GIT_BRANCH}|${REPO_NAME}:${GIT_BRANCH}> (<${REPO_URL}/commit/${GIT_COMMIT}|${GIT_COMMIT_SHORT}>)"
}
pipeline {
environment {
GIT_COMMIT_SHORT = "${env.GIT_COMMIT[0..7]}" // first 7 symbols of commit hash (because GH show only 7)
REPO_URL = "${GIT_URL[0..-5]}" // removed postfix `.git`
REPO_NAME = "${REPO_URL - ~/.*\//}" // removed anything before `docker-`
IMAGE_NAME = "${REPO_NAME}" // use for readability
}
agent {
label 'master'
}
triggers {
githubPush()
}
options {
buildDiscarder(logRotator(numToKeepStr: '15', artifactNumToKeepStr: '15'))
timestamps()
}
stages {
stage("Preparations") {
steps {
script {
slackSend channel: '#jenkins',
color: 'good',
message: "Job ${getSlackJobNameMsg()} for building ${getSlackProjectNameMsg()} started"
}
}
}
stage("Build docker image") {
steps {
script {
echo " ============== start building :latest from exodusmovement/${REPO_NAME}:${GIT_BRANCH} =================="
sh """
docker build \
-t quay.io/exodusmovement/${IMAGE_NAME}:latest \
-t quay.io/exodusmovement/${IMAGE_NAME}:${GIT_BRANCH} \
.
"""
currentBuild.description = "Image built, "
}
}
}
stage("Push docker image :latest") {
when { not { tag "*" } }
steps {
script {
echo " ============== start pushing :latest from exodusmovement/${REPO_NAME}:${GIT_BRANCH} =================="
withDockerRegistry([ credentialsId: "exodusmovement-quay-creds", url: "https://quay.io" ]) {
sh """
docker push quay.io/exodusmovement/${IMAGE_NAME}:latest
"""
}
currentBuild.description += "and pushed to registry"
}
}
}
stage("Push docker image :release") {
when { tag "*" }
steps {
script {
echo " ============== start pushing ${GIT_BRANCH} from exodusmovement/${REPO_NAME}:${GIT_BRANCH} =================="
withDockerRegistry([ credentialsId: "exodusmovement-quay-creds", url: "https://quay.io" ]) {
sh """
docker push quay.io/exodusmovement/${IMAGE_NAME}:${GIT_BRANCH}
"""
}
currentBuild.description += "and pushed to registry"
}
}
}
}
post {
failure {
slackSend channel: '#jenkins',
color: 'danger',
message: "Job ${getSlackJobNameMsg()} for building ${getSlackProjectNameMsg()} failed"
}
aborted {
slackSend channel: '#jenkins',
color: 'warning',
message: "Job ${getSlackJobNameMsg()} for building ${getSlackProjectNameMsg()} aborted"
}
success {
slackSend channel: '#jenkins',
color: 'good',
message: "Job ${getSlackJobNameMsg()} for building ${getSlackProjectNameMsg()} finished in ${currentBuild.durationString[0..-13]}"
}
}
}