forked from hexlo/terraria-server-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
155 lines (141 loc) · 6.32 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
pipeline {
environment {
userName = "hexlo"
imageName = "terraria-server-docker"
// Set buildVersion to manually change the server version. Leaving empty will default to 'latest'
buildVersion = 'latest'
tag = "${buildVersion ? buildVersion : 'latest'}"
gitRepo = "https://github.com/${userName}/${imageName}.git"
gitBranch = "main"
dockerhubRegistry = "${userName}/${imageName}"
githubRegistry = "ghcr.io/${userName}/${imageName}"
arch=''
dockerhubCredentials = 'DOCKERHUB_TOKEN'
githubCredentials = 'GITHUB_TOKEN'
dockerhubImage = ''
dockerhubImageLatest = ''
githubImage = ''
serverVersion = ''
versionTag = ''
}
agent any
triggers {
cron('H * * * *')
}
stages {
stage('Cloning Git') {
environment { HOME = "${env.WORKSPACE}" }
steps {
git branch: "${gitBranch}", credentialsId: 'GITHUB_TOKEN', url: "${gitRepo}"
}
}
stage('Getting Latest Version') {
environment { HOME = "${env.WORKSPACE}" }
steps {
script {
echo "tag=${tag}"
if (tag == 'latest') {
latestVersion = sh(script: "${WORKSPACE}/.scripts/get-terraria-version.sh", returnStdout: true).trim()
versionTag = sh(script: "echo $latestVersion | sed 's/[0-9]/&./g;s/\\.\$//'", returnStdout:true).trim()
}
else {
versionTag = sh(script: "echo $buildVersion | sed 's/[0-9]/&./g;s/\\.\$//'", returnStdout:true).trim()
}
echo "versionTag=${versionTag}"
echo "buildVersion=${buildVersion}"
}
}
}
stage('Creating buildx builder') {
environment { HOME = "${env.WORKSPACE}" }
steps {
script {
catchError(buildResult: 'SUCCESS', stageResult: 'SUCCESS') {
sh "docker buildx create --name multiarch --use"
}
}
}
}
stage('Building amd64 image') {
environment { HOME = "${env.WORKSPACE}" }
steps{
script {
arch='amd64'
"Building ${dockerhubRegistry}-${arch}"
// Docker Hub
// dockerhubImage = docker.build( "${dockerhubRegistry}-${arch}:${tag}", "--target build-amd64 --platform linux/amd64 --no-cache --build-arg VERSION=${buildVersion} ." )
// dockerhubImage = docker.build( "${dockerhubRegistry}-${arch}:${versionTag}", "--target build-amd64 --platform linux/amd64 --no-cache --build-arg VERSION=${buildVersion} ." )
sh "docker buildx use multiarch"
sh "docker buildx build --builder multiarch --target build-${arch} --no-cache --progress plain --platform linux/${arch} -t ${dockerhubRegistry}-${arch}:${tag} --load ."
sh "docker buildx build --builder multiarch --target build-${arch} --no-cache --progress plain --platform linux/${arch} -t ${dockerhubRegistry}-${arch}:${versionTag} --load ."
// Github
// githubImage = docker.build( "${githubRegistry}-${arch}:${tag}", "--target build-amd64 --platform linux/amd64 --no-cache --build-arg VERSION=${buildVersion} ." )
}
}
}
stage('Building arm64 image') {
environment { HOME = "${env.WORKSPACE}" }
steps{
script {
arch='arm64'
echo "Building ${dockerhubRegistry}-${arch}"
// Docker Hub
sh "docker buildx use multiarch"
sh "docker buildx build --builder multiarch --target build-${arch} --no-cache --progress plain --platform linux/${arch} -t ${dockerhubRegistry}-${arch}:${tag} --load ."
sh "docker buildx build --builder multiarch --target build-${arch} --no-cache --progress plain --platform linux/${arch} -t ${dockerhubRegistry}-${arch}:${versionTag} --load ."
// Github
// githubImage = docker.build( "${githubRegistry}-${arch}:${tag}", "--target build-arm64 --platform linux/arm64 --no-cache --build-arg VERSION=${buildVersion} ." )
}
}
}
stage('Deploy Image') {
environment { HOME = "${env.WORKSPACE}" }
steps{
script {
// Docker Hub
docker.withRegistry( '', "${dockerhubCredentials}" ) {
// Push individual images for them to be available to the manifest
sh "docker push ${dockerhubRegistry}-amd64:${tag}"
sh "docker push ${dockerhubRegistry}-arm64:${tag}"
sh "docker push ${dockerhubRegistry}-amd64:${versionTag}"
sh "docker push ${dockerhubRegistry}-arm64:${versionTag}"
// Better way using imagetools. Need testing
// docker buildx imagetools create --progress plain hexlo/terraria-server-docker-amd64:latest hexlo/terraria-server-docker-arm64 --tag hexlo/terraria-server-docker:latest
echo "creating manifest"
sh "docker manifest create --amend ${dockerhubRegistry}:${tag} ${dockerhubRegistry}-amd64:${tag} ${dockerhubRegistry}-arm64:${tag}"
sh "docker manifest push ${dockerhubRegistry}:${tag}"
sh "docker manifest create --amend ${dockerhubRegistry}:${versionTag} ${dockerhubRegistry}-amd64:${versionTag} ${dockerhubRegistry}-arm64:${versionTag}"
sh "docker manifest push ${dockerhubRegistry}:${versionTag}"
}
// Github
// docker.withRegistry("https://${githubRegistry}", "${githubCredentials}" ) {
// githubImage.push("${tag}")
// githubImage.push("${versionTag}")
// }
}
}
}
}
post {
always {
// Cleanup of docker images and volumes
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
// Docker Hub
sh "docker rmi -f ${dockerhubRegistry}:${tag}"
sh "docker rmi -f ${dockerhubRegistry}:${versionTag}"
sh "docker rmi -f ${dockerhubRegistry}-amd64:${tag}"
sh "docker rmi -f ${dockerhubRegistry}-arm64:${tag}"
// Github
sh "docker rmi -f ${githubRegistry}:${tag}"
sh "docker rmi -f ${githubRegistry}:${versionTag}"
// Global
sh "docker system prune --all --force --volumes"
}
}
failure {
mail bcc: '', body: "<b>Jenkins Build Report</b><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} \
<br>Build URL: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', \
subject: "Jenkins Build Failed: ${env.JOB_NAME}", to: "jenkins@runx.io";
}
}
}