-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
192 lines (174 loc) · 6.37 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
node {
def app
def branch_tag
def project_uuid = UUID.randomUUID().toString()
stage('Fetch') {
checkout scm
}
stage('Build') {
slackSend(
color: 'good',
message: "Started build of doorbot server [build #${env.BUILD_NUMBER}, branch ${env.BRANCH_NAME}] (<${env.BUILD_URL}|Open>)"
)
// Create cookie session key
// Note that this means existing sessions will be invalidated
// with each release
// Based on https://shunchaowang.wordpress.com/2015/03/10/generate-random-string-with-groovy/
// But modified with a secure random generator
def random = new java.security.SecureRandom()
def pool = [ 'a'..'f', 0..9 ].flatten()
def randomChars = (1..64).collect {
pool[random.nextInt(pool.size())]
}
def session_key = randomChars.join("")
try {
app = docker.build( "doorbot" )
app.inside {
// Build config file
sh 'rm -f config.yml'
withCredentials([
string(
credentialsId: 'rfid-dev-db-password'
,variable: 'RFID_DB_PASSWORD'
),
string(
credentialsId: 'mms-password'
,variable: 'MMS_PASSWORD'
)
]) {
def example_conf = 'config.yml.example'
def conf = readYaml file: example_conf
conf.postgresql.passwd = RFID_DB_PASSWORD
conf.postgresql.username = 'doorbot'
conf.postgresql.database = 'doorbot'
conf.postgresql.host = 'host.docker.internal'
conf.postgresql.port = 5432
conf.memberpress.user = '[email protected]'
conf.memberpress.passwd = MMS_PASSWORD
conf.oauth.expires_days = 180
conf.oauth.token_hex_length = 64
conf.session.key = session_key
conf.build_id = env.BUILD_ID
conf.build_branch = env.BRANCH_NAME
conf.build_date = env.BUILD_TIMESTAMP
writeYaml file: 'config.yml', data: conf
}
}
}
catch( err ) {
slackSend(
color: '#ff0000',
message: "Failed build of doorbot server during Build stage [build #${env.BUILD_NUMBER}, branch ${env.BRANCH_NAME}] (<${env.BUILD_URL}|Open>)"
)
throw err
}
}
stage( 'Test' ) {
try {
app.inside {
dir( "t" ) {
sh 'PYTHONPATH=./:../:${PYTHONPATH} python3 -m nose2'
}
}
}
catch( err ) {
slackSend(
color: '#ff0000',
message: "Failed build of doorbot server during Test stage [build #${env.BUILD_NUMBER}, branch ${env.BRANCH_NAME}] (<${env.BUILD_URL}|Open>)"
)
throw err
}
}
stage( 'Push to Docker Reg' ) {
try {
docker.withRegistry(
'https://docker.shop.thebodgery.org',
,'docker_reg'
) {
branch_tag = "${env.BRANCH_NAME}-latest"
branch_tag_uuid = "${env.BRANCH_NAME}-${project_uuid}"
branch_tag_build = "${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
app.push( branch_tag )
app.push( branch_tag_uuid )
app.push( branch_tag_build )
}
}
catch( err ) {
slackSend(
color: '#ff0000',
message: "Failed build of doorbot server during Push Docker Reg stage [build #${env.BUILD_NUMBER}, branch ${env.BRANCH_NAME}] (<${env.BUILD_URL}|Open>)"
)
throw err
}
}
stage( 'Pull to Environment' ) {
withCredentials([
sshUserPrivateKey(
keyFileVariable: 'SSH_KEY_PATH'
,usernameVariable: 'SSH_USERNAME'
,credentialsId: 'rfid_dev_ssh'
)
,usernamePassword(
usernameVariable: 'DOCKER_USER'
,passwordVariable: 'DOCKER_PASS'
,credentialsId: 'docker_reg'
)
]) {
def remote = [:]
remote.name = "jenkins"
remote.user = SSH_USERNAME
remote.identityFile = SSH_KEY_PATH
remote.host = "10.0.4.164"
remote.allowAnyHosts = true
// Only pull the main-latest branch to dev
try {
sshCommand(
remote: remote
,command: "docker login -u ${DOCKER_USER} -p '${DOCKER_PASS}' https://docker.shop.thebodgery.org && docker pull docker.shop.thebodgery.org/doorbot:main-latest"
)
}
catch( err ) {
slackSend(
color: '#ff0000',
message: "Failed pulling to environment; ${env.BUILD_ID} on ${env.BRANCH_NAME} (${env.BUILD_URL})"
)
throw err
}
}
}
stage( 'Run on Environment' ) {
withCredentials([
sshUserPrivateKey(
keyFileVariable: 'SSH_KEY_PATH'
,usernameVariable: 'SSH_USERNAME'
,credentialsId: 'rfid_dev_ssh'
)
]) {
def remote = [:]
remote.name = "jenkins"
remote.user = SSH_USERNAME
remote.identityFile = SSH_KEY_PATH
remote.host = "10.0.4.164"
remote.allowAnyHosts = true
try {
sshCommand(
remote: remote
,command: "/usr/local/bin/start_docker.sh doorbot:main-latest rfid-dev"
)
}
catch( err ) {
slackSend(
color: '#ff0000',
message: "Failed executing in environment; ${env.BUILD_ID} on ${env.BRANCH_NAME} (${env.BUILD_URL})"
)
throw err
}
}
}
stage( 'Finish' ) {
slackSend(
color: 'good',
message: "Build finished; ${env.BUILD_ID} on ${env.BRANCH_NAME} (${env.BUILD_URL})"
)
}
}