-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
175 lines (165 loc) · 6.04 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
String registryEndpoint = 'registry.comp.ystv.co.uk'
def branch = env.BRANCH_NAME.replaceAll("/", "_")
def image
String proceed = "yes"
String serverImageName = "ystv/streamer/server:${branch}-${env.BUILD_ID}"
String forwarderImageName = "ystv/streamer/forwarder:${branch}-${env.BUILD_ID}"
String recorderImageName = "ystv/streamer/recorder:${branch}-${env.BUILD_ID}"
def productionBuild = env.TAG_NAME ==~ /v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/
pipeline {
agent {
label 'docker'
}
environment {
DOCKER_BUILDKIT = '1'
}
stages {
stage('Build images') {
parallel {
stage('Build Server') {
steps {
script {
dir("server") {
GIT_COMMIT_HASH = sh (script: "git log -n 1 --pretty=format:'%H' | head -c 7", returnStdout: true)
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
serverImage = docker.build(serverImageName, "--build-arg STREAMER_VERSION_ARG=${env.BRANCH_NAME}-${env.BUILD_ID} --build-arg STREAMER_COMMIT_ARG=${GIT_COMMIT_HASH} --no-cache .")
}
}
}
}
}
stage('Build Forwarder') {
steps {
script {
dir("forwarder") {
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
forwarderImage = docker.build(forwarderImageName, "--build-arg STREAMER_VERSION_ARG=${env.BRANCH_NAME}-${env.BUILD_ID} --no-cache .")
}
}
}
}
}
stage('Build Recorder') {
environment {
STREAMER_RECORDER_USER_UID = credentials('streamer-recorder-user-uid')
STREAMER_RECORDER_USER_GID = credentials('streamer-recorder-user-gid')
STREAMER_RECORDER_USER_GROUP_NAME = credentials('streamer-recorder-user-group-name')
}
steps {
script {
dir("recorder") {
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
recorderImage = docker.build(recorderImageName, "--build-arg STREAMER_VERSION_ARG=${env.BRANCH_NAME}-${env.BUILD_ID} --build-arg STREAMER_RECORDER_USER_UID=${STREAMER_RECORDER_USER_UID} --build-arg STREAMER_RECORDER_USER_GID=${STREAMER_RECORDER_USER_GID} --build-arg STREAMER_RECORDER_USER_GROUP_NAME=${STREAMER_RECORDER_USER_GROUP_NAME} --no-cache .")
}
}
}
}
}
}
}
stage('Push images to registry') {
parallel {
stage('Push Server image to registry') {
steps {
script {
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
serverImage.push()
if (env.BRANCH_IS_PRIMARY) {
serverImage.push('latest')
}
}
}
}
}
stage('Push Forwarder image to registry') {
steps {
script {
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
forwarderImage.push()
if (env.BRANCH_IS_PRIMARY) {
forwarderImage.push('latest')
}
}
}
}
}
stage('Push Recorder image to registry') {
steps {
script {
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
recorderImage.push()
if (env.BRANCH_IS_PRIMARY) {
recorderImage.push('latest')
}
}
}
}
}
}
}
stage('Checking existing streams') {
steps {
script {
String url = "https://streamer."
if (!productionBuild) {
url += "dev."
}
url += "ystv.co.uk/activeStreams"
final def (String response, String tempCode) =
sh(script: "curl -s -w '~~~%{response_code}' $url", returnStdout: true)
.trim()
.tokenize("~~~")
int code = Integer.parseInt(tempCode)
echo "HTTP response status code: $code"
echo "HTTP response: $response"
if (code == 200) {
if (response.contains("streams")) {
tempStreams = sh(script: "echo '$response' | jq -M '.streams'", returnStdout: true).trim()
int streams = Integer.parseInt(tempStreams)
if (streams > 0) {
echo "Pre-existing active streams: $streams, not proceeding to deploy stage"
proceed = "no"
} else {
echo "No pre-existing active streams, proceeding to deploy stage"
}
} else {
echo "Streamer not currently running, proceeding to deploy stage"
}
} else {
echo "Invalid HTTP response code: $code, proceeding to deploy stage"
}
}
}
}
stage('Deploy') {
when {
expression { proceed == "yes" }
}
stages {
stage('Development') {
when {
expression { env.BRANCH_IS_PRIMARY }
}
steps {
build(job: 'Deploy Nomad Job', parameters: [
string(name: 'JOB_FILE', value: 'streamer-dev.nomad'),
text(name: 'TAG_REPLACEMENTS', value: "${registryEndpoint}/${serverImageName} ${registryEndpoint}/${forwarderImageName} ${registryEndpoint}/${recorderImageName}")
])
}
}
stage('Production') {
when {
// Checking if it is semantic version release.
expression { return productionBuild }
}
steps {
build(job: 'Deploy Nomad Job', parameters: [
string(name: 'JOB_FILE', value: 'streamer-prod.nomad'),
text(name: 'TAG_REPLACEMENTS', value: "${registryEndpoint}/${serverImageName} ${registryEndpoint}/${forwarderImageName} ${registryEndpoint}/${recorderImageName}")
])
}
}
}
}
}
}