-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-bump-version
137 lines (128 loc) · 3.77 KB
/
Jenkinsfile-bump-version
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
#!/usr/bin/env groovy
currentBuild.description = "${version} -> ${ref}"
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
def semverPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
pipeline {
agent {
kubernetes {
cloud 'SLKE'
defaultContainer 'maven'
yaml """
kind: Pod
spec:
# All containers should have the same UID
securityContext:
runAsUser: 0
containers:
- name: maven
image: harbor.softleader.com.tw/library/maven:3-eclipse-temurin-17
imagePullPolicy: Always
command: ['cat']
tty: true
resources:
limits:
memory: "1Gi"
cpu: "2"
volumeMounts:
- name: m2
mountPath: /root/.m2
- name: git
image: harbor.softleader.com.tw/library/git:2
imagePullPolicy: Always
command: ['cat']
tty: true
resources:
limits:
memory: "100Mi"
cpu: "100m"
volumes:
- name: m2
persistentVolumeClaim:
claimName: m2-claim
"""
}
}
environment {
// 在 Jenkins 中 System Configuration > Manage Credential
// ref: https://docs.cloudbees.com/docs/cloudbees-ci/latest/cloud-secure-guide/injecting-secrets
CREDENTIAL = credentials("a84db61d-b4a4-4e05-a368-c1b283860090")
MAVEN_OPTS="-Xmx768m -XX:MaxMetaspaceSize=128m"
}
stages {
stage('Setup') {
steps {
script {
env.PR_BRANCH = "bot/bump-version-to-${version}"
}
container('git') {
script {
env.GIT_PATH = sh(
script: 'echo $GIT_URL | awk -F"github.com/" "{print \\\$2}"',
returnStdout: true
).trim()
sh """
git remote set-url origin https://$CREDENTIAL_USR:"$CREDENTIAL_PSW"@github.com/$GIT_PATH".git"
git config --global user.email "[email protected]"
git config --global user.name "jenkins[bot]"
echo $CREDENTIAL_PSW | gh auth login --with-token
"""
}
}
sh 'printenv'
sh 'java -version'
sh 'mvn --version'
echo "${params}"
}
}
stage ('Preflight Checks') {
steps {
script {
def isSemVer = ("${version}" ==~ semverPattern)
if (!isSemVer) {
error "Tag must matches semantic versioning 2 (https://semver.org/) but got: ${version}";
}
}
sh "make format"
container('git') {
sh '[ ! -z "$(git status -s)" ] && echo "排版檢查不通過!" && exit 1 || echo "Good to go!"'
}
sh "make new-version VERSION=${version}"
container('git') {
sh '[ -z "$(git status -s)" ] && echo "版本號沒異動喔! 是不是用到重複的版本號了!?" && exit 1 || echo "Good to go!"'
}
}
}
stage('Unit Testing') {
steps {
sh "make test"
}
post {
always {
junit "**/target/surefire-reports/**/*.xml"
}
}
}
stage ('Bump New Version') {
steps {
container('git') {
sh """
git checkout -b ${PR_BRANCH}
git commit -am "chore: bump version to ${version}"
git push --set-upstream origin ${PR_BRANCH}
gh pr create --base ${ref} --title "chore: bump version to ${version}" --body "Bump version to ${version} -> ${ref}"
gh pr merge ${PR_BRANCH} --merge --admin --delete-branch
"""
}
}
}
}
post {
failure {
slackSend(
color: "danger",
channel: "@matt",
message: "Attention @here, The pipeline <$BUILD_URL|*${env.JOB_NAME} #${env.BUILD_NUMBER}*> has failed! :omg:"
)
}
}
}