forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile-dev-build
More file actions
288 lines (273 loc) · 11.2 KB
/
Jenkinsfile-dev-build
File metadata and controls
288 lines (273 loc) · 11.2 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
node("docker-ubuntu20-xlarge") {
properties([
pipelineTriggers([
cron('H 0 * * *')
])
])
try {
stage('Setup Environment and Tools') {
def goRoot = tool name: 'go-1.24.6', type: 'go'
withEnv(["GOROOT=${goRoot}", "PATH+GO=${goRoot}/bin"]) {
withCredentials([string(credentialsId: 'repo21-url', variable: 'REPO21_URL')]) {
echo "${REPO21_URL}"
def repo21Name = "${REPO21_URL}".substring(8, "${REPO21_URL}".length())
env.REPO_NAME_21 = "$repo21Name"
}
def architectures = [
[pkg: 'jfrog-cli-windows-amd64', goos: 'windows', goarch: 'amd64', fileExtension: '.exe', chocoImage: '${REPO_NAME_21}/jfrog-docker/linuturk/mono-choco'],
[pkg: 'jfrog-cli-linux-386', goos: 'linux', goarch: '386', fileExtension: '', debianImage: '${REPO_NAME_21}/jfrog-docker/i386/ubuntu:20.04', debianArch: 'i386'],
[pkg: 'jfrog-cli-linux-amd64', goos: 'linux', goarch: 'amd64', fileExtension: '', debianImage: '${REPO_NAME_21}/jfrog-docker/ubuntu:20.04', debianArch: 'x86_64', rpmImage: 'almalinux:8.10'],
[pkg: 'jfrog-cli-linux-arm64', goos: 'linux', goarch: 'arm64', fileExtension: ''],
[pkg: 'jfrog-cli-linux-arm', goos: 'linux', goarch: 'arm', fileExtension: ''],
[pkg: 'jfrog-cli-mac-386', goos: 'darwin', goarch: 'amd64', fileExtension: ''],
[pkg: 'jfrog-cli-mac-arm64', goos: 'darwin', goarch: 'arm64', fileExtension: ''],
[pkg: 'jfrog-cli-linux-s390x', goos: 'linux', goarch: 's390x', fileExtension: ''],
[pkg: 'jfrog-cli-linux-ppc64', goos: 'linux', goarch: 'ppc64', fileExtension: ''],
[pkg: 'jfrog-cli-linux-ppc64le', goos: 'linux', goarch: 'ppc64le', fileExtension: '']
]
identifier = 'v2'
cliExecutableName = 'jfrog'
devBranch = "master"
devRepo = "https://github.com/jfrog/jfrog-cli.git"
buildStatus = 'SUCCESS'
repo = 'jfrog-cli'
formattedDate = new Date().format('yyyy-MM-dd')
env.CI = true
env.JFROG_CLI_LOG_LEVEL = "DEBUG"
dir('temp') {
sh "cat /etc/lsb-release"
cliWorkspace = pwd()
sh "echo cliWorkspace=$cliWorkspace"
builderDir = "${cliExecutableName}-builder/"
sh "mkdir $builderDir"
builderPath = "${cliWorkspace}/${builderDir}${cliExecutableName}"
jfrogCliRepoDir = "${cliWorkspace}/${repo}/"
zippedBinariesDir = "${jfrogCliRepoDir}zippedBinaries/"
rtDeployRepoName = params.JFROG_RT_DEPLOY_REPO_NAME
rtResolveRepoName = params.JFROG_RT_RESOLVE_REPO_NAME
repoDeployServerId = params.JFROG_CLI_DEPLOY_SERVER_ID
repoResolveServerId = params.JFROG_CLI_RESOLVE_SERVER_ID
jfrogCLITargetRepoName = params.REPO_FOR_CLI_DEV_UPLOAD
project = params.PROJECT
slackChannelName = params.SLACK_CHANNEL_FOR_CLI_DEV_BUILD_NOTIFICATION
watchName = params.WATCH_NAME
buildNumber = env.BUILD_NUMBER
buildName = "jfrog-cli-dev-build"
stage('Clone Source Repo') {
try {
dir("${cliWorkspace}") {
sh "git clone ${devRepo}"
dir("${repo}") {
sh "git checkout ${devBranch}"
}
}
}
catch (e) {
notifyFailure('Clone Source Repo', e)
throw e
}
}
stage('Build JFrog CLI') {
try {
sh 'go version'
dir("$jfrogCliRepoDir") {
sh "build/build.sh $cliExecutableName"
}
sh "mv $jfrogCliRepoDir/$cliExecutableName $builderDir"
version = getCliVersion(builderPath)
print "CLI version: $version"
version = getCLINextVersion(version) + "-beta"
print "Next CLI version: $version"
}
catch (e) {
notifyFailure('Build JFrog CLI', e)
throw e
}
}
stage('Setting JF-Config') {
try {
setJfConfigure()
}
catch (e) {
notifyFailure('Setting JF-Config', e)
throw e
}
}
buildAndUploadBinaryAndBuildInfo(architectures)
}
}
}
} catch (e) {
echo "ERROR: Pipeline failed with exception: ${e}"
buildStatus = 'FAILURE'
throw e
}
finally {
stage('Send Notifications') {
message = ''
if (buildStatus == 'SUCCESS') {
message = "Dev Build Jenkins Pipeline for Jfrog-CLI has successfully completed.\n<${env.BUILD_URL}|View Build>"
slackSend(channel: "#${slackChannelName}", message: message, color: 'good')
} else {
message = """@here Dev Build Jenkins Pipeline has a problem.
*Status* : *${buildStatus}*.
*Build* : <${env.BUILD_URL}|${env.JOB_NAME} #${env.BUILD_NUMBER}>
"""
slackSend(channel: "#${slackChannelName}", message: message, color: 'danger')
}
echo "Final notification message would be: \n${message}"
}
}
}
def notifyFailure(String stageName, error) {
def message = """@here :x: *Build FAILED!*
*Job:* `${env.JOB_NAME}` #${env.BUILD_NUMBER}
*Failed Stage:* `${stageName}`
*Error:* `${error.message.trim()}`
*<${env.BUILD_URL}|Open Build Log>*"""
echo "Sending failure notification for stage: ${stageName}"
slackSend(
channel: "#${slackChannelName}",
color: 'danger',
message: message
)
}
def setJfConfigure() {
sh """#!/bin/bash
$builderPath go-config --repo-deploy ${rtDeployRepoName} --repo-resolve ${rtResolveRepoName} --server-id-deploy ${repoDeployServerId} --server-id-resolve ${repoResolveServerId}
"""
}
def getCLINextVersion(String currentVersion) {
if (currentVersion.startsWith('v')) {
currentVersion = currentVersion.substring(1)
}
def parts = currentVersion.split('\\.')
if (parts.size() != 3) {
error("Invalid version format. Expected MAJOR.MINOR.PATCH, but got: ${currentVersion}")
}
def major = parts[0].toInteger()
def minor = parts[1].toInteger()
def patch = parts[2].toInteger()
minor++
patch = 0
def nextVersion = "${major}.${minor}.${patch}"
return nextVersion
}
def getCliVersion(exePath) {
version = sh(script: "$exePath -v | tr -d 'jfrog version' | tr -d '\n'", returnStdout: true)
return version
}
def buildAndUploadBinaryAndBuildInfo(architectures) {
configRepo21()
try {
sh "mkdir -p '${zippedBinariesDir}'"
makeBuildInfo()
uploadCliBinary(architectures)
uploadAndScanBuildInfo()
} finally {
cleanupRepo21()
}
}
def configRepo21() {
withCredentials([
// jfrog-ignore
usernamePassword(credentialsId: 'repo21', usernameVariable: 'REPO21_USER', passwordVariable: 'REPO21_PASSWORD'),
string(credentialsId: 'repo21-url', variable: 'REPO21_URL')
]) {
sh """#!/bin/bash
$builderPath c add repo21 --url=$REPO21_URL --user=$REPO21_USER --password=$REPO21_PASSWORD --overwrite
$builderPath c use repo21
"""
}
}
def cleanupRepo21() {
sh """#!/bin/bash
$builderPath c rm repo21
"""
}
def uploadCliBinary(architectures) {
for (int i = 0; i < architectures.size(); i++) {
def currentBuild = architectures[i]
try {
stage("Build and upload ${currentBuild.pkg}") {
buildAndUpload(currentBuild.goos, currentBuild.goarch, currentBuild.pkg, currentBuild.fileExtension)
}
}
catch (e) {
notifyFailure('Build and upload ${currentBuild.pkg}', e)
throw e
}
}
}
def uploadBinaryToJfrogRepo21(pkg, fileName) {
sh """#!/bin/bash
set -e
$builderPath rt u ${jfrogCliRepoDir}/binaries/${pkg}/${fileName} ${jfrogCLITargetRepoName}/dev/${formattedDate}/$identifier/$version/$pkg/ --build-name=${buildName} --build-number=${buildNumber} --project=${project} --fail-no-op --flat
echo Uploaded the binary here: ${jfrogCLITargetRepoName}/dev/${formattedDate}/$identifier/$version/$pkg/
"""
}
def makeBuildInfo() {
sh """#!/bin/bash
cd ${jfrogCliRepoDir}
$builderPath go build --build-name="${buildName}" --build-number="${buildNumber}" --project="${project}"
"""
}
def uploadAndScanBuildInfo() {
try {
stage('Upload Build-Info') {
sh """#!/bin/bash
$builderPath rt build-publish "${buildName}" "${buildNumber}" --project=${project}
"""
}
} catch (e) {
notifyFailure('Upload Build-Info', e)
throw e
}
try {
stage('Scanning Build-Info') {
sh """#!/bin/bash
$builderPath build-scan "${buildName}" "${buildNumber}" --project ${project}
"""
}
}
catch (e) {
notifyFailure('Scanning Build-Info', e)
throw e
}
try {
stage("Scanning Binaries under watch ${watchName}"){
dir("${zippedBinariesDir}"){
sh "'${builderPath}' scan '**/*.zip' --ant=true --watches ${watchName} --format=simple-json"
}
}
}
catch (e) {
notifyFailure('Scanning Binaries under watch ${watchName}', e)
throw e
}
}
def build(goos, goarch, pkg, fileName) {
dir("${jfrogCliRepoDir}") {
script {
def zipName = "${pkg}.zip"
def sourceDirToZip = "./binaries/${pkg}/"
withEnv(["GOOS=${goos}", "GOARCH=${goarch}"]) {
echo "Building for OS: ${goos}, Arch: ${goarch}"
sh "./build/build.sh ./binaries/${pkg}/$fileName"
}
echo "Zipping the output..."
sh "ls -la binaries/${pkg}/$fileName"
zip(
zipFile: zipName,
dir: "binaries/${pkg}/"
)
sh "mv ${zipName} ${zippedBinariesDir}"
}
}
}
def buildAndUpload(goos, goarch, pkg, fileExtension) {
def extension = fileExtension == null ? '': fileExtension
def fileName = "$cliExecutableName$fileExtension"
build(goos, goarch, pkg, fileName)
uploadBinaryToJfrogRepo21(pkg, fileName)
}