-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile-master
167 lines (142 loc) · 6.43 KB
/
Jenkinsfile-master
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
#!/usr/bin/env groovy
// When implementing this file for your project, it is recommended that you should
// remove all comments in this file and do some changes as you require for your
//project.
//Required
def validatedeployment(f)
{
env.WORKSPACE = pwd()
boolean valid = false;
def yamlText = readFile "${env.WORKSPACE}/"+f
if(yamlText.contains('resources') && yamlText.contains('requests') && yamlText.contains('limits')){
valid = true;
}
if(!valid)
throw new Exception("Invalid deployment.yaml. Resources section not specified.");
}
try {
node('ubu-slave-1') {
stage('Clean workspace') {
deleteDir()
sh 'ls -lah'
}
stage('Checkout source') {
checkout scm
}
stage ('Validate Compute Resources') {
println "Validating CPU and Memory Settings in Deployment File"
validatedeployment('deployment.yaml');
}
stage('Run Test Case') {
echo 'Running test cases'
echo "Passed test cases"
}
stage ('Build docker image') {
println "Building and packaging python application"
sh 'sleep 5'
def img = docker.build('python-sqs', '.')
}
stage('Publish image') {
echo "Publishing docker images"
sh "\$(aws ecr get-login --region ap-southeast-2)"
// need the following steps below if the token has expired.
sh '''
aws_login=$(aws ecr get-login --region ap-southeast-2)
if echo "$aws_login" | grep -q -E '^docker login -u AWS -p \\S{1092} -e none https://[0-9]{12}.dkr.ecr.\\S+.amazonaws.com$'; then $aws_login; fi
'''
docker.withRegistry('https://077077460384.dkr.ecr.ap-southeast-2.amazonaws.com', 'ecr:ap-southeast-2:AWS-SVC-ECS') {
docker.image('python-sqs').push('latest')
docker.image('python-sqs').push("build-master-${env.BUILD_NUMBER}")
}
}
}
}
catch (exc) {
echo "Caught: ${err}"
String recipient = '[email protected]'
mail subject: "${env.JOB_NAME} (${env.BUILD_NUMBER}) failed",
body: "It appears that ${env.BUILD_URL} is failing, somebody should do something about that",
to: '[email protected]',
replyTo: '[email protected]',
from: '[email protected]'
}
node('ubu-slave-1') {
// send to email
emailext (
subject: "Attention: Deploy to UAT Notification: ${env.JOB_BASE_NAME} - Build # ${env.BUILD_NUMBER}",
body: """
Dear User,
You are receiving this email because you are listed as receipient for ${env.JOB_BASE_NAME} project.
The purpose of this notification is to let you know that ${env.JOB_BASE_NAME} has completed building and pushing docker image to ECR repository.
It needs approval for proceed to deploy to UAT/Production.
Currently, munish.mehta is authorized to deploy to uat and production.
The approval period is set as 5 minutes currently. If job is not approved within specified time, it will not proceed to deploy.
Later if you wish to deploy the image created by this build, you can execute deploy to UAT job.
Build log can be see at: ${env.BUILD_URL}/console
Authorized users, for approving build, click here: ${env.BUILD_URL}/input
Thanks
CI/CD team.
""",
to: '[email protected]',
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}
def imageTag = "077077460384.dkr.ecr.ap-southeast-2.amazonaws.com/python-sqs:build-master-${env.BUILD_NUMBER}"
def userInput = true
def didTimeout = false
long startTime = System.currentTimeMillis()
def approverChoice = ""
def env = ""
try {
timeout(time: 15, unit: 'SECONDS') { // wait for 15 seconds. Projects should customize this settings as per their requirements. Total 3 changes #change 1
approverChoice = input(id: 'approverChoice', message: 'Deploy Now?', ok: 'Deploy', submitter:'munish.mehta', parameters: [
[$class: 'ChoiceParameterDefinition', choices: 'uat\nprod', description: 'Select environment to deploy to', name: 'choice']])
env = approverChoice
}
} catch(err) { // timeout reached or input false
echo "Caught: ${err}"
long timePassed = System.currentTimeMillis() - startTime
if (timePassed >= 15 * 1000) {//timeout #change 2
echo 'Timed out'
echo 'No action is taken within specified 15 seconds. However, the image is build and pushed to ECR repo.' // timeout #change 3
echo 'Aborting the build with SUCCESS status.'
didTimeout = true
} else {
userInput = false
echo "Aborted by: [${user}]"
}
}
if(env) {
stage ('Pull and deploy to cluster') {
node ('ubu-slave-1') {
echo "Pulling and deploying app from ECR"
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'b0097933-cea0-4729-8b7a-1e1f8702299f', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
// copy the kubeconfig file for your cluster to root of application.
if(env == "uat") {
echo 'copying kubeconfig for UAT cluster'
sh 'aws s3 cp s3://isentia-kube-config/uat/kubeconfig .'
} else {
echo 'copying kubeconfig for production cluster'
sh 'aws s3 cp s3://isentia-kube-config/prod/kubeconfig .'
}
// create deployment, service and pods
sh("sed -i.bak -e 's#077077460384.dkr.ecr.ap-southeast-2.amazonaws.com/python-sqs:latest#${imageTag}#' -e 's#dev#${env}#' ./deployment.yaml")
sh('kubectl apply --namespace=templates --kubeconfig=kubeconfig -f deployment.yaml --record')
}
}
}
} else {
echo "Wrong choice of environment variable. Exiting now."
}
node ('ubu-slave-1'){
if (didTimeout) {
currentBuild.result = 'SUCCESS'
echo "no input was received before timeout"
} else if (userInput == true) {
currentBuild.result = 'SUCCESS'
echo "Application getting deployed to ${env} cluster"
} else {
echo "this was not successful"
currentBuild.result = 'FAILURE'
}
}