-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
141 lines (140 loc) · 5.44 KB
/
Jenkinsfile
File metadata and controls
141 lines (140 loc) · 5.44 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
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
pipeline {
environment {
// HARDCODED VARIABLES
// These variables are manually set and can be changed if necessary
repo = 'github.com/Brights-DevOps-2022-Script/team-3-argoTest.git'
branch = '*/main'
acr = "devops2022.azurecr.io"
image = "felixstrauss"
gitCred = '2eb747c4-f19f-4601-ab83-359462e62482'
dockerPath = "./App/Docker/Helloworld"
// dockerPath = "./App/Docker/ColorQuiz"
// dockerPath = "./App/Docker/HtmlComicBook"
// dockerPath = "./App/Docker/ReactComicBook"
// 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(returnStdout: true, script: 'git log -1 --pretty=format:"%s"').trim()
// Groovy variables in camelCase
buildNo = env.BUILD_NUMBER
tag = "${GIT_COMMIT}"
imageTag = "${image}:${tag}"
// conditions
isNewImage = false
isNonBuildRelease = false
isJenkins = env.GIT_AUTHOR.equalsIgnoreCase('Jenkins')
if (GIT_MSG.contains("update") && (GIT_MSG.contains("minor") || GIT_MSG.contains("major") || GIT_MSG.contains("patch"))) {
isNonBuildRelease = true;
}
}
agent any
stages {
stage('print Infos') {
steps {
script {
println "Git Author : ${GIT_AUTHOR}"
println "Git Commit : ${GIT_COMMIT}"
println "Git Message : ${GIT_MSG}"
println "is jenkins : ${isJenkins}"
println "Image tag : ${imageTag}"
println "ACR login Server : ${acr}"
println "Repo : ${repo}"
println "build number : ${buildNO}"
}
}
}
stage('CHECK DOCKER IMAGE TAG') {
when{ expression {isJenkins}}
steps {
sh "chmod +x ./BashScripts/checkDockerImageTag.sh"
def result = sh(script: "./BashScripts/checkDockerImageTag.sh ${GIT_USERNAME} ${GIT_PASSWORD} 'Build' ${buildNO}",
returnStdout: true, returnStatus: true)
tag = ${result.stdout}
isNewImage = result.status
imageTag = "${image}:${tag}"
println "Script output: ${imageTag}"
println "app has changed: ${isNewImage}"
}
}
stage('Reset build number') {
when{ expression {isNonBuildRelease}}
steps {
script {
// resets the Jenkin controller build number to 1
def resetBuildNumber() {
def jobName = env.JOB_NAME
def buildNumber = env.BUILD_NUMBER
def crumb = sh(script: "curl '$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'", returnStdout: true).trim()
def response = sh(script: "curl -X POST -H '$crumb' -d 'json={\"buildNumber\":\"$buildNumber\",
\"reset\":true}' '$JENKINS_URL/job/$jobName/doResetBuildNumber'", returnStdout: true).trim()
if (response == "") { println("Build number reset successfully") }
if (response != "") { println("Failed to reset build number: $response") }
}
env.BUILD_NUMBER = "1"
resetBuildNumber()
}
}
}
stage('BUILD + PUSH DOCKER IMAGE') {
when{ expression {isNewImage}}
steps {
withDockerRegistry(credentialsId: 'acr_creds', url: "https://${acr}/v2/") {
sh "docker build -t ${acr}/${imageTag} ${dockerPath}"
sh "docker push ${acr}/${imageTag}"
sh "docker rmi ${acr}/${imageTag}"
}
}
}
stage('DEPLOY DEPLOYMENT FILE') {
when{ expression {isNewImage}}
steps {
checkout(
[$class: 'GitSCM',
branches: [[name: ${branch}]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: ${gitCred},
url: "https://${repo}"
]]
]
)
withCredentials([usernamePassword(credentialsId: 'devopsProjectTocken', passwordVariable: 'GIT_PASSWORD',
usernameVariable: 'GIT_USERNAME')]) {
// TO DO
sh "ls"
sh "ls ./BashScripts"
sh "cat ./BashScripts/deployFile1.sh"
// TO DO END
sh "chmod +x ./BashScripts/deployFile1.sh"
sh ('./BaseScripts/deployFile1.sh ${GIT_USERNAME} ${GIT_PASSWORD} ${imageTag} ${acr} ${repo}')
}
}
}
stage('DEPLOY DEPLOYMENT FILE2') {
when{ expression {isNewImage}}
steps {
checkout(
[
$class: 'GitSCM',
branches: [[name: ${branch}]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: ${gitCred},
url: "https://${repo}"
]]
]
)
withCredentials([usernamePassword(credentialsId: 'devopsProjectTocken', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh "chmod +x ./BashScripts/deployFile2.sh"
sh ('./BashScripts/deployFile2.sh ${GIT_USERNAME} ${GIT_PASSWORD} ${imageTag} ${acr} ${repo}')
}
}
}
}
}