-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathJenkinsfile_k8s
159 lines (150 loc) · 4.88 KB
/
Jenkinsfile_k8s
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
def recordDeployment(owner, repo, ref, status, environmentURL, environment = "preview", description = "Deploy to preview environment") {
withCredentials([usernamePassword(credentialsId: 'github-app-infra', usernameVariable: 'GITHUB_APP', passwordVariable: 'GH_TOKEN')]) {
def json = writeJSON(returnText: true, json: [
"ref": ref,
"environment": environment,
"description": description,
"required_contexts": [],
"auto_merge": false,
"auto_inactive": false,
"transient_environment": environment != "production",
])
def id = readJSON(text: sh(script: "gh api repos/${owner}/${repo}/deployments -X POST --input - << EOF\n${json}\nEOF", returnStdout: true).trim()).id
if (id == ''){
error('Unable to create deployment')
}
json = writeJSON(returnText: true, json: [
"state": status,
"environment": environment,
"description": description,
"log_url": "${BUILD_URL}console",
"environment_url": environmentURL,
])
sh("gh api repos/${owner}/${repo}/deployments/${id}/statuses -X POST --input - << EOF\n${json}\nEOF")
}
}
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: "v1"
kind: "Pod"
metadata:
labels:
jenkins: "agent"
job: "jenkins-is-the-way"
spec:
tolerations:
- key: "os"
operator: "Equal"
value: "linux"
effect: "NoSchedule"
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
restartPolicy: "Never"
automountServiceAccountToken: false
containers:
- name: "jnlp"
image: "jenkinsciinfra/builder:2.0.11@sha256:474cd3b4a5aa5b65befa3a0a84d92a725030eba0116a2cad1bbfb33a50a40b65"
resources:
limits: {}
requests:
memory: "4Gi"
cpu: "2"
'''
}
}
environment {
TZ = "UTC"
NODE_ENV = "production"
NETLIFY = "true"
}
options {
timeout(time: 60, unit: 'MINUTES')
ansiColor('xterm')
disableConcurrentBuilds(abortPrevious: true)
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '5', numToKeepStr: '5')
}
stages {
stage('Install Dependencies') {
steps {
sh 'NODE_ENV=development npm install'
}
}
stage('Build PR') {
when { changeRequest() }
steps {
sh 'npm run build'
}
}
stage('Build Production') {
when {
branch "main"
}
environment {
GATSBY_MATOMO_SITE_ID = "2"
GATSBY_MATOMO_SITE_URL = "https://jenkins-matomo.do.g4v.dev"
}
steps {
sh 'npm run build'
}
}
stage('Lint and Test') {
environment {
NODE_ENV = "development"
}
steps {
sh '''
npm run lint
'''
}
}
stage('Deploy') {
when {
branch "main"
}
environment {
NETLIFY_AUTH_TOKEN = credentials('netlify-auth-token')
}
post {
success {
recordDeployment('jenkins-infra', 'jenkins-is-the-way', env.GIT_COMMIT, 'success', "https://jenkins-is-the-way.netlify.app", "production")
}
failure {
recordDeployment('jenkins-infra', 'jenkins-is-the-way', env.GIT_COMMIT, 'failure', "https://jenkins-is-the-way.netlify.app", "production")
}
}
steps {
sh('curl -qsL https://github.com/halkeye/netlify-golang-deploy/releases/download/v0.1.5/netlify-golang-deploy_0.1.5_Linux_x86_64.tar.gz | tar xvzf - netlify-golang-deploy; chmod +x netlify-golang-deploy; mv netlify-golang-deploy netlify-deploy')
sh('./netlify-deploy --draft=false --siteName "jenkins-is-the-way" --title "Deploy" -d ./public')
}
}
stage('Deploy to preview site') {
when {
changeRequest target: 'main'
}
environment {
NETLIFY_AUTH_TOKEN = credentials('netlify-auth-token')
}
post {
success {
recordDeployment('jenkins-infra', 'jenkins-is-the-way', pullRequest.head, 'success', "https://deploy-preview-${CHANGE_ID}--jenkins-jenkins-is-the-way.netlify.app")
}
failure {
recordDeployment('jenkins-infra', 'jenkins-is-the-way', pullRequest.head, 'failure', "https://deploy-preview-${CHANGE_ID}--jenkins-jenkins-is-the-way.netlify.app")
}
}
steps {
sh('curl -qsL https://github.com/halkeye/netlify-golang-deploy/releases/download/v0.1.5/netlify-golang-deploy_0.1.5_Linux_x86_64.tar.gz | tar xvzf - netlify-golang-deploy; chmod +x netlify-golang-deploy; mv netlify-golang-deploy netlify-deploy')
sh('./netlify-deploy --draft=true --siteName "jenkins-is-the-way" --title "Preview deploy for ${CHANGE_ID}" --alias "deploy-preview-${CHANGE_ID}" -d ./public')
}
}
}
}