-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
130 lines (117 loc) · 3.98 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env groovy
appName = 'local-office-search-api'
dockerRegistryDomain = '979633842206.dkr.ecr.eu-west-1.amazonaws.com'
dockerRegistryUrl = "https://${dockerRegistryDomain}"
ecrCredentialId = 'ecr:eu-west-1:cita-devops'
BUILD_STAGE = 'Build'
deployBranches = ['develop','qa']
isRelease = deployBranches.contains(env.BRANCH_NAME)
def tagPrefix = isRelease ? '' : 'dev_'
dockerTag = "${tagPrefix}${env.BUILD_TAG}"
node('docker && awsaccess') {
cleanWs()
checkout scm
dockerTag += "_${getSha()}"
currentBuild.displayName = "${env.BUILD_NUMBER}: ${dockerTag}"
withDockerRegistry(registry: [credentialsId: 'docker_hub']) {
withEnv([
"SEARCH_API_VERSION_TAG=:${dockerTag}",
"SEARCH_API_PR_TAG=:${env.BRANCH_NAME}"
]) {
dockerBuild(context: pwd(), tag: dockerImageId())
}
}
}
@NonCPS
def dockerImageId() {
"local-office-search-api:${dockerTag}"
}
def dockerBuild(Map config) {
try {
def image = null
stage("Build ${config.tag}") {
BUILD_STAGE = "Build ${config.tag}"
dir(config.context) {
writeBuildStatus {
file = 'public/.buildInfo'
}
image = docker.build(config.tag)
}
}
stage("Lint ${config.tag}") {
BUILD_STAGE = "Lint ${config.tag}"
def lintScript = 'bin/jenkins/lint'
if (fileExists("${config.context}/${lintScript}")) {
sh "${lintScript}"
} else {
echo 'Nothing to lint'
}
}
stage("Test ${config.tag}") {
BUILD_STAGE = "Test ${config.tag}"
// make sure rails boots ok in production mode
sh "docker run -e RAILS_ENV=production -e SECRET_KEY_BASE=smoketest -e LOCAL_OFFICE_SEARCH_EPISERVER_USER=user -e LOCAL_OFFICE_SEARCH_EPISERVER_PASSWORD=smoke --rm ${config.tag} timeout --preserve-status -s 1 15 rails s"
def testScript = 'bin/jenkins/test'
if (fileExists("${config.context}/${testScript}")) {
sh "${testScript}"
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: './coverage',
reportFiles: 'index.html',
reportName: 'Test Report'
])
} else {
echo 'No tests'
}
}
stage("Push ${config.tag}") {
BUILD_STAGE = "Push ${config.tag}"
docker.withRegistry(dockerRegistryUrl, ecrCredentialId) {
image.push()
if (!deployBranches.contains(env.BRANCH_NAME)) {
// add PR number as tag
image.push(env.BRANCH_NAME)
}
else {
deployBranches.each { branch ->
if (env.BRANCH_NAME == branch) {
// push latest tag
image.push(branch)
// deploy latest tag on environment
build(job:"../public-website-config/${branch}", wait: false)
}
if (env.BRANCH_NAME == 'develop') {
// We tag as latest here, because any tests failures will prevent pushing the image anyway.
image.push('latest')
}
}
}
}
}
} catch (all) {
// Set result manually as Jenkins does not update this (or currentResult) until after
// the finally block is executed.
currentBuild.result = 'FAILURE'
throw all
} finally {
sh 'bin/jenkins/down || true'
if (deployBranches.contains(env.BRANCH_NAME)) {
withCredentials([string(credentialsId: 'slack-plugin', variable: 'SLACK_TOKEN')]) {
def message = "${env.BUILD_TAG}\n"
message += "STAGE: ${BUILD_STAGE}\n"
message += "${sh(returnStdout: true, script: "git log -1 --pretty=format':%h %s (%an, %ar)'")}\n"
message += currentBuild.result == 'FAILURE' ?
"FAILURE ${buildLink()}console" :
'GREAT SUCCESS'
slackSend(
token: SLACK_TOKEN,
channel: '#content-platform_builds',
color: currentBuild.result == 'FAILURE' ? 'danger' : 'good',
message: message
)
}
}
}
}