-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile.example
157 lines (140 loc) · 5.28 KB
/
Jenkinsfile.example
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
#!groovy
pipeline {
agent {
label 'hamsterswap'
}
post {
always {
cleanWs()
deleteDir()
}
cleanup {
script {
sh '''
docker rmi -f "${REGISTRY_NAME}:${CURRENT_VERSION}-${GIT_BRANCH}"
docker rmi -f "test-${REGISTRY_NAME}:${CURRENT_VERSION}-${GIT_BRANCH}"
docker system prune --volumes -f
'''
}
}
}
environment {
// app env
// Add something here
// build info env
GIT_BRANCH = "${GIT_BRANCH.split("/")[1]}"
CURRENT_VERSION = sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()
// credential id
REGISTRY_CREDENTIAL_ID = 'ecr:ap-southeast-1:aws-credentials'
GITLAB_PUSH_SECRET = credentials('uid-gitlab-webhook-secret')
REGISTRY_URL = credentials('registry-url-backend')
// dokku deployment credential
REGISTRY_NAME = 'a8-uid-backend'
DOKKU_DEV_REMOTE = credentials('dokku-dev-remote-backend')
DOKKU_PROD_REMOTE = credentials('dokku-prod-remote-backend')
SSH_PRIVATE_KEY = credentials('uid-dokku-deployment-private-key')
}
stages {
stage('setup-parameters') {
steps {
gitlabCommitStatus('setup-parameters') {
script {
properties([
disableConcurrentBuilds([
abortPrevious: true
]),
parameters([
booleanParam(
defaultValue: false,
description: 'Trigger a dokku deployment.',
name: 'DOKKU_DEPLOY'
),
booleanParam(
defaultValue: false,
description: 'Trigger a dockerfile build and publish to ecr repository.',
name: 'BUILD_AND_PUBLISH_IMAGE'
),
])
])
}
}
}
}
stage('build-info') {
steps {
gitlabCommitStatus('build-info') {
echo 'Current branch: ' + env.GIT_BRANCH
echo 'Current version: ' + env.CURRENT_VERSION
}
}
}
stage('test') {
steps {
gitlabCommitStatus('test') {
script {
def image = docker.build("test-${env.REGISTRY_NAME}:${env.CURRENT_VERSION}-${env.GIT_BRANCH}", "-f Dockerfile.test ./")
image.inside {
sh 'yarn install'
sh 'yarn test'
sh 'yarn test:e2e'
}
}
}
}
}
stage('publish-image') {
when {
expression {
params.BUILD_AND_PUBLISH_IMAGE == true && (env.GIT_BRANCH == 'develop' || env.GIT_BRANCH == 'main')
}
}
steps {
gitlabCommitStatus('publish-image') {
script {
docker.withRegistry(env.REGISTRY_URL, env.REGISTRY_CREDENTIAL_ID) {
def image = docker.build("${env.REGISTRY_NAME}:${env.CURRENT_VERSION}-${env.GIT_BRANCH}", "./")
// push the image to registry
image.push()
}
}
}
}
}
stage('deploy') {
agent {
docker {
image 'dokku/ci-docker-image'
args '-v $PWD:/app'
reuseNode true
}
}
when {
expression {
params.DOKKU_DEPLOY == true && (env.GIT_BRANCH == 'develop' || env.GIT_BRANCH == 'main')
}
}
steps {
gitlabCommitStatus('deploy') {
sh 'echo "Deploying to ${GIT_BRANCH} environment ..."'
sh 'rm -rf .husky/'
script {
if (env.GIT_BRANCH == 'develop') {
sh '''
set +x
GIT_REMOTE_URL=${DOKKU_DEV_REMOTE} SSH_PRIVATE_KEY=$(cat ${SSH_PRIVATE_KEY}) dokku-deploy
set -x
'''
}
if (env.GIT_BRANCH == 'main') {
sh '''
set +x
GIT_REMOTE_URL=${DOKKU_PROD_REMOTE} SSH_PRIVATE_KEY=$(cat ${SSH_PRIVATE_KEY}) dokku-deploy
set -x
'''
}
}
}
}
}
}
}