forked from bambora/na-payment-apis-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JenkinsFile
116 lines (101 loc) · 4.99 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
#!groovy
DEPLOY = true
if("$BRANCH_NAME" == 'develop') {
ENV = 'dev-cde'
} else if("$BRANCH_NAME" == 'master') {
ENV = 'prod-cde'
} else {
DEPLOY = false
ENV = 'dev-cde'
}
if ( ENV == 'prod-cde' ) {
credentialsId = 'vic-devops-cd'
slackChannel = 'vic-devops-cd'
} else {
credentialsId = 'vic-devops-cd-dev'
slackChannel = 'vic-devops-cd-dev'
}
REGION = 'ca-central-1'
def ACCOUNTS = ['dev-cde': '409000534468','prod-cde': '399491208114']
ACCOUNT = ACCOUNTS[ENV]
REGISTRY = ACCOUNT + '.dkr.ecr.ca-central-1.amazonaws.com'
timestamps {
node("$ENV") {
try {
stage('build-and-push') {
deleteDir()
checkout scm
dir("server") {
def ret = sh script: "git rev-parse --short HEAD", returnStdout: true
def sha = ret.trim()
def full_sha = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
def timestamp = sh(returnStdout: true, script: 'date "+%Y-%m-%d %H:%M:%S"').trim()
echo "branch is $BRANCH_NAME"
sh """
sed -i 's|HCNARB|$BRANCH_NAME|g' version.json
sed -i 's|NOISIVER|$full_sha|g' version.json
sed -i 's|EMITDLIUB|$timestamp|g' version.json
sed -i 's|REBMUNDLIUB|$env.BUILD_NUMBER|g' version.json
"""
archiveArtifacts 'version.json'
def image = "paymentapidemo-$ENV-$REGION:$sha"
echo "Image: $image"
sh """sed -i'.orig' -e 's/<VERSION>/$sha/g' app/server.py"""
sh """\$(AWS_DEFAULT_REGION=$REGION aws ecr get-login --no-include-email)"""
docker.withRegistry("https://$REGISTRY") {
paymentapidemo = docker.build("$image", ".")
paymentapidemo.push()
paymentapidemo.push('latest')
}
sh """docker rmi $image"""
sh """mv app/server.py.orig app/server.py"""
}
}
stage('safetycheck') {
def pwd = sh(script: "echo \$PWD", returnStdout: true).trim()
sh """docker run --rm -v $pwd:/safety pyupio/safety safety check -r /safety/server/app/requirements.txt --full-report"""
}
if (ENV == 'prod-cde') {
withCredentials([usernamePassword(credentialsId: credentialsId, usernameVariable: 'username', passwordVariable: 'TOKEN')]) {
slackSend color: "good", message: "Prod na-payment-apis-demo - confirm deploy ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}console|Open>)", token: "$TOKEN", channel: slackChannel
}
timeout(time:30, UNITS:'MINUTES') {
input 'Are we okay to deploy?'
}
}
stage('deploy') {
if(DEPLOY) {
def ret = sh script: "git rev-parse --short HEAD", returnStdout: true
def sha = ret.trim()
dir("server") {
sh """export AWS_DEFAULT_REGION=$REGION
sed -i 's|ACCOUNT|$ACCOUNT|g' ecstaskdefinition.aws.json
sed -i 's|VNE|$ENV|g' ecstaskdefinition.aws.json
sed -i 's|NOIGER|$REGION|g' ecstaskdefinition.aws.json
sed -i 's|VERSION|$sha|g' ecstaskdefinition.aws.json
REVISION=\$(aws ecs register-task-definition --family paymentapidemo --cli-input-json file://ecstaskdefinition.aws.json | jq '.taskDefinition | .revision ')
aws ecs update-service --cluster shared-$ENV-$REGION --service paymentapidemo --task-definition paymentapidemo:\$REVISION --desired-count 1"""
}
}
}
currentBuild.result = 'SUCCESS'
} catch (e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
color = ""
message = ""
if(currentBuild.result == 'SUCCESS') {
color = "good"
message = "Build Successful"
}
else {
color = "danger"
message = "Build Failed"
}
withCredentials([usernamePassword(credentialsId: credentialsId, usernameVariable: 'username', passwordVariable: 'TOKEN')]) {
slackSend color: "$color", message: "$message - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}console|Open>)", token: "$TOKEN", channel: slackChannel
}
}
}
}