-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.prod-release
137 lines (125 loc) · 5.33 KB
/
Jenkinsfile.prod-release
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
131
132
133
134
135
136
137
properties([
disableConcurrentBuilds(),
parameters([
run(description: 'Build to promote to production', filter: 'SUCCESSFUL', name: 'BUILD', projectName: 'Sports Scores/main')
])
])
def findSHAOfImage(image) {
def nameMatch = (image =~ /^([^\/]+)\/(.+?):(.+)$/)[0]
def result
withCredentials([usernameColonPassword(credentialsId: 'docker-registry', variable: 'DOCKER_AUTH')]) {
String auth = env.DOCKER_AUTH.bytes.encodeBase64().toString()
def manifestRes = httpRequest(
consoleLogResponseBody: true,
url: "https://registry.comp.ystv.co.uk/v2/${nameMatch[2]}/manifests/${nameMatch[3]}",
customHeaders:[
[name: 'Accept', value: 'application/vnd.docker.distribution.manifest.v2+json'],
[name: 'Authorization', value: "Basic ${auth}"]
]
)
def manifest = readJSON(text: manifestRes.content)
def configBlobID = manifest.config.digest
def configRes = httpRequest(
consoleLogResponseBody: true,
url: "https://registry.comp.ystv.co.uk/v2/${nameMatch[2]}/blobs/${configBlobID}",
customHeaders:[
[name: 'Authorization', value: "Basic ${auth}"]
]
)
def config = readJSON(text: configRes.content)
result = config.config.Labels.'ystv.git-rev'
}
return result
}
node('docker') {
def deployBldNum = (env.BUILD =~ /\/(\d+)\/?$/)[0][1]
def issueKeys = []
def isRollBack = false
stage('Checkout') {
checkout(
changelog: false,
poll: false,
scm: [
$class: 'GitSCM',
branches: [[name: '*/main']],
extensions: [],
userRemoteConfigs: [[credentialsId: 'github-app-ystv', url: 'https://github.com/ystv/ystv-sports-graphics']
]
]
)
}
stage('Preparation') {
def currentSHAs = []
for (job in ["sports-scores-prod", "sports-scores-bundle-prod"]) {
// Find the build that is currently live
def currentJSON = sh returnStdout: true, script: "nomad job inspect $job"
def current = readJSON(text: currentJSON)
// Find the git SHAs of its images
def images = current.Job.TaskGroups.collectMany { group ->
group.Tasks.collect { it.Config.image }
}.findAll { it.contains("registry.comp.ystv.co.uk/sports-scores") }
def shas = images.collect { findSHAOfImage(it) }.unique(false)
currentSHAs.addAll(shas)
def currentBuildNum = images.collect {
((it =~ /.*:(\d+)$/)[0][1]).toInteger()
}.min()
if (currentBuildNum > deployBldNum.toInteger()) {
isRollBack = true
}
}
def nextSHAs = [
"registry.comp.ystv.co.uk/sports-scores/client:$deployBldNum",
"registry.comp.ystv.co.uk/sports-scores/server:$deployBldNum",
"registry.comp.ystv.co.uk/sports-scores/bundle:$deployBldNum"
].collect { findSHAOfImage(it) }.unique(false)
for (sha1 in currentSHAs) {
for (sha2 in nextSHAs) {
def issueKeyStrs = (sh(returnStdout: true, script: "git log ${sha1}..${sha2} | grep -ohE '[A-Z][A-Z0-9]*-[0-9]+' || test \$? = 1")).split("\n")
// If this is a rollback, we may have to check backwards
if (issueKeyStrs.size() == 0 && isRollBack) {
issueKeyStrs = (sh(returnStdout: true, script: "git log ${sha2}..${sha1} | grep -ohE '[A-Z][A-Z0-9]*-[0-9]+' || test \$? = 1")).split("\n")
}
issueKeys.addAll(issueKeyStrs)
}
}
// Send all this to jira!
jiraSendDeploymentInfo(
environmentType: 'production',
environmentId: 'prod',
environmentName: 'production',
issueKeys: issueKeys,
site: 'ystv.atlassian.net',
state: 'in_progress'
)
}
stage('Deploy') {
try {
build job: 'Deploy Nomad Job', parameters: [
string(name: 'JOB_FILE', value: 'sports-graphics-prod.nomad'),
text(name: 'TAG_REPLACEMENTS', value: "registry.comp.ystv.co.uk/sports-scores/server:${deployBldNum} registry.comp.ystv.co.uk/sports-scores/client:${deployBldNum}")
]
build job: 'Deploy Nomad Job', parameters: [
string(name: 'JOB_FILE', value: 'sports-graphics-bundle-prod.nomad'),
text(name: 'TAG_REPLACEMENTS', value: "registry.comp.ystv.co.uk/sports-scores/bundle:${deployBldNum}")
]
jiraSendDeploymentInfo(
environmentType: 'production',
environmentId: 'prod',
environmentName: 'production',
issueKeys: issueKeys,
site: 'ystv.atlassian.net',
state: isRollBack ? 'rolled_back' : 'successful'
)
} catch (Exception e) {
jiraSendDeploymentInfo(
environmentType: 'production',
environmentId: 'prod',
environmentName: 'production',
issueKeys: issueKeys,
site: 'ystv.atlassian.net',
state: 'failed'
)
throw e
}
}
}