forked from pandacloud1/DevopsProject1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile-CI
119 lines (108 loc) · 4.29 KB
/
Jenkinsfile-CI
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
// JENKINS CI PIPELINE/
// Purpose: The Code will be built into executable file (.jar) & pushed to Dockerhub
// NOTE:
// i. Store Dockerhub, EC2 pem key credentials in Jenkins Server
// ii. Install 'ssh agent' plugin in Jenkins
pipeline {
agent any
// DECLARE THE VARIABLES HERE:
environment {
DOCKER_USERNAME = "nasirpatel" // check the 'ID' in your Jenkins credentials
}
stages {
stage("1. Cleanup") {
// Clean workspace directory for the current build
steps {
deleteDir ()
}
}
stage ('2. Git Checkout') {
// use pipeline syntax generator to generate below step
// 'Pipeline syntax' --> Steps 'Smaple step' --> git (enter url & branch & generate)
steps {
dir ("DevopsProject1"){
script {
git branch: 'main', url: 'https://github.com/pandacloud1/DevopsProject1.git'
}
}
}
}
stage("3. Maven Unit Test") {
// Test the individual units of code
steps{
dir ("DevopsProject1"){
sh 'mvn test'
}
}
}
stage('4. Maven Build') {
// Build the application into an executable file (.jar)
steps{
dir ("DevopsProject1"){
sh 'mvn clean install'
}
}
}
stage("5. Maven Integration Test") {
// Test the interaction between different units of code
steps{
dir ("DevopsProject1"){
sh 'mvn verify'
}
}
}
stage('6. Docker Image Build') {
// Build Docker Image
steps{
dir('DevopsProject1') { // go to directory where 'Dockerfile' is stored
script {
def JOB = env.JOB_NAME.toLowerCase() // Convert Jenkins Job name to lower-case
sh "docker build -t ${JOB}:${BUILD_NUMBER} ." // 'JOB_NAME' & 'BUILD_NUMBER' are Jenkins Global variable
}
}
}
}
stage('7. Docker Image Tag') {
// Rename the Docker Image before pushing to Dockerhub
steps{
dir('DevopsProject1') { // go to directory where Docker Image is created
script {
def JOB = env.JOB_NAME.toLowerCase() // Convert Jenkins Job name to lower-case
sh "docker tag ${JOB}:${BUILD_NUMBER} ${DOCKER_USERNAME}/${JOB}:v${BUILD_NUMBER}"
sh "docker tag ${JOB}:${BUILD_NUMBER} ${DOCKER_USERNAME}/${JOB}:latest"
}
}
}
}
stage('8. Trivy Image Scan') {
// Scan Docker images for vulnerabilities
steps{
script {
def JOB = env.JOB_NAME.toLowerCase() // Convert Jenkins Job name to lower-case
sh "trivy image ${DOCKER_USERNAME}/${JOB}:v${BUILD_NUMBER} > scan.txt"
}
}
}
stage('9. Docker Image Push') {
// Login to Dockerhub & Push the image to Dockerhub
steps{
script {
withCredentials([usernamePassword(credentialsId: 'my_dockerhub_creds', usernameVariable: 'docker_user', passwordVariable: 'docker_pass')]) {
sh "docker login -u '${docker_user}' -p '${docker_pass}'"
def JOB = env.JOB_NAME.toLowerCase() // Convert Jenkins Job name to lower-case
sh "docker push ${DOCKER_USERNAME}/${JOB}:v${BUILD_NUMBER}"
sh "docker push ${DOCKER_USERNAME}/${JOB}:latest"
}
}
}
}
stage('10. Docker Image Cleanup') {
// Remove the unwanted (dangling) images created in Jenkins Server to free-up space
steps{
script {
sh "docker image prune -af"
}
}
}
}
}