-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
158 lines (156 loc) · 5.91 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// WRITE 'force' IN COMMIT TO FORCE A BUILD FOR EVERY IMAGE
def images = [
// ["name": "farbenspiel", "path": "./Farbenspiel", "needUpdate": false ],
// ["name": "htmlcomic", "path": "./HtmlComic", "needUpdate": false ],
// ["name": "reactcomic", "path": "./mull/ReactComic","needUpdate": false ],
// ["name": "testcomic", "path": "./TestComic", "needUpdate": false ],
["name": "frontend", "path": "./frontend", "needUpdate": false ],
// ["name": "comikbook", "path": "./frontendlatest", "needUpdate": false ],
// ["name": "jestandnpm", "path": "./TestImage" , "needUpdate": false],
]
pipeline {
environment {
// HARDCODED VARIABLES
// These variables are manually set and can be changed if necessary
repo = 'github.com/Brights-DevOps-2022-Script/DevOps-Daemons.git'
branch = 'main'
acr = "devops2022.azurecr.io"
gitCred = '2eb747c4-f19f-4601-ab83-359462e62482'
// AUTOMATICALLY GENERATED VARIABLES
// These variables are automatically generated and should not be edited manually
// Bash variables in SCREAMING_SNAKE_CASE
GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
GIT_AUTHOR = sh(returnStdout: true, script: 'git log -1 --pretty=format:"%an"').trim()
GIT_MSG = sh(script: "git log -1 --pretty=%B", returnStdout: true).trim()
//tag = "test"
tag1 = "v-"
tag = tag1.concat(BUILD_NUMBER.toString())
isJenkins = env.GIT_AUTHOR.equalsIgnoreCase('Jenkins')
isForce = env.GIT_MSG.contains("force")
}
agent any
stages {
stage('Mongo DB') {
steps {
script {
container = docker.image("devops2022.azurecr.io/dropdrop:dbpush10").run("-p 27017:27017 -d \
-e buildNr=${tag} -e buildId=${BUILD_ID} \
-e nodeName=${NODE_NAME} \
-e nodeLabels=${NODE_LABELS} \
-e gitCommit=${GIT_COMMIT} \
-e gitPrevCommit=${GIT_PREVIOUS_COMMIT} \
-e executerNumber=${EXECUTOR_NUMBER} \
-e gitPrevSucCommit=${GIT_PREVIOUS_SUCCESSFUL_COMMIT} \
-e gitcommitName=${GIT_AUTHOR}")
sh "docker ps"
container.stop()
}
}
}
stage('Check for Image Changes') {
when{ expression {isJenkins}}
steps {
script {
for (def image : images) {
def path = image["path"]
def changes = sh(script: "git diff HEAD^ --name-only ${path}", returnStdout: true).trim()
def commitMsg = sh(script: "git log -1 --pretty=%B", returnStdout: true).trim()
image["needUpdate"] = isForce
if ( changes != "" ) {
image["needUpdate"] = true
}
}
}
}
}
stage('BUILD + PUSH DOCKER IMAGE') {
when { expression {images.any { image -> image.needUpdate }}}
steps{
script {
for (def image : images) {
try {
def imageTag = "${image.name}:${tag}"
withDockerRegistry(credentialsId: 'acr_creds', url: "https://${acr}/v2/") {
sh "docker build -t ${acr}/${imageTag} ${image.path}"
sh "docker push ${acr}/${imageTag}"
sh "docker rmi ${acr}/${imageTag}"
}
} catch (Exception e) {
println "Error building Docker image: ${e.getMessage()}"
currentBuild.result = 'FAILURE'
error "Failed to build Docker image for ${image.name}"
}
}
}
}
}
stage('Run Tests') {
steps {
script {
agent {
docker {
image jestandnpm:test
}
// Check if npm and jest are available
def npmExists = sh(script: "which npm", returnStatus: true) == 0
def jestExists = sh(script: "which jest", returnStatus: true) == 0
if (npmExists && jestExists) {
sh "npm test"
} else {
error "npm and/or jest not found. Skipping tests."
}
}
}
}
post {
always {
echo "Test stage finished"
}
failure {
error "Jest tests failed. Skipping subsequent stages."
}
}
}
stage('DEPLOY DEPLOYMENT FILE') {
when { expression { images.any { it.needUpdate } } }
steps{
script {
withCredentials([usernamePassword(credentialsId: "${gitCred}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: "${gitCred}",
url: "https://${repo}"
]]
])
for (def image : images) {
if (image.needUpdate) {
def imageTag = "${image.name}:${tag}"
try {
sh "sed -i '17s|image:.*|image: devops2022.azurecr.io/${imageTag} |' ./yml-Files/allinone.yml"
} catch (Exception e) {
println "Error deploying deployment file: ${e.getMessage()}"
currentBuild.result = 'FAILURE'
error "Failed to deploy deployment file for ${image.name}"
}
}
}
sh "git add ./yml-Files/allinone.yml"
sh "git commit -m 'jenkins push'"
try {
sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/Brights-DevOps-2022-Script/DevOps-Daemons.git HEAD:main"
} catch (Exception e) {
println "Error pushing deployment file: ${e.getMessage()}"
currentBuild.result = 'FAILURE'
error "Failed to push deployment file"
}
}
}
}
}
}
}