-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathJenkinsfile
170 lines (145 loc) · 8.29 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
node {
environment {
APP_NAME = "SampleNodeJs"
STAGING = "Staging"
PRODUCTION = "Production"
}
stage('Checkout') {
// Checkout our application source code
git url: 'https://github.com/dynatrace-innovationlab/jenkins-dynatrace-pipeline-tutorial.git', credentialsId: 'cd41a86f-ea57-4477-9b10-7f9277e650e1', branch: 'master'
// into a dynatrace-cli subdirectory we checkout the CLI
dir ('dynatrace-cli') {
git url: 'https://github.com/Dynatrace/dynatrace-cli.git', credentialsId: 'cd41a86f-ea57-4477-9b10-7f9277e650e1', branch: 'master'
}
}
stage('Build') {
// Lets build our docker image
dir ('sample-nodejs-service') {
def app = docker.build("sample-nodejs-service:${BUILD_NUMBER}")
}
}
stage('CleanStaging') {
// The cleanup script makes sure no previous docker staging containers run
dir ('sample-nodejs-service') {
sh "./cleanup.sh SampleNodeJsStaging"
}
}
stage('DeployStaging') {
// Lets deploy the previously build container
def app = docker.image("sample-nodejs-service:${BUILD_NUMBER}")
app.run("--name SampleNodeJsStaging -p 80:80 " +
"-e 'DT_CLUSTER_ID=SampleNodeJsStaging' " +
"-e 'DT_TAGS=Environment=Staging Service=Sample-NodeJs-Service' " +
"-e 'DT_CUSTOM_PROP=ENVIRONMENT=Staging JOB_NAME=${JOB_NAME} " +
"BUILD_TAG=${BUILD_TAG} BUILD_NUMBER=${BUIlD_NUMBER}'")
dir ('dynatrace-scripts') {
// push a deployment event on the host with the tag [AWS]Environment:JenkinsTutorial
sh './pushdeployment.sh HOST AWS Environment JenkinsTutorial ' +
'${BUILD_TAG} ${BUILD_NUMBER} ${JOB_NAME} ' +
'Jenkins ${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
// now I push one on the actual service (it has the tags from our rules)
sh './pushdeployment.sh SERVICE CONTEXTLESS DockerService SampleNodeJsStaging ' +
'${BUILD_TAG} ${BUILD_NUMBER} ${JOB_NAME} ' +
'Jenkins ${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
}
}
stage('Testing') {
// lets push an event to dynatrace that indicates that we START a load test
dir ('dynatrace-scripts') {
sh './pushevent.sh SERVICE CONTEXTLESS DockerService SampleNodeJsStaging ' +
'"STARTING Load Test" ${JOB_NAME} "Starting a Load Test as part of the Testing stage"' +
' ${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
}
// lets run some test scripts
dir ('sample-nodejs-service-tests') {
// start load test and run for 120 seconds - simulating traffic for Staging enviornment on port 80
sh "rm -f stagingloadtest.log stagingloadtestcontrol.txt"
sh "./loadtest.sh 80 stagingloadtest.log stagingloadtestcontrol.txt 120 Staging"
archiveArtifacts artifacts: 'stagingloadtest.log', fingerprint: true
}
// lets push an event to dynatrace that indicates that we STOP a load test
dir ('dynatrace-scripts') {
sh './pushevent.sh SERVICE CONTEXTLESS DockerService SampleNodeJsStaging '+
'"STOPPING Load Test" ${JOB_NAME} "Stopping a Load Test as part of the Testing stage" '+
'${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
}
}
stage('ValidateStaging') {
// lets see if Dynatrace AI found problems -> if so - we can stop the pipeline!
dir ('dynatrace-scripts') {
DYNATRACE_PROBLEM_COUNT = sh (script: './checkforproblems.sh', returnStatus : true)
echo "Dynatrace Problems Found: ${DYNATRACE_PROBLEM_COUNT}"
}
// now lets generate a report using our CLI and lets generate some direct links back to dynatrace
dir ('dynatrace-cli') {
sh 'python3 dtcli.py dqlr srv tags/CONTEXTLESS:DockerService=SampleNodeJsStaging '+
'service.responsetime[avg%hour],service.responsetime[p90%hour] ${DT_URL} ${DT_TOKEN}'
sh 'mv dqlreport.html dqlstagingreport.html'
archiveArtifacts artifacts: 'dqlstagingreport.html', fingerprint: true
// get the link to the service's dashboard and make it an artifact
sh 'python3 dtcli.py link srv tags/CONTEXTLESS:DockerService=SampleNodeJsStaging '+
'overview 60:0 ${DT_URL} ${DT_TOKEN} > dtstagelinks.txt'
archiveArtifacts artifacts: 'dtstagelinks.txt', fingerprint: true
}
}
stage('DeployProduction') {
// first we clean production
dir ('sample-nodejs-service') {
sh "./cleanup.sh SampleNodeJsProduction"
}
// now we deploy the new container
def app = docker.image("sample-nodejs-service:${BUILD_NUMBER}")
app.run("--name SampleNodeJsProduction -p 90:80 "+
"-e 'DT_CLUSTER_ID=SampleNodeJsProduction' "+
"-e 'DT_TAGS=Environment=Production Service=Sample-NodeJs-Service' "+
"-e 'DT_CUSTOM_PROP=ENVIRONMENT=Production JOB_NAME=${JOB_NAME} "+
"BUILD_TAG=${BUILD_TAG} BUILD_NUMBER=${BUIlD_NUMBER}'")
dir ('dynatrace-scripts') {
// push a deployment event on the host with the tag [AWS]Environment:JenkinsTutorial
sh './pushdeployment.sh HOST AWS Environment JenkinsTutorial '+
'${BUILD_TAG} ${BUILD_NUMBER} ${JOB_NAME} Jenkins '+
'${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
// now I push one on the actual service (it has the tags from our rules)
sh './pushdeployment.sh SERVICE CONTEXTLESS DockerService SampleNodeJsProduction '+
'${BUILD_TAG} ${BUILD_NUMBER} ${JOB_NAME} Jenkins '+
'${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
}
}
stage('WarmUpProduction') {
// lets push an event to dynatrace that indicates that we START a load test
dir ('dynatrace-scripts') {
sh './pushevent.sh SERVICE CONTEXTLESS DockerService SampleNodeJsProduction '+
'"STARTING Load Test" ${JOB_NAME} "Starting a Load Test to warm up new prod deployment" '+
'${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
}
// lets run some test scripts
dir ('sample-nodejs-service-tests') {
// start load test and run for 120 seconds - simulating traffic for Production enviornment on port 90
sh "rm -f productionloadtest.log productionloadtestcontrol.txt"
sh "./loadtest.sh 90 productionloadtest.log productionloadtestcontrol.txt 60 Production"
archiveArtifacts artifacts: 'productionloadtest.log', fingerprint: true
}
// lets push an event to dynatrace that indicates that we STOP a load test
dir ('dynatrace-scripts') {
sh './pushevent.sh SERVICE CONTEXTLESS DockerService SampleNodeJsProduction '+
'"STOPPING Load Test" ${JOB_NAME} "Stopping a Load Test as part of the Production warm up phase" '+
'${JENKINS_URL} ${JOB_URL} ${BUILD_URL} ${GIT_COMMIT}'
}
}
stage('ValidateProduction') {
dir ('dynatrace-scripts') {
DYNATRACE_PROBLEM_COUNT = sh (script: './checkforproblems.sh', returnStatus : true)
echo "Dynatrace Problems Found: ${DYNATRACE_PROBLEM_COUNT}"
}
// now lets generate a report using our CLI and lets generate some direct links back to dynatrace
dir ('dynatrace-cli') {
sh 'python3 dtcli.py dqlr srv tags/CONTEXTLESS:DockerService=SampleNodeJsProduction '+
'service.responsetime[avg%hour],service.responsetime[p90%hour] ${DT_URL} ${DT_TOKEN}'
sh 'mv dqlreport.html dqlproductionreport.html'
archiveArtifacts artifacts: 'dqlproductionreport.html', fingerprint: true
// sh 'python3 dtcli.py link srv tags/CONTEXTLESS:DockerService=SampleNodeJsProduction ' +
// ' overview 60:0 ${DT_URL} ${DT_TOKEN} > dtprodlinks.txt'
// archiveArtifacts artifacts: 'dtprodlinks.txt', fingerprint: true
}
}
}