Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (138 sloc) 6.86 KB
#!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 {
IBM_CLOUD_DEVOPS_CREDS_USR = 'niklas_heidloff@de.ibm.com'
IBM_CLOUD_DEVOPS_CREDS_PSW = 'xxxxx'
IBM_CLOUD_DEVOPS_ORG = 'niklas_heidloff@de.ibm.com'
IBM_CLOUD_DEVOPS_APP_NAME = 'Weather-V1-Niklas'
IBM_CLOUD_DEVOPS_TOOLCHAIN_ID = 'b85795c7-6819-42e2-83a2-b5d37feb0d11'
IBM_CLOUD_DEVOPS_WEBHOOK_URL= 'https://jenkins:12f7e43e-c477-448f-b313-b5491f2d42ef:51ed36d0-98a2-42a8-8c83-d09b2d148c6f@devops-api.ng.bluemix.net/v1/toolint/messaging/webhook/publish'
}
tools {
nodejs 'recent'
}
stages {
stage('Build') {
environment {
GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
GIT_BRANCH = 'master'
GIT_REPO = 'https://github.com/nheidloff/DemoDRA-1.git'
}
steps {
sh 'export PATH=$PATH:/tmp/cf-dl'
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 {
// post build section to use "publishBuildRecord" method to publish build record
publishBuildRecord gitBranch: "${GIT_BRANCH}", gitCommit: "${GIT_COMMIT}", gitRepo: "${GIT_REPO}", result:"SUCCESS"
notifyOTC stageName: "Build", status: "SUCCESS"
}
failure {
publishBuildRecord gitBranch: "${GIT_BRANCH}", gitCommit: "${GIT_COMMIT}", gitRepo: "${GIT_REPO}", result:"FAIL"
notifyOTC stageName: "Build", status: "FAILURE"
}
}
}
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 {
// post build section to 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') {
steps {
// Push the Weather App to Bluemix, staging space
sh '''
echo "CF Login..."
/tmp/cf-dl/cf api https://api.ng.bluemix.net
/tmp/cf-dl/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"
/tmp/cf-dl/cf delete $CF_APP_NAME -f
/tmp/cf-dl/cf push $CF_APP_NAME -n $CF_APP_NAME -m 64M -i 1
export APP_URL=http://$(/tmp/cf-dl/cf app $CF_APP_NAME | grep urls: | awk '{print $2}')
'''
}
// post build section to use "publishDeployRecord" method to publish deploy record
post {
success {
// post build section to use "publishDeployRecord" method to publish deploy record
publishDeployRecord environment: "STAGING", appUrl: "http://staging-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"SUCCESS"
notifyOTC stageName: "Deploy to Staing", status: "SUCCESS"
}
failure {
publishDeployRecord environment: "STAGING", appUrl: "http://staging-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"FAIL"
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 {
// post build section to use "publishTestResult" method to publish test result
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..."
/tmp/cf-dl/cf api https://api.ng.bluemix.net
/tmp/cf-dl/cf login -u $IBM_CLOUD_DEVOPS_CREDS_USR -p $IBM_CLOUD_DEVOPS_CREDS_PSW -o $IBM_CLOUD_DEVOPS_ORG -s prod
echo "Deploying...."
export CF_APP_NAME="prod-$IBM_CLOUD_DEVOPS_APP_NAME"
/tmp/cf-dl/cf delete $CF_APP_NAME -f
/tmp/cf-dl/cf push $CF_APP_NAME -n $CF_APP_NAME -m 64M -i 1
export APP_URL=http://$(/tmp/cf-dl/cf app $CF_APP_NAME | grep urls: | awk '{print $2}')
'''
}
// post build section to use "publishDeployRecord" method to publish deploy record
post {
success {
// post build section to use "publishDeployRecord" method to publish deploy record
publishDeployRecord environment: "PRODUCTION", appUrl: "http://prod-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"SUCCESS"
notifyOTC stageName: "Deploy to prod", status: "SUCCESS"
}
failure {
publishDeployRecord environment: "PRODUCTION", appUrl: "http://prod-${IBM_CLOUD_DEVOPS_APP_NAME}.mybluemix.net", result:"FAIL"
notifyOTC stageName: "Deploy to prod", status: "FAILURE"
}
}
}
}
}