-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
184 lines (172 loc) · 5.89 KB
/
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
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
175
176
177
178
179
180
181
182
183
184
//def version, mvnCmd = "mvn -s configuration/cicd-settings-nexus3.xml"
import groovy.json.JsonSlurper
def mvnCmd = "mvn"
def version="1.0"
def app_name="my-sb-war"
def quay_host="docker.io"
def quay_org="vizuri"
def nextVersionFromGit(scope) {
def latestVersion = sh returnStdout: true, script: 'git describe --tags "$(git rev-list --tags=*.*.* --max-count=1 2> /dev/null)" 2> /dev/null || echo 0.0.0'
echo "latestVersion: ${latestVersion}"
def (major, minor, patch) = latestVersion.tokenize('.').collect { it.toInteger() }
def nextVersion
switch (scope) {
case 'major':
nextVersion = "${major + 1}.0.0"
break
case 'minor':
nextVersion = "${major}.${minor + 1}.0"
break
case 'patch':
nextVersion = "${major}.${minor}.${patch + 1}"
break
}
nextVersion
}
pipeline {
agent {
label 'maven-buildah'
}
stages {
stage("Checkout") {
steps {
sshagent (credentials: ['github-jenins']) {
sh """
echo '->> In Checkout <<-'
echo "Branch Name ${BRANCH_NAME}"
git checkout ${BRANCH_NAME}
git fetch --tags
echo '->> Done Checkout <<-'
"""
}
}
}
stage("Gather Deployment Parameters") {
steps {
timeout(time: 30, unit: 'SECONDS') {
script {
// Show the select input modal
def INPUT_PARAMS = input message: 'Please Provide Parameters', ok: 'Next',
parameters: [
choice(name: 'ENVIRONMENT', choices: ['dev','test', 'perf', 'prod'].join('\n'), description: 'Please select the Environment'),
choice(name: 'RELEASE_SCOPE', choices: ['major','minor', 'patch'].join('\n'), description: 'Release Scope'),
booleanParam(name: 'UNINSTALL', defaultValue: false, description: 'Perform Uninstall')]
env.ENVIRONMENT = INPUT_PARAMS.ENVIRONMENT
env.RELEASE_SCOPE = INPUT_PARAMS.RELEASE_SCOPE
env.UNINSTALL = INPUT_PARAMS.UNINSTALL
echo "UNINSTALL: ${UNINSTALL}"
}
}
}
}
stage('Build App') {
steps {
//script {
// def pom = readMavenPom file: 'pom.xml'
// version = pom.version
//}
script {
if(ENVIRONMENT == 'prod') {
version = nextVersionFromGit(RELEASE_SCOPE)
sh "mvn versions:set -DnewVersion=${version}"
echo "release_number: ${version}"
}
else {
sh (
script: "${mvnCmd} -B help:evaluate -Dexpression=project.version | grep -e '^[^\\[]' > release.txt",
returnStdout: true,
returnStatus: false
)
version = readFile('release.txt').trim()
echo "release_number: ${version}"
}
}
sh "${mvnCmd} install -DskipTests=true"
}
}
stage('Test') {
steps {
sh "${mvnCmd} test"
// step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
stage('Build Container') {
steps {
container("buildah") {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'rh-credentials',
usernameVariable: 'RH_USERNAME', passwordVariable: 'RH_PASSWORD'], [$class: 'UsernamePasswordMultiBinding', credentialsId: 'quay-credentials',
usernameVariable: 'QUAY_USERNAME', passwordVariable: 'QUAY_PASSWORD']]) {
sh """
echo '->> In Buildah ${app_name}-${version} <<-'
buildah login -u $RH_USERNAME -p $RH_PASSWORD registry.redhat.io
buildah login -u $QUAY_USERNAME -p $QUAY_PASSWORD ${quay_host}
buildah bud -t ${quay_host}/${quay_org}/${app_name}:${version} .
buildah push ${quay_host}/${quay_org}/${app_name}:${version}
echo '->> Done Buildah <<-'
"""
}
}
}
}
stage('Helm Package') {
when {
expression { ENVIRONMENT != 'prod' }
}
steps {
container("buildah") {
sh """
echo '->> In Helm Package <<-'
helm package src/main/helm/ --version=${version} --app-version=${version}
echo '->> Done Helm Package <<-'
"""
}
}
}
stage('Uninstall') {
when {
expression { UNINSTALL == 'true' }
}
steps {
container("buildah") {
sh """
echo '->> In Uninstall <<-'
#helm uninstall ${ENVIRONMENT}-${app_name} --namespace=default
sleep 20
echo '->> Done Uninstall <<-'
"""
}
}
}
stage('Deploy') {
when {
expression { ENVIRONMENT != 'prod' }
}
steps {
container("buildah") {
sh """
echo '->> In Helm Install DEV ${app_name}-${version} <<-'
helm upgrade --install --set env=${ENVIRONMENT} ${ENVIRONMENT}-${app_name} ${app_name}-${version}.tgz --namespace=default
echo '->> Done Helm Install <<-'
"""
}
}
}
stage('Tag') {
when {
expression { ENVIRONMENT == 'prod' }
}
steps {
sshagent (credentials: ['github-jenins']) {
sh """
echo '->> In Tag <<-'
git config --global user.email 'jenkins@upenn.edu'
git config --global user.name 'Jenkins'
git tag -a ${version} -m 'Jenkins Tag'
git push --tags
echo '->> Done Tag <<-'
"""
}
}
}
}
}