-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile
167 lines (148 loc) · 4.39 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
#!groovy
pipeline {
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}
agent {
node {
label 'docker'
}
}
stages {
stage('Compute Version') {
steps {
script {
version = computeVersion()
}
}
}
stage('Apply Cache') {
steps {
sh 'rm -rf public .cache website.tar.gz || true'
googleStorageDownload bucketUri: 'gs://scm-manager/cache/website.tar.gz', credentialsId: 'ces-demo-instances', localDirectory: '.'
sh 'tar xfz cache/website.tar.gz'
sh 'rm -rf cache'
}
}
stage('Build') {
agent {
docker {
image 'scmmanager/node-build:14.16.1'
label 'docker'
args "-v ${env.WORKSPACE}:/tmp/app -e HOME=/tmp/app"
reuseNode true
}
}
steps {
sh "yarn install"
withCredentials([usernamePassword(credentialsId: 'cesmarvin', passwordVariable: 'GITHUB_API_TOKEN', usernameVariable: 'GITHUB_ACCOUNT')]) {
sh "yarn run collect-content"
}
withCredentials([string(credentialsId: "scmm-website-algolia-adminkey", variable: "ALGOLIA_ADMIN_KEY")]) {
withEnv([
"SITE_URL=${siteUrl}",
"GATSBY_ALGOLIA_APP_ID=UEI29OVBL2",
"GATSBY_ALGOLIA_SEARCH_KEY=22ec6368da4c084bd10071dd45899bb5",
"ALGOLIA_DRY_RUN=${env.BRANCH_NAME != 'master'}"
]) {
// we have to ensure that the build uses the same path
// on all build nodes to avoid broken gatsby caches.
// The workspace is mounted to /tmp/app, see docker agent.
sh "cd /tmp/app && yarn run build"
}
}
}
}
stage('Image') {
steps {
script {
def image = docker.build "scmmanager/website:${version}", "--build-arg=SERVER_NAME=${serverName} ."
docker.withRegistry('', 'cesmarvin-dockerhub-access-token') {
image.push()
}
}
}
}
stage('Staging Deployment') {
when {
branch 'staging'
}
agent {
docker {
image 'lachlanevenson/k8s-helm:v3.2.1'
label 'docker'
reuseNode true
args '--entrypoint=""'
}
}
steps {
withCredentials([file(credentialsId: 'helm-client-scm-manager', variable: 'KUBECONFIG')]) {
sh "helm upgrade --install --values=deployment/staging.yml --set image.tag=${version} staging-website deployment/website"
}
}
}
stage('Deployment') {
when {
branch 'master'
}
agent {
docker {
image 'lachlanevenson/k8s-helm:v3.2.1'
label 'docker'
reuseNode true
args '--entrypoint=""'
}
}
steps {
withCredentials([file(credentialsId: 'helm-client-scm-manager', variable: 'KUBECONFIG')]) {
sh "helm upgrade --install --set image.tag=${version} website deployment/website"
}
}
}
stage('Push to GitHub') {
when {
branch 'master'
}
steps {
authGit 'cesmarvin', "push -f https://github.com/scm-manager/website HEAD:${env.BRANCH_NAME}"
}
}
stage('Trigger Dependend Builds') {
when {
branch 'master'
}
steps {
build job: 'scm-manager/plugin-center-api/master', wait: false
build job: 'scm-manager/alerts/main', wait: false
}
}
stage('Update Cache') {
when {
branch 'master'
}
steps {
sh "tar cfz website.tar.gz public .cache"
googleStorageUpload bucket: 'gs://scm-manager/cache', credentialsId: 'ces-operations-internal', pattern: 'website.tar.gz'
}
}
}
}
String version
String getSiteUrl() {
return "https://${serverName}"
}
String getServerName() {
return env.BRANCH_NAME == 'staging' ? 'staging-website.scm-manager.org' : 'scm-manager.org'
}
String computeVersion() {
def commitHashShort = sh(returnStdout: true, script: 'git rev-parse --short HEAD')
return "${new Date().format('yyyyMMddHHmm')}-${commitHashShort}".trim()
}
void authGit(String credentials, String command) {
withCredentials([
usernamePassword(credentialsId: credentials, usernameVariable: 'AUTH_USR', passwordVariable: 'AUTH_PSW')
]) {
sh "git -c credential.helper=\"!f() { echo username='\$AUTH_USR'; echo password='\$AUTH_PSW'; }; f\" ${command}"
}
}