-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile-linux
109 lines (95 loc) · 3.3 KB
/
Jenkinsfile-linux
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
node {
try {
notifySlack()
stage('Create userAuth') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'my-APIGEE-Creds',
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
env.USERNAME="$USERNAME"
env.PASSWORD="$PASSWORD"
env.AUTH_PASS="$USERNAME:$PASSWORD"
sh "echo ${env.USERNAME}"
}
}
stage('Checkout repo and clean it') {
mvnHome = tool 'maven2'
sh "echo ${env.WORKSPACE}"
git(
url: 'https://github.com/satish1240/apigee-cicd.git',
credentialsId: 'GITToken',
branch: 'master'
)
sh "mvn clean -f cicd-api"
}
stage('Policy-Code Analysis') {
// Run the maven build
env.NODEJS_HOME = "${tool 'nodejs'}"
env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
sh 'npm --version'
sh "apigeelint -s cicd-api/apiproxy/ -f table.js"
}
stage('Promotion') {
timeout(time: 2, unit: 'DAYS') {
input 'Do you want to Approve?'
}
}
stage('Get current prod revision') {
def currRev = sh(script: '''
AUTH="$ae_username:$ae_password";
depl_info=$(curl -u $AUTH "https://api.enterprise.apigee.com/v1/organizations/$ae_org/environments/prod/apis/cicd-api/deployments");
currntRev=$(jq -r .revision[0].name <<< "${depl_info}");
echo $currntRev
''',returnStdout: true).split()
env.currRev = "${currRev[-1]}"
}
stage('Deploy to Production') {
// Run the maven build
sh "mvn -f cicd-api/ install -P prod -D username=${env.USERNAME} -D password=${env.PASSWORD} -D org=$ae_org"
}
try {
stage('Integration Tests') {
// Install cucumber and run apickli tests to generate reports
sh """
cd ${env.WORKSPACE}/cicd-api/test
if [[ ! -d node_modules/cucumber ]]; then npm install; fi
cd ${env.WORKSPACE}/cicd-api/test/node_modules/cucumber/bin
./cucumber-js --format json:${env.WORKSPACE}/reports.json ${env.WORKSPACE}/cicd-api/test/features
"""
}
} catch (e) {
//if tests fail, used maven to clean which undeploy, deletes the deployed revision and shell script which deploys previous revision
sh """
chmod 755 ${env.WORKSPACE}/undeploy.sh
mvn -f cicd-api/ install -P prod -D username=${env.USERNAME} -D password=${env.PASSWORD} -D org=$ae_org -D options=clean
${env.WORKSPACE}/undeploy.sh ${env.AUTH_PASS} $ae_org cicd-api ${env.currRev}
"""
// sh "cat ${env.WORKSPACE}/../pom.xml | awk -F'[<>]' '/artifactId/{print $3}' | sed -n 2p
throw e
} finally {
// generate cucumber reports in both Test Pass/Fail scenario
// to generate reports, cucumber plugin searches for an *.json file in Workspace by default
cucumber fileIncludePattern: 'reports.json'
}
}
catch (e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
notifySlack(currentBuild.result)
}
}
def notifySlack(String buildStatus = 'STARTED') {
// Build status of null means success.
buildStatus = buildStatus ?: 'SUCCESS'
def color
if (buildStatus == 'STARTED') {
color = '#636363'
} else if (buildStatus == 'SUCCESS') {
color = '#47ec05'
} else if (buildStatus == 'UNSTABLE') {
color = '#d5ee0d'
} else {
color = '#ec2805'
}
def msg = "${buildStatus}: `${env.JOB_NAME}` #${env.BUILD_NUMBER}:\n${env.BUILD_URL}"
slackSend(color: color, message: msg)
}