-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathDeclarative-Jenkinsfile
148 lines (142 loc) · 6.92 KB
/
Declarative-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
#!groovy
/*
This is an sample Jenkins file for the Weather App, which is a node.js application that has unit test, code coverage
and functional verification tests, deploy to staging and production environment and use IBM Cloud DevOps gate.
We use this as an example to use our plugin in the Jenkinsfile
Basically, you need to specify required 4 environment variables and then you will be able to use the 4 different methods
for the build/test/deploy stage and the gate
*/
pipeline {
agent any
environment {
// You need to specify 4 required environment variables first, they are going to be used for the following IBM Cloud DevOps steps
IBM_CLOUD_DEVOPS_CREDS = credentials('BM_CRED')
IBM_CLOUD_DEVOPS_ORG = 'dlatest'
IBM_CLOUD_DEVOPS_APP_NAME = 'Weather-V1'
IBM_CLOUD_DEVOPS_TOOLCHAIN_ID = '1320cec1-daaa-4b63-bf06-7001364865d2'
IBM_CLOUD_DEVOPS_WEBHOOK_URL = 'WEBHOOK_URL_PLACEHOLDER'
}
tools {
nodejs 'recent'
}
stages {
stage('Build') {
environment {
// get git commit from Jenkins
GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
GIT_BRANCH = 'master'
GIT_REPO = 'GIT_REPO_URL_PLACEHOLDER'
}
steps {
checkout scm
sh 'npm --version'
sh 'npm install'
sh 'grunt dev-setup --no-color'
}
// post build section to use "publishBuildRecord" method to publish build record
post {
success {
publishBuildRecord gitBranch: "${GIT_BRANCH}", gitCommit: "${GIT_COMMIT}", gitRepo: "${GIT_REPO}", result:"SUCCESS"
}
failure {
publishBuildRecord gitBranch: "${GIT_BRANCH}", gitCommit: "${GIT_COMMIT}", gitRepo: "${GIT_REPO}", result:"FAIL"
}
}
}
stage('Unit Test and Code Coverage') {
steps {
sh 'grunt dev-test-cov --no-color -f'
}
// post build section to use "publishTestResult" method to publish test result
post {
always {
publishTestResult type:'unittest', fileLocation: './mochatest.json'
publishTestResult type:'code', fileLocation: './tests/coverage/reports/coverage-summary.json'
}
}
}
stage('Deploy to Staging') {
steps {
// Push the Weather App to Bluemix, staging space
sh '''
echo "CF Login..."
cf api https://api.ng.bluemix.net
cf login -u $IBM_CLOUD_DEVOPS_CREDS_USR -p $IBM_CLOUD_DEVOPS_CREDS_PSW -o $IBM_CLOUD_DEVOPS_ORG -s staging
echo "Deploying...."
export CF_APP_NAME="staging-$IBM_CLOUD_DEVOPS_APP_NAME"
cf delete $CF_APP_NAME -f
cf push $CF_APP_NAME -n $CF_APP_NAME -m 64M -i 1
# use "cf icd --create-connection" to enable traceability
cf icd --create-connection $IBM_CLOUD_DEVOPS_WEBHOOK_URL $CF_APP_NAME
export APP_URL=http://$(cf app $CF_APP_NAME | grep urls: | awk '{print $2}')
'''
}
// post build section to use "publishDeployRecord" method to publish deploy record and notify OTC of stage status
post {
success {
publishDeployRecord environment: "STAGING", appUrl: "http://staging-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"SUCCESS"
// use "notifyOTC" method to notify otc of stage status
notifyOTC stageName: "Deploy to Staging", status: "SUCCESS"
sendDeployableMessage status: "SUCCESS"
}
failure {
publishDeployRecord environment: "STAGING", appUrl: "http://staging-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"FAIL"
// use "notifyOTC" method to notify otc of stage status
notifyOTC stageName: "Deploy to Staging", status: "FAILURE"
}
}
}
stage('FVT') {
//set the APP_URL as the environment variable for the fvt
environment {
APP_URL = "http://staging-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net"
}
steps {
sh 'grunt fvt-test --no-color -f'
}
// post build section to use "publishTestResult" method to publish test result
post {
always {
publishTestResult type:'fvt', fileLocation: './mochafvt.json', environment: 'STAGING'
}
}
}
stage('Gate') {
steps {
// use "evaluateGate" method to leverage IBM Cloud DevOps gate
evaluateGate policy: 'Weather App Policy', forceDecision: 'true'
}
}
stage('Deploy to Prod') {
steps {
// Push the Weather App to Bluemix, production space
sh '''
echo "CF Login..."
cf api https://api.ng.bluemix.net
cf login -u $IBM_CLOUD_DEVOPS_CREDS_USR -p $IBM_CLOUD_DEVOPS_CREDS_PSW -o $IBM_CLOUD_DEVOPS_ORG -s production
echo "Deploying...."
export CF_APP_NAME="prod-$IBM_CLOUD_DEVOPS_APP_NAME"
cf delete $CF_APP_NAME -f
cf push $CF_APP_NAME -n $CF_APP_NAME -m 64M -i 1
# use "cf icd --create-connection" to enable traceability
cf icd --create-connection $IBM_CLOUD_DEVOPS_WEBHOOK_URL $CF_APP_NAME
export APP_URL=http://$(cf app $CF_APP_NAME | grep urls: | awk '{print $2}')
'''
}
// post build section to use "publishDeployRecord" method to publish deploy record and notify OTC of stage status
post {
success {
publishDeployRecord environment: "PRODUCTION", appUrl: "http://prod-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"SUCCESS"
// use "notifyOTC" method to notify otc of stage status
notifyOTC stageName: "Deploy to Prod", status: "SUCCESS"
sendDeployableMessage status: "SUCCESS"
}
failure {
publishDeployRecord environment: "PRODUCTION", appUrl: "http://prod-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"FAIL"
// use "notifyOTC" method to notify otc of stage status
notifyOTC stageName: "Deploy to Prod", status: "FAILURE"
}
}
}
}
}