-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-CI
129 lines (119 loc) · 4.7 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
120
121
122
123
124
125
126
127
128
129
pipeline {
agent any
environment {
DOCKER_USERNAME = "pramithamj"
}
stages {
stage("1. Cleanup") {
steps {
deleteDir()
}
}
stage('2. Git Checkout') {
steps {
script {
git branch: 'main', url: 'https://github.com/PramithaMJ/PFT-CICD.git'
}
}
}
stage('Node Build Backend') {
steps {
dir("Backend") {
sh '''
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
nvm install 14
nvm use 14
npm install
'''
}
}
}
stage('Node Build Frontend') {
steps {
dir("frontend") {
sh '''
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
nvm install 14
nvm use 14
npm install
'''
}
}
}
stage("Integration Test") {
steps {
dir("Backend") {
echo 'Integration Test for DevopsProject1'
}
}
}
stage('Docker Image Build') {
steps {
dir('Backend') {
script {
def JOB = env.JOB_NAME.toLowerCase()
sh "docker build -t ${DOCKER_USERNAME}/pft-ci-backend:${BUILD_NUMBER} ."
}
}
dir('frontend') {
script {
def JOB = env.JOB_NAME.toLowerCase()
sh "docker build -t ${DOCKER_USERNAME}/pft-ci-frontend:${BUILD_NUMBER} ."
}
}
}
}
stage('Docker Image Tag') {
steps {
dir('Backend') {
script {
def JOB = env.JOB_NAME.toLowerCase()
sh "docker tag ${DOCKER_USERNAME}/pft-ci-backend:${BUILD_NUMBER} ${DOCKER_USERNAME}/pft-ci-backend:v${BUILD_NUMBER}"
sh "docker tag ${DOCKER_USERNAME}/pft-ci-backend:${BUILD_NUMBER} ${DOCKER_USERNAME}/pft-ci-backend:latest"
}
}
dir('frontend') {
script {
def JOB = env.JOB_NAME.toLowerCase()
sh "docker tag ${DOCKER_USERNAME}/pft-ci-frontend:${BUILD_NUMBER} ${DOCKER_USERNAME}/pft-ci-frontend:v${BUILD_NUMBER}"
sh "docker tag ${DOCKER_USERNAME}/pft-ci-frontend:${BUILD_NUMBER} ${DOCKER_USERNAME}/pft-ci-frontend:latest"
}
}
}
}
stage('Trivy Image Scan') {
steps {
script {
def JOB = env.JOB_NAME.toLowerCase()
sh "trivy image ${DOCKER_USERNAME}/pft-ci-backend:v${BUILD_NUMBER} > backend_scan.txt"
sh "trivy image ${DOCKER_USERNAME}/pft-ci-frontend:v${BUILD_NUMBER} > frontend_scan.txt"
}
}
}
stage('Docker Image Push') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'hub_creds', usernameVariable: 'docker_user', passwordVariable: 'docker_pass')]) {
sh "docker login -u '${docker_user}' -p '${docker_pass}'"
sh "docker push ${DOCKER_USERNAME}/pft-ci-backend:v${BUILD_NUMBER}"
sh "docker push ${DOCKER_USERNAME}/pft-ci-backend:latest"
sh "docker push ${DOCKER_USERNAME}/pft-ci-frontend:v${BUILD_NUMBER}"
sh "docker push ${DOCKER_USERNAME}/pft-ci-frontend:latest"
}
}
}
}
stage('Cleanup Local Images') {
steps {
sh "docker rmi ${DOCKER_USERNAME}/pft-ci-backend:v${BUILD_NUMBER}"
sh "docker rmi ${DOCKER_USERNAME}/pft-ci-backend:latest"
sh "docker rmi ${DOCKER_USERNAME}/pft-ci-frontend:v${BUILD_NUMBER}"
sh "docker rmi ${DOCKER_USERNAME}/pft-ci-frontend:latest"
}
}
}
}