forked from Yass525/DEVOPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins_pipeline_groovy.txt
128 lines (121 loc) · 4.82 KB
/
jenkins_pipeline_groovy.txt
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
pipeline{
environment {
registry = "yassine525/jenkins_test"
registryCredential = 'dockerhub_id'
dockerImage = ''
NEXUS_VERSION = "nexus3"
// This can be http or https
NEXUS_PROTOCOL = "http"
// Where your Nexus is running. 'nexus-3' is defined in the docker-compose file
NEXUS_URL = "192.168.0.10:8081"
// Repository where we will upload the artifact
NEXUS_REPOSITORY = "maven-releases"
// Jenkins credential id to authenticate to Nexus OSS
NEXUS_CREDENTIAL_ID = "cee7c60f-0cb7-4635-a53f-cf4ba6810c41"
}
agent any
stages{
stage('Cloning the project from git'){
steps{
git('https://github.com/Yass525/DEVOPS.git')
}
}
stage('Test') {
steps{
sh 'mvn test'
}
}
stage('SonarQube Analysis'){
steps{
script {
scannerHome = tool 'sonarqube'
}
withSonarQubeEnv('sonarqube')
{
sh "${scannerHome}/bin/sonar-scanner \
-D sonar.login=admin \
-D sonar.password=password \
-D sonar.projectKey=sonarqubeTest \
-D sonar.exclusions=vendor/**,resources/**,**/*.java \
-D sonar.host.url=http://192.168.0.10:9000/"
}
}
}
stage('PUBLISH TO NEXUS'){
steps {
script {
// Read POM xml file using 'readMavenPom' step , this step 'readMavenPom' is included in: https://plugins.jenkins.io/pipeline-utility-steps
pom = readMavenPom file: "pom.xml";
// Find built artifact under target folder
filesByGlob = findFiles(glob: "target/*.${pom.packaging}");
// Print some info from the artifact found
echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}"
// Extract the path from the File found
artifactPath = filesByGlob[0].path;
// Assign to a boolean response verifying If the artifact name exists
artifactExists = fileExists artifactPath;
if(artifactExists) {
echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version}";
nexusArtifactUploader(
nexusVersion: NEXUS_VERSION,
protocol: NEXUS_PROTOCOL,
nexusUrl: NEXUS_URL,
groupId: pom.groupId,
version: pom.version,
repository: NEXUS_REPOSITORY,
credentialsId: NEXUS_CREDENTIAL_ID,
artifacts: [
// Artifact generated such as .jar, .ear and .war files.
[artifactId: pom.artifactId,
classifier: '',
file: artifactPath,
type: pom.packaging],
// Lets upload the pom.xml file for additional information for Transitive dependencies
[artifactId: pom.artifactId,
classifier: '',
file: "pom.xml",
type: "pom"]
]
);
} else {
error "*** File: ${artifactPath}, could not be found";
}
}
}
}
stage('BUILDING IMAGE') {
steps {
script {
dockerImage= docker.build registry + ":$BUILD_NUMBER"
}
}
}
stage('DEPLOYING IMAGE') {
steps {
script {
// docker.withRegistry( '', registryCredential)
withDockerRegistry([ credentialsId: "dockerhub_id", url: "" ]){
dockerImage.push()
}
}
}
}
stage('CLEANING UP') {
steps {
sh "docker rmi $registry:$BUILD_NUMBER"
}
}
stage('STARTING DOCKER COMPOSE') {
steps {
sh "/usr/local/bin/docker-compose up -d" ;
sh "/usr/local/bin/docker-compose ps" ;
}
}
}
post{
always{
sh "docker-compose down --remove-orphans -v" ;
sh "docker-compose ps" ;
}
}
}