forked from apache/dubbo-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
117 lines (102 loc) · 3.24 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
@Library('visenze-lib')_
pipeline {
// choose a suitable agent to build
agent {
label "${params.AGENT_LABEL ?: 'build-arm64'}"
}
options {
timestamps()
}
parameters {
string(name: 'DOCKER_REGISTRY', defaultValue: '', description: 'leave empty to use dockerhub as registry')
string(name: 'DOCKER_REGISTRY_CREDENTIAL', defaultValue: 'docker-hub-credential',
description: 'The credential ID stored in Jenkins to pull/push to docker registry')
string(name: 'AGENT_LABEL', defaultValue: '', description: 'The label of the agent to run the build')
}
stages {
stage('Checkout') {
steps {
// checkout the code from scm (github)
checkout scm
}
}
stage('Package') {
steps {
script {
sh("docker run --rm -v ${WORKSPACE}:${WORKSPACE} -v ${HOME}/.m2:/root/.m2 -w ${WORKSPACE} maven:3.9-amazoncorretto-11 mvn clean compile package install")
}
}
}
stage('Docker Build and Push') {
when {
expression {
return doBuildAndTest(env.BRANCH_NAME)
}
}
steps {
script {
// assume the Dockerfile is directly under WORKSPACE
def commitHash = getCommit()
def version = getVersion()
// push all the tags
def tags = genDockerTags(env.BRANCH_NAME, commitHash, version, env.BUILD_NUMBER)
// keep the last tag as a global variable used by later stages
TAG = tags[-1]
def tagsString = tags.collect{"-t visenze/dubbo-admin:${it}"}.join(" ")
docker.withRegistry(params.DOCKER_REGISTRY, params.DOCKER_REGISTRY_CREDENTIAL) {
sh "docker buildx create --use"
sh "docker buildx build ${tagsString} -f docker/Dockerfile --push --platform linux/arm64,linux/amd64 ."
}
}
}
}
stage('Archive') {
when {
expression {
return doBuildAndTest(env.BRANCH_NAME) || isProdRelease(env.BRANCH_NAME)
}
}
steps {
script {
def archive = [
docker_tag: TAG,
version: getVersion(),
docker_repos: ['visenze/dubbo-admin']
]
writeFile(file: "${WORKSPACE}/version.json", text: groovy.json.JsonOutput.toJson(archive))
archiveArtifacts(artifacts: "version.json", allowEmptyArchive: true)
}
}
}
}
}
// Get commit sha
def getCommit() {
return sh(
script: "(cd '${WORKSPACE}'; git rev-parse HEAD)", returnStdout: true
).trim()
}
def getVersion() {
// read version from pom
return readMavenPom(file: 'pom.xml').properties.revision
}
def genDockerTags(branch, commit, version=null, buildNumber=null) {
def tags = []
def shortCommit = commit.substring(0, 9)
tags.add(branch.replaceAll("/", "_"))
tags.add("${branch.replaceAll("/", "_")}-${shortCommit}")
//if(branch == 'production') {
// assert version !=null
// tags.add("${version}")
//} else {
// assert version != null && buildNumber != null
// tags.add("${version}.${buildNumber}-${shortCommit}")
//}
return tags
}
def doBuildAndTest(branch) {
return branch.startsWith('feature') || branch == 'staging' || branch == 'production'
}
def isProdRelease(branch) {
return env.BRANCH_NAME == "production"
}