forked from IBM/ibm-cloud-devops
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Scripted-Jenkinsfile-With-OTCNotifications
136 lines (120 loc) · 7.12 KB
/
Scripted-Jenkinsfile-With-OTCNotifications
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
#!groovy
node {
git url: 'https://github.com/xunrongl/DemoDRA-1.git'
tool name: 'recent', type: 'nodejs'
withEnv([
// You need to specify 3 required environment variables and your bluemix credentials first, they are going to be used for the following IBM Cloud DevOps steps
'IBM_CLOUD_DEVOPS_ORG=dlatest',
'IBM_CLOUD_DEVOPS_APP_NAME=Weather-V1-Scripted',
'IBM_CLOUD_DEVOPS_TOOLCHAIN_ID=1320cec1-daaa-4b63-bf06-7001364865d2'
'IBM_CLOUD_DEVOPS_WEBHOOK_URL=WEBHOOK_URL_PLACEHOLDER'
]) {
//specify your bluemix credentials, please use "IBM_CLOUD_DEVOPS_CREDS_USR" for usernameVariable, "IBM_CLOUD_DEVOPS_CREDS_PSW" for passwordVariable
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'BM_CRED',
usernameVariable: 'IBM_CLOUD_DEVOPS_CREDS_USR', passwordVariable: 'IBM_CLOUD_DEVOPS_CREDS_PSW']]) {
// work around to get the git commit id
def gitCommit = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
stage('Build') {
withEnv(["GIT_COMMIT=${gitCommit}",
'GIT_BRANCH=master',
"GIT_REPO=GIT_REPO_URL_PLACEHOLDER"]) {
try {
sh 'npm install'
sh 'grunt dev-setup --no-color'
// use "publishBuildRecord" method to publish build record
publishBuildRecord gitBranch: "${GIT_BRANCH}", gitCommit: "${GIT_COMMIT}", gitRepo: "${GIT_REPO}", result:"SUCCESS"
}
catch (Exception e) {
publishBuildRecord gitBranch: "${GIT_BRANCH}", gitCommit: "${GIT_COMMIT}", gitRepo: "${GIT_REPO}", result:"FAIL"
}
}
stage('Unit Test and Code Coverage') {
sh 'grunt dev-test-cov --no-color -f'
// use "publishTestResult" method to publish test result
publishTestResult type:'unittest', fileLocation: './mochatest.json'
publishTestResult type:'code', fileLocation: './tests/coverage/reports/coverage-summary.json'
}
stage('Deploy to Staging') {
try {
// 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}')
'''
// use "publishDeployRecord" method to publish deploy record
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"
}
catch (Exception e) {
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') {
withEnv(["APP_URL=http://staging-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net"]) {
sh 'grunt fvt-test --no-color -f'
}
// use "publishTestResult" method to publish test result
publishTestResult type:'fvt', fileLocation: './mochafvt.json', environment: 'STAGING'
}
stage('Gate') {
// use "evaluateGate" method to leverage IBM Cloud DevOps gate
evaluateGate policy: 'Weather App Policy', forceDecision: 'true'
}
stage('Deploy to Prod') {
try {
// 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}')
'''
// use "publishDeployRecord" method to publish deploy record
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"
}
catch(Exception e) {
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"
}
}
}
}
}
}
/*
void withDevOpsEnv(List envVars = [], def body) {
List jobEnv = [
'IBM_CLOUD_DEVOPS_APP_NAME=Weather-V1-Xunrong-Scripted',
'IBM_CLOUD_DEVOPS_TOOLCHAIN_ID=1320cec1-daaa-4b63-bf06-7001364865d2'
]
// Add any additional environment variables.
jobEnv.addAll(envVars)
// Invoke the body closure we're passed within the environment we've created.
withEnv(jobEnv) {
body.call()
}
}
*/