-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathJenkinsfile-dev
107 lines (99 loc) · 4.82 KB
/
Jenkinsfile-dev
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
pipeline {
environment {
registry = 'mathematicalthinking/vmt'
registryCredential = 'mt-docker-hub-credentials'
dockerImage = ''
deploymentHost = 'mt-test'
githubRepo = 'git@github.com:mathematicalthinking/vmt.git'
githubBranch = 'development'
}
agent any
stages {
stage('Checkout SCM') {
steps {
// notify slack build started
slackSend message:"Build Started - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
// checkout github repo
git branch: "${githubBranch}", credentialsId: 'jenkins-docker-ssh', url: "${githubRepo}"
}
}
stage('Build Docker Image') {
steps {
script {
// set the docker build context to local, assumes contexts have been set on the build server
sh 'docker context use default'
// build the docker image
dockerImage = docker.build(registry + ":dev-build-$BUILD_NUMBER", "-f Dockerfile-dev .")
}
}
}
stage('Push Docker Image to DockerHub') {
steps {
script {
// push docker image to registry
docker.withRegistry('', registryCredential) {
dockerImage.push()
dockerImage.push('dev-latest')
}
// clean up build image
sh "docker rmi $registry:dev-build-$BUILD_NUMBER"
}
}
}
stage('Deploy to test server') {
steps {
script {
// checkout the devops git repo to get the swarm stack file
checkout([
$class: 'GitSCM',
branches: [[name: 'refs/heads/main']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'DevOpsTemp']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'jenkins-docker-ssh', url: 'git@github.com:21pstem/devops.git']]
])
// copy .env files into same dir as stack file
sh 'cp /home/jenkins/app-config/mt/environments/test/.env* DevOpsTemp/mt/'
// use sshagent plugin to ssh to remote server
sshagent(['jenkins-docker-ssh']) {
sh """
# setup the /mtdocker folder on remote host (for manual manipulation of stack)
ssh ${deploymentHost} 'mkdir -p /mtdocker'
scp DevOpsTemp/mt/docker-compose*.yml DevOpsTemp/mt/swarm-monitoring/docker-compose*.yml DevOpsTemp/mt/.env* jenkins@${deploymentHost}:/mtdocker
ssh ${deploymentHost} 'chown -R :docker /mtdocker && chmod -R 770 /mtdocker'
# set the docker remote host context
docker context use ${deploymentHost}
# pull the latest image
docker pull ${registry}:dev-latest
# remove the docker swarm service
docker service rm mt-test_vmt1 mt-test_vmt2 mt-test_vmt3
# redeploy the app service
docker stack deploy --compose-file DevOpsTemp/mt/docker-compose-mt-apps-test.yml mt-test
# wait 10s for deployed services to fully come alive
sleep 10
# restart nginx (clears the cache and reconnects the backend service)
docker service update --force mt-test_nginx
# set the docker build context back to local
docker context use default
"""
}
}
}
}
}
post {
success {
slackSend message:"Build deployed successfully - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
}
failure {
slackSend failOnError:true, message:"Build failed - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
}
// clean workspace after build
always {
cleanWs(cleanWhenNotBuilt: false,
deleteDirs: true,
disableDeferredWipeout: true,
notFailBuild: true)
}
}
}