-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
91 lines (86 loc) · 1.88 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
def appName = 'birthday-app'
def projectId = ''
def imageTag = ''
pipeline {
agent {
kubernetes {
label 'birthday-app'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
component: ci
spec:
serviceAccountName: cd-jenkins
containers:
- name: node
image: node:12-alpine
command:
- cat
tty: true
- name: gcloud
image: gcr.io/cloud-builders/gcloud
command:
- cat
tty: true
"""
}
}
stages {
stage('Get project variables') {
steps {
container('gcloud') {
script {
projectId = sh(script: "gcloud config get-value project", returnStdout: true).trim()
imageTag = "eu.gcr.io/${projectId}/${appName}:${env.BUILD_NUMBER}"
}
}
}
}
stage('Run unit tests') {
steps {
container('node') {
sh 'cd app && npm install'
sh 'cd app && npm test'
}
}
}
stage('Build and push image to Container Registry using Cloud Build') {
when {
branch 'master'
}
steps {
container('gcloud') {
sh "gcloud builds submit --tag ${imageTag} ./app"
}
}
}
stage('Deploy Canary') {
when {
branch 'master'
}
steps {
container('gcloud') {
sh "sed -i 's|DOCKER_IMAGE_PATH|'${imageTag}'|g' kube-manifests/app-canary.yml"
sh "kubectl apply -f kube-manifests/app-canary.yml"
}
}
}
stage('Deploy Production') {
when {
branch 'master'
}
steps {
input 'Deploy to Production?'
milestone(1)
container('gcloud') {
sh "sed -i 's|DOCKER_IMAGE_PATH|'${imageTag}'|g' kube-manifests/app.yml"
sh "kubectl apply -f kube-manifests/app.yml"
sh "kubectl delete -f kube-manifests/app-canary.yml"
}
}
}
}
}