forked from siamaksade/openshift-jenkins-demo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
JenkinsFile_JuiceShop
174 lines (159 loc) · 6.93 KB
/
JenkinsFile_JuiceShop
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
171
172
173
174
#!/usr/bin/env groovy
pipeline {
agent any
stages {
stage('SCM Get Code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], userRemoteConfigs: [[url: 'https://github.com/craig-br/juice-shop.git']]])
}
}
// Takes too long for demo
// stage('Code Analysis') {
// node {
// def scannerHome = tool 'sonarqube'
// withSonarQubeEnv('sonarqube'){
// // sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=craig-br_juice-shop2 -Dsonar.sources=. -Dsonar.exclusions=node_modules/*/**,test/**/* -Dsonar.tests=test"
// sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=craig-br_juice-shop -Dsonar.sources=. -Dsonar.exclusions=node_modules/*/**,test/**/*,frontend/node_modules/*/**"
// }
// }
// }
stage('Code Analysis') {
steps {
nodejs(nodeJSInstallationName: 'nodejs') {
sh "npm install"
sh "npm run lint"
}
}
}
// Takes too long. need more power captain
// stage('Unit Tests') {
// steps {
// nodejs(nodeJSInstallationName: 'nodejs') {
// sh "npm test"
// }
// }
// }
stage('Build and Tag') {
steps {
nodejs(nodeJSInstallationName: 'nodejs') {
withCredentials([
usernamePassword(credentialsId: 'juiceshop',
passwordVariable: 'GITHUB_TOKEN',
usernameVariable: 'GITHUB_USERNAME')]) {
script {
// Determine version number for next release.
env.pkgVersion = sh (
script: 'git tag --list | sort --version-sort --reverse | head -n1 | cut -d "-" -f1',
returnStdout: true
).trim()
env.newPkgVersion = bumpPatchVersion(pkgVersion)
}
// Configure author for tag and auth credentials for pushing tag to GitHub.
// See https://git-scm.com/docs/git-credential-store.
sh """
git config --replace-all user.name ${env.GITHUB_USERNAME}
git config --replace-all user.email ${env.GITHUB_USERNAME}
git config credential.helper store
echo https://${env.GITHUB_USERNAME}:${env.GITHUB_TOKEN}@github.com >> \$HOME/.git-credentials
"""
// Bump the package version.
sh """
git tag ${newPkgVersion}
npm --no-git-tag-version version ${newPkgVersion}
git checkout master
git add .
git commit -m ${newPkgVersion}
git push origin master
git push https://github.com/craig-br/juice-shop.git ${newPkgVersion}
"""
//sleep 2 // Give GitHub a moment to realize the tag exists
// Install and Create the Dist Package. No time in demo
//sh "npm install --production && grunt package"
}
}
}
}
// Create a release and deploy instances with Tower
stage ('Tower - Publish and Cloud') {
parallel {
stage('Tower - Create Release') {
steps {
ansibleTower(
towerServer: 'JuiceShop Tower',
templateType: 'job',
jobTemplate: 'GitHub Release',
importTowerLogs: true,
removeColor: false,
verbose: true,
extraVars: '''---
pkg_version: $pkgVersion
tag_name: $newPkgVersion
'''
)
}
}
// Create instances with Tower
stage('Tower - Instances') {
steps {
ansibleTower(
towerServer: 'JuiceShop Tower',
templateType: 'workflow',
jobTemplate: 'Cloud Instances Workflow',
importTowerLogs: true,
removeColor: false,
verbose: true,
credential: '',
)
}
}
}
}
// Deploy app and create VS
stage ('Tower - Deploy App and LB') {
parallel {
stage('Tower - Deploy App') {
steps {
ansibleTower(
towerServer: 'JuiceShop Tower',
templateType: 'job',
jobTemplate: 'Deploy JuiceShop App',
importTowerLogs: true,
removeColor: false,
verbose: true,
)
}
}
stage('Tower - Net') {
steps {
ansibleTower(
towerServer: 'JuiceShop Tower',
templateType: 'job',
jobTemplate: 'Create JuiceShop App VS',
importTowerLogs: true,
removeColor: false,
verbose: true,
)
}
}
}
}
}
}
// Increment the minor part of a `MAJOR.MINOR.PATCH` semver version.
String bumpMinorVersion(String version) {
def parts = version.tokenize('.')
if (parts.size() != 3) {
error "${version} is not a valid MAJOR.MINOR.PATCH version"
}
def newMinorVersion = parts[1].toInteger() + 1
return "${parts[0]}.${newMinorVersion}.${parts[2]}"
}
// Increment the patch part of a `MAJOR.MINOR.PATCH` semver version.
String bumpPatchVersion(String version) {
def parts = version.tokenize('.')
if (parts.size() != 3) {
error "${version} is not a valid MAJOR.MINOR.PATCH version"
}
def newPatchVersion = parts[2].toInteger() + 1
return "${parts[0]}.${parts[1]}.${newPatchVersion}"
}