-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
105 lines (100 loc) · 4.93 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
pipeline {
agent any
environment {
PROJECT = 'sul-dlss/folio-graphql'
GIT_URL = 'https://github.com/sul-dlss/folio-graphql.git'
}
triggers {
// the generic-webhook-trigger plugin is installed on the Jenkins server
GenericTrigger(
genericVariables: [
[$class: 'GenericVariable', key: 'image', value: '$.repository.repo_name', defaultValue: ''],
[$class: 'GenericVariable', key: 'pusher', value: '$.push_data.pusher', defaultValue: ''],
[$class: 'GenericVariable', key: 'imageTag', value: '$.push_data.tag', defaultValue: ''],
],
causeString: 'Docker Hub push event for ${image}:${imageTag} by ${pusher}',
printContributedVariables: true,
// The presence of the token in the webhook push is what triggers this specific build.
// Dockerhub will send a POST request with this token via our brick-city gateway
// See https://hub.docker.com/repository/docker/suldlss/folio-graphql/webhooks (logged in)
token: 'foliogql'
)
}
stages {
stage('Echo Before Stages') {
steps {
echo "git branch: ${env.GIT_BRANCH}"
echo "tag name: ${env.TAG_NAME}"
}
}
stage('Deploy to test server') {
environment {
DEPLOY_ENVIRONMENT = 'folio-test'
}
when {
// 1. Only run on builds trigggered by GenericWebhookTrigger
// 2. Check the docker image tag name, based on a push to the main branch in Github
// 3. Only when the Docker image and Github branch are both "main", in order to avoid duplicate builds
expression { currentBuild.getBuildCauses('org.jenkinsci.plugins.gwt.GenericCause') && imageTag == 'main' && env.GIT_BRANCH == 'main' && !env.TAG_NAME}
}
steps {
echo 'Deploying to test server'
sshagent (['sul-devops-team', 'sul-continuous-deployment']){
sh """#!/bin/bash -l
ssh graphql@sul-folio-graphql-test.stanford.edu \
'docker pull suldlss/folio-graphql:main && \
docker rm -f \$(docker ps -a -q --filter="name=folio-graphql") && \
docker run -d --env-file ./.env -p 4000:4000 --name folio-graphql suldlss/folio-graphql:main'
"""
}
}
post {
always {
build job: '/Continuous Deployment/Slack Deployment Notification', parameters: [
string(name: 'PROJECT', value: env.PROJECT),
string(name: 'DEPLOY_ENVIRONMENT', value: env.DEPLOY_ENVIRONMENT),
string(name: 'TAG_NAME', value: imageTag),
string(name: 'GIT_URL', value: env.GIT_URL),
booleanParam(name: 'SUCCESS', value: currentBuild.resultIsBetterOrEqualTo('SUCCESS')),
string(name: 'RUN_DISPLAY_URL', value: env.RUN_DISPLAY_URL)
]
}
}
}
stage('Deploy to production server') {
environment {
DEPLOY_ENVIRONMENT = 'folio-prod'
}
when {
// 1. Only run on builds trigggered by GenericWebhookTrigger
// 2. Check the docker image tag name, based on a tagged release in Github
// 3. Only run when the Github branch is "main",
// 4. Only run when the Docker image starts with "v"
expression { currentBuild.getBuildCauses('org.jenkinsci.plugins.gwt.GenericCause') && imageTag.startsWith('v') && env.GIT_BRANCH == 'main' && !env.TAG_NAME}
}
steps {
echo 'Deploying to production server'
sshagent (['sul-devops-team', 'sul-continuous-deployment']){
sh """#!/bin/bash -l
ssh graphql@sul-folio-graphql-prod.stanford.edu \
'docker pull suldlss/folio-graphql:${imageTag} && \
docker rm -f \$(docker ps -a -q --filter="name=folio-graphql") && \
docker run -d --env-file ./.env -p 4000:4000 --name folio-graphql suldlss/folio-graphql:${imageTag}'
"""
}
}
post {
always {
build job: '/Continuous Deployment/Slack Deployment Notification', parameters: [
string(name: 'PROJECT', value: env.PROJECT),
string(name: 'TAG_NAME', value: imageTag),
string(name: 'DEPLOY_ENVIRONMENT', value: env.DEPLOY_ENVIRONMENT),
string(name: 'GIT_URL', value: env.GIT_URL),
booleanParam(name: 'SUCCESS', value: currentBuild.resultIsBetterOrEqualTo('SUCCESS')),
string(name: 'RUN_DISPLAY_URL', value: env.RUN_DISPLAY_URL)
]
}
}
}
}
}