-
Notifications
You must be signed in to change notification settings - Fork 201
/
Jenkinsfile
159 lines (142 loc) · 5.59 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
159
/*
* Copyright (c) 2016-present Sonatype, Inc. All rights reserved.
* Includes the third-party code listed at http://links.sonatype.com/products/nexus/attributions.
* "Sonatype" is a trademark of Sonatype, Inc.
*/
@Library(['private-pipeline-library', 'jenkins-shared']) _
import com.sonatype.jenkins.pipeline.GitHub
import com.sonatype.jenkins.pipeline.OsTools
properties([
parameters([
string(defaultValue: '', description: 'New Nexus Repository Manager Version', name: 'nexus_repository_manager_version')
])
])
node('ubuntu-zion') {
def commitId, commitDate, longVersion, imageId, branch, dockerImages
def organization = 'sonatype',
gitHubRepository = 'docker-nexus',
credentialsId = 'integrations-github-api',
imageName = 'sonatype/nexus',
archiveName = 'docker-nexus',
dockerHubRepository = 'nexus'
GitHub gitHub
try {
stage('Preparation') {
deleteDir()
OsTools.runSafe(this, "docker system prune -a -f")
def checkoutDetails = checkout scm
dockerImages = [
[ dockerFilePath: "${pwd()}/oss/Dockerfile", imageTag: "${imageName}:oss", imageId: "", flavor: "oss" ],
[ dockerFilePath: "${pwd()}/pro/Dockerfile", imageTag: "${imageName}:pro", imageId: "", flavor: "pro" ]
]
branch = checkoutDetails.GIT_BRANCH == 'origin/main' ? 'main' : checkoutDetails.GIT_BRANCH
commitId = checkoutDetails.GIT_COMMIT
OsTools.runSafe(this, 'git config --global user.email sonatype-ci@sonatype.com')
OsTools.runSafe(this, 'git config --global user.name Sonatype CI')
longVersion = readLongVersion()
withGitHubAppToken {
gitHub = new GitHub(this, "${organization}/${gitHubRepository}", "${GITHUB_TOKEN}")
}
}
if (params.nexus_repository_manager_version) {
stage('Update Repository Manager Version') {
OsTools.runSafe(this, "git checkout ${branch}")
dockerImages.each { updateRepositoryManagerVersion(it.dockerFilePath) }
longVersion = params.nexus_repository_manager_version
}
}
stage('Build') {
gitHub.statusUpdate commitId, 'pending', 'build', 'Build is running'
dockerImages.each { image ->
def hash = OsTools.runSafe(this, "docker build --quiet --no-cache --tag ${image.imageTag} -f ${image.dockerFilePath} .")
image.imageId = hash.split(':')[1]
if (currentBuild.result == 'FAILURE') {
gitHub.statusUpdate commitId, 'failure', 'build', 'Build failed'
return
} else {
gitHub.statusUpdate commitId, 'success', 'build', 'Build succeeded'
}
}
}
if (params.nexus_repository_manager_version) {
stage('Commit Repository Manager Version Update') {
def commitMessage = "Update Repository Manager to ${params.nexus_repository_manager_version}."
sonatypeZionGitConfig()
sshagent(credentials: [sonatypeZionCredentialsId()]) {
sh """git add .
git commit -m '${commitMessage}'
git push origin ${branch}
"""
}
}
}
stage('Archive') {
dir('build/target') {
dockerImages.each {
OsTools.runSafe(this, "docker save ${it.imageId} | gzip > ${archiveName}-${it.flavor}.tar.gz")
}
archiveArtifacts artifacts: "${archiveName}-*.tar.gz", onlyIfSuccessful: true
}
}
if (branch != 'main') {
return
}
input 'Push image and tags?'
stage('Push image and tags') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'docker-hub-credentials',
usernameVariable: 'DOCKERHUB_API_USERNAME', passwordVariable: 'DOCKERHUB_API_PASSWORD']]) {
dockerImages.each { image ->
def tags = getTags(image.flavor, longVersion)
tags.each { tag ->
OsTools.runSafe(this, "docker tag ${image.imageId} ${organization}/${dockerHubRepository}:${tag}")
}
}
OsTools.runSafe(this, """
docker login --username ${env.DOCKERHUB_API_USERNAME} --password ${env.DOCKERHUB_API_PASSWORD}
""")
OsTools.runSafe(this, "docker push --all-tags ${organization}/${dockerHubRepository}")
}
}
stage('Push tags') {
def shortVersion = getShortVersion(longVersion)
sonatypeZionGitConfig()
sshagent(credentials: [sonatypeZionCredentialsId()]) {
sh """git tag ${shortVersion}
git push origin ${shortVersion}
"""
}
OsTools.runSafe(this, "git tag -d ${shortVersion}")
}
} finally {
OsTools.runSafe(this, "docker logout")
OsTools.runSafe(this, "docker system prune -a -f")
OsTools.runSafe(this, 'git clean -f && git reset --hard origin/main')
}
}
def readLongVersion() {
def content = readFile 'oss/Dockerfile'
for (line in content.split('\n')) {
if (line.startsWith('ARG NEXUS_VERSION=')) {
return line.substring(18)
}
}
error 'Could not determine version.'
}
def getShortVersion(longVersion) {
return longVersion.split('-')[0]
}
def getTags(flavor, longVersion) {
def shortVersion = getShortVersion(longVersion)
if (flavor == "pro") {
return ["pro", "pro-${shortVersion}", "pro-${longVersion}"]
}
else {
return ["oss", "${shortVersion}", "${longVersion}", "latest"]
}
}
def updateRepositoryManagerVersion(dockerFileLocation) {
def dockerFile = readFile(file: dockerFileLocation)
def versionRegex = /(ARG NEXUS_VERSION=)(\d\.\d{1,3}\.\d{1,3}\-\d{2})/
dockerFile = dockerFile.replaceAll(versionRegex, "\$1${params.nexus_repository_manager_version}")
writeFile(file: dockerFileLocation, text: dockerFile)
}