-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile_Nexus
More file actions
89 lines (80 loc) · 4.31 KB
/
Jenkinsfile_Nexus
File metadata and controls
89 lines (80 loc) · 4.31 KB
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
pipeline {
agent { label 'docker' }
environment {
projectname = JOB_NAME.toLowerCase()
M2_HOME = "/home/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven"
PATH = "${M2_HOME}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}
stages {
stage ('Pipeline configuration') {
agent none
steps {
script {
def targetPath="target"
def artifactType="jar"
def nexusUrl="nexus:8081"
def nexusVersion="nexus3"
def nexusProtocol="http"
def nexusSnapshotRepo="maven-snapshots"
def pomReader
def version = "${env.BUILD_NUMBER}"
def port=9191
def dockerNetwork="devopscoc-demotoolchain_default"
// used if pipeline-code is directly used in Jenkins
// stage('Git Checkout') {
// git branch: 'master', url: 'https://github.com/philippselle/demo-application/'
// }
stage('Java Build') {
if (!fileExists(file: '/home/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven')) {
sh 'wget https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip -P /home/jenkins/tools/hudson.tasks.Maven_MavenInstallation/ && unzip /home/jenkins/tools/hudson.tasks.Maven_MavenInstallation/apache-maven-3.6.0-bin.zip -d /home/jenkins/tools/hudson.tasks.Maven_MavenInstallation'
sh 'mv /home/jenkins/tools/hudson.tasks.Maven_MavenInstallation/apache-maven-3.6.0 /home/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven && rm /home/jenkins/tools/hudson.tasks.Maven_MavenInstallation/apache-maven-3.6.0-bin.zip'
}
sh 'mvn clean package -Dmaven.test.skip=true'
}
stage('JUnit') {
try {
sh 'mvn test'
} finally {
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
}
}
stage('QA') {
withSonarQubeEnv('sonar') {
sh 'mvn sonar:sonar'
}
}
stage('Sonarqube Quality Gate') {
sleep 20
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
stage('Publish to Nexus') {
pomReader = readMavenPom file: 'pom.xml'
artifactId = pomReader.artifactId
artifactVersion = pomReader.version
artifactGroupId = pomReader.groupId
artifactFile = "${targetPath}/${artifactId}-${artifactVersion}.${artifactType}"
nexusArtifactUploader artifacts: [[artifactId: artifactId, classifier: '', file: artifactFile, type: artifactType]], credentialsId: 'nexus', groupId: artifactGroupId, nexusUrl: nexusUrl, nexusVersion: nexusVersion, protocol: nexusProtocol, repository: nexusSnapshotRepo, version: artifactVersion
}
stage('Build Dockerimage') {
docker.withServer('tcp://socatdockersock:2375') {
def customImage = docker.build("${projectname}:${version}")
}
}
stage('Deploy') {
docker.withServer('tcp://socatdockersock:2375') {
// stop old container, if no container is running, exit with true
sh 'docker stop "$(docker ps -qf name=deploy-${projectname})" || true'
// deploy fresh container
sh """docker run --net ${dockerNetwork} \
--name deploy-${projectname}${version} -d -p ${port}:${port} ${projectname}:${version}"""
}
echo "Application was deployed on http://localhost:${port}"
}
}
}
}
}
}