forked from redhat-cop/container-pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.hygieia
156 lines (141 loc) · 5.06 KB
/
Jenkinsfile.hygieia
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
library identifier: "[email protected]",
retriever: modernSCM(
[
$class: "GitSCMSource",
remote: "https://github.com/redhat-cop/pipeline-library.git"
]
)
openshift.withCluster() {
env.NAMESPACE = openshift.project()
env.POM_FILE = env.BUILD_CONTEXT_DIR ? "${env.BUILD_CONTEXT_DIR}/pom.xml" : "pom.xml"
env.APP_NAME = "${JOB_NAME}".replaceAll(/-build.*/, '')
echo "Starting Pipeline for ${APP_NAME}..."
env.BUILD = "${env.NAMESPACE}"
env.DEV = "${APP_NAME}-dev"
env.STAGE = "${APP_NAME}-stage"
env.PROD = "${APP_NAME}-prod"
}
pipeline {
// Use Jenkins Maven slave
// Jenkins will dynamically provision this as OpenShift Pod
// All the stages and steps of this Pipeline will be executed on this Pod
// After Pipeline completes the Pod is killed so every run will have clean
// workspace
agent {
label 'maven'
}
// Pipeline Stages start here
// Requeres at least one stage
stages {
// Checkout source code
// This is required as Pipeline code is originally checkedout to
// Jenkins Master but this will also pull this same code to this slave
stage('Git Checkout') {
steps {
// Turn off Git's SSL cert check, uncomment if needed
// sh 'git config --global http.sslVerify false'
git url: "${APPLICATION_SOURCE_REPO}", branch: "${APPLICATION_SOURCE_REF}"
script {
env.commit = sh(script:"git rev-parse HEAD", returnStdout: true).trim()
env.version = "${env.commit}-${BUILD_NUMBER}"
}
echo "Setting version to ${env.version}"
}
}
// Run Maven build, skipping tests
stage('Build'){
steps {
hygieiaBuildPublishStep buildStatus: 'InProgress'
sh "mvn clean install -DskipTests=true -f ${POM_FILE}"
}
post {
failure {
hygieiaBuildPublishStep buildStatus: 'Failure'
}
aborted {
hygieiaBuildPublishStep buildStatus: 'Aborted'
}
success {
hygieiaBuildPublishStep buildStatus: 'Success'
hygieiaArtifactPublishStep artifactDirectory: './target/', artifactGroup: 'org.test', artifactName: "shift-rest-0.1.0.jar", artifactVersion: "${env.version}"
}
}
}
// Run Maven unit tests
stage('Unit Test'){
steps {
sh "mvn test -f ${POM_FILE}"
}
}
// Build Container Image using the artifacts produced in previous stages
stage('Build Container Image'){
steps {
// Copy the resulting artifacts into common directory
sh """
ls target/*
rm -rf oc-build && mkdir -p oc-build/deployments
for t in \$(echo "jar;war;ear" | tr ";" "\\n"); do
cp -rfv ./target/*.\$t oc-build/deployments/ 2> /dev/null || echo "No \$t files"
done
"""
// Build container image using local Openshift cluster
// Giving all the artifacts to OpenShift Binary Build
// This places your artifacts into right location inside your S2I image
// if the S2I image supports it.
binaryBuild(projectName: env.BUILD, buildConfigName: env.APP_NAME, buildFromPath: "oc-build")
}
}
stage('Promote from Build to Dev') {
steps {
tagImage(sourceImageName: env.APP_NAME, sourceImagePath: env.BUILD, toImagePath: env.DEV)
}
}
stage ('Verify Deployment to Dev') {
steps {
verifyDeployment(projectName: env.DEV, targetApp: env.APP_NAME)
}
post {
success {
hygieiaDeployPublishStep applicationName: "${APP_NAME}", artifactDirectory: './target/', artifactGroup: 'org.test', artifactName: "shift-rest-0.1.0.jar", artifactVersion: "${env.version}", buildStatus: 'Success', environmentName: "${env.DEV}"
}
}
}
stage('Promote from Dev to Stage') {
steps {
tagImage(sourceImageName: env.APP_NAME, sourceImagePath: env.DEV, toImagePath: env.STAGE)
}
}
stage ('Verify Deployment to Stage') {
steps {
verifyDeployment(projectName: env.STAGE, targetApp: env.APP_NAME)
}
post {
success {
hygieiaDeployPublishStep applicationName: "${APP_NAME}", artifactDirectory: './target/', artifactGroup: 'org.test', artifactName: "shift-rest-0.1.0.jar", artifactVersion: "${env.version}", buildStatus: 'Success', environmentName: "${env.STAGE}"
}
}
}
stage('Promotion gate') {
steps {
script {
input message: 'Promote application to Production?'
}
}
}
stage('Promote from Stage to Prod') {
steps {
tagImage(sourceImageName: env.APP_NAME, sourceImagePath: env.STAGE, toImagePath: env.PROD)
}
}
stage ('Verify Deployment to Prod') {
steps {
verifyDeployment(projectName: env.PROD, targetApp: env.APP_NAME)
}
post {
success {
hygieiaDeployPublishStep applicationName: "${APP_NAME}", artifactDirectory: './target/', artifactGroup: 'org.test', artifactName: "shift-rest-0.1.0.jar", artifactVersion: "${env.version}", buildStatus: 'Success', environmentName: "${env.PROD}"
}
}
}
}
}