-
Notifications
You must be signed in to change notification settings - Fork 167
/
Jenkinsfile
128 lines (109 loc) · 4 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
#!groovy
// FULL_BUILD -> true/false build parameter to define if we need to run the entire stack for lab purpose only
final FULL_BUILD = params.FULL_BUILD
// HOST_PROVISION -> server to run ansible based on provision/inventory.ini
final HOST_PROVISION = params.HOST_PROVISION
final GIT_URL = 'https://github.com/ricardozanini/soccer-stats.git'
final NEXUS_URL = 'nexus.local:8081'
stage('Build') {
node {
git GIT_URL
withEnv(["PATH+MAVEN=${tool 'm3'}/bin"]) {
if(FULL_BUILD) {
def pom = readMavenPom file: 'pom.xml'
sh "mvn -B versions:set -DnewVersion=${pom.version}-${BUILD_NUMBER}"
sh "mvn -B -Dmaven.test.skip=true clean package"
stash name: "artifact", includes: "target/soccer-stats-*.war"
}
}
}
}
if(FULL_BUILD) {
stage('Unit Tests') {
node {
withEnv(["PATH+MAVEN=${tool 'm3'}/bin"]) {
sh "mvn -B clean test"
stash name: "unit_tests", includes: "target/surefire-reports/**"
}
}
}
}
if(FULL_BUILD) {
stage('Integration Tests') {
node {
withEnv(["PATH+MAVEN=${tool 'm3'}/bin"]) {
sh "mvn -B clean verify -Dsurefire.skip=true"
stash name: 'it_tests', includes: 'target/failsafe-reports/**'
}
}
}
}
if(FULL_BUILD) {
stage('Static Analysis') {
node {
withEnv(["PATH+MAVEN=${tool 'm3'}/bin"]) {
withSonarQubeEnv('sonar'){
unstash 'it_tests'
unstash 'unit_tests'
sh 'mvn sonar:sonar -DskipTests'
}
}
}
}
}
if(FULL_BUILD) {
stage('Approval') {
timeout(time:3, unit:'DAYS') {
input 'Do I have your approval for deployment?'
}
}
}
if(FULL_BUILD) {
stage('Artifact Upload') {
node {
unstash 'artifact'
def pom = readMavenPom file: 'pom.xml'
def file = "${pom.artifactId}-${pom.version}"
def jar = "target/${file}.war"
sh "cp pom.xml ${file}.pom"
nexusArtifactUploader artifacts: [
[artifactId: "${pom.artifactId}", classifier: '', file: "target/${file}.war", type: 'war'],
[artifactId: "${pom.artifactId}", classifier: '', file: "${file}.pom", type: 'pom']
],
credentialsId: 'nexus',
groupId: "${pom.groupId}",
nexusUrl: NEXUS_URL,
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'ansible-meetup',
version: "${pom.version}"
}
}
}
stage('Deploy') {
node {
def pom = readMavenPom file: "pom.xml"
def repoPath = "${pom.groupId}".replace(".", "/") +
"/${pom.artifactId}"
def version = pom.version
if(!FULL_BUILD) { //takes the last version from repo
sh "curl -o metadata.xml -s http://${NEXUS_URL}/repository/ansible-meetup/${repoPath}/maven-metadata.xml"
version = sh script: 'xmllint metadata.xml --xpath "string(//latest)"',
returnStdout: true
}
def artifactUrl = "http://${NEXUS_URL}/repository/ansible-meetup/${repoPath}/${version}/${pom.artifactId}-${version}.war"
withEnv(["ARTIFACT_URL=${artifactUrl}", "APP_NAME=${pom.artifactId}"]) {
echo "The URL is ${env.ARTIFACT_URL} and the app name is ${env.APP_NAME}"
// install galaxy roles
sh "ansible-galaxy install -vvv -r provision/requirements.yml -p provision/roles/"
ansiblePlaybook colorized: true,
credentialsId: 'ssh-jenkins',
limit: "${HOST_PROVISION}",
installation: 'ansible',
inventory: 'provision/inventory.ini',
playbook: 'provision/playbook.yml',
sudo: true,
sudoUser: 'jenkins'
}
}
}