-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile.github
More file actions
204 lines (181 loc) · 5.85 KB
/
Copy pathJenkinsfile.github
File metadata and controls
204 lines (181 loc) · 5.85 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
import groovy.json.JsonOutput
githubOwner = 'AccelByte'
githubRepo = 'accelbyte-go-modular-sdk'
githubSsh = 'accelbyte-sdk-sdkbuild-ssh'
githubPat = 'accelbyte-sdk-sdkbuild-pat-github'
bitbucketSsh = 'bitbucket-build-extend-ssh'
githubUrl = "git@github.com:${githubOwner}/${githubRepo}.git"
sdkName = "AccelByte Go Extend Modular SDK"
agentLabel = params.ACTION == 'PUSH+RELEASE' ? 'extend-builder-ci && linux-amd64' : 'extend-builder-ci'
jobChannel = '#activity-extend-codegen-modular-sdk'
def createGithubRelease(tag_name, prerelease = false, name = null, body = null) {
def request = ['tag_name': tag_name]
if (name) {
request['name'] = name
}
if (body) {
request['body'] = body
}
request['prerelease'] = prerelease
def requestBody = JsonOutput.toJson(request)
echo "Request Body: ${requestBody}"
def response = httpRequest(
url : "https://api.github.com/repos/${githubOwner}/${githubRepo}/releases",
httpMode: 'POST',
requestBody: requestBody,
contentType: 'APPLICATION_JSON',
authentication: githubPat,
validResponseCodes: '200:201,422'
)
if (response.getStatus() != 201) {
error("Failed to create GitHub release. Response code: ${response.getStatus()}. Response message: ${response.getContent()}")
}
}
def isTagExist(tag) {
codeStatus = sh(script: "if [ \$(git tag -l ${tag}) ]; then exit 0; else exit 1; fi", returnStatus: true)
return codeStatus == 0
}
def pushGitTag(selectedTag, isPrerelease, releaseNote, agsVersion) {
serviceWrapperTag = ''
lobbyServiceWrapperTag = ''
agsTag = ''
// if it is service sdk module
if (selectedTag =~ /.*-sdk\/v[0-9]+.[0-9]+.[0-9]+.*/) {
serviceName = selectedTag.split('-')[0]
serviceVersion = selectedTag.split('-sdk/v')[-1]
serviceWrapperTag = "services-api/pkg/service/${serviceName}/v${serviceVersion}"
if (!isTagExist(serviceWrapperTag)) {
errMessage = "Error: you're trying to release ${selectedTag} " +
"which is a service sdk but corresponding service wrapper ${serviceWrapperTag} not found"
error(errMessage)
}
// special case for lobby-sdk because it has compat module inside services-api/pkg/service/go.mod
if (selectedTag.contains('lobby-sdk')) {
lobbyServiceWrapperTag = "services-api/pkg/service/v${serviceVersion}"
if (!isTagExist(lobbyServiceWrapperTag)) {
errMessage = "Error: you're trying to release ${selectedTag} " +
"which is a service sdk but corresponding service wrapper ${lobbyServiceWrapperTag} not found"
error(errMessage)
}
}
}
// if it is ags tag for release
if (selectedTag =~ /ags\/v[0-9]+\.[0-9]+(?:\.[0-9]+)?/) {
agsTag = "ags/v${agsVersion}"
if (!isTagExist(agsTag)) {
errMessage = "Error: you're trying to release ${selectedTag} not exist in source repository."
error(errMessage)
}
}
// push git tags
sshagent(credentials: [githubSsh]) {
sh "git push ${githubUrl} ${selectedTag}"
if (serviceWrapperTag) {
sh "git push ${githubUrl} ${serviceWrapperTag}"
}
if (lobbyServiceWrapperTag) {
sh "git push ${githubUrl} ${lobbyServiceWrapperTag}"
}
if (agsTag) {
sh "git push ${githubUrl} ${agsTag}"
}
}
// create github release from git tags
releaseName = "${sdkName} for AGS v${agsVersion}"
if (agsTag) {
createGithubRelease(agsTag, isPrerelease, releaseName, releaseNote)
}
}
pipeline {
agent {
label "${agentLabel}"
}
stages {
stage('Prepare') {
steps {
script {
currentBuild.displayName = "#${env.BUILD_NUMBER} ${params.ACTION}"
gitHost = sh(script: "echo '${githubUrl}' | grep -oP '(?<=@).+(?=:)'", returnStdout: true).trim()
sh "grep -q ${gitHost} ~/.ssh/known_hosts || ssh-keyscan ${gitHost} | tee -a ~/.ssh/known_hosts"
}
}
}
stage('Push') {
when {
expression {
return params.ACTION == 'PUSH'
}
}
steps {
script {
sh 'git checkout master'
sshagent(credentials: [githubSsh]) {
sh "git push ${githubUrl} HEAD:main"
}
}
}
}
stage('Release') {
when {
expression {
return params.ACTION == 'PUSH+RELEASE'
}
}
steps {
script {
releaseNote = params.RELEASE_NOTE
if (params.AGS_VERSION != "None") {
releaseNote = releaseNote.replace('@VERSION@', params.AGS_VERSION)
}
if (releaseNote ==~ /(?s).*@[A-Za-z0-9_-]+@.*/) {
echo 'Release note still contains one or more template placeholders e.g. @VERSION@, etc. Please check.'
sh 'false' // Fail this job
}
sh "git checkout ${params.RELEASE_BRANCH}"
sshagent(credentials: [githubSsh]) {
if (params.RELEASE_BRANCH == 'master') {
sh "git push ${githubUrl} HEAD:main"
} else {
sh "git push ${githubUrl} HEAD:${params.RELEASE_BRANCH}"
}
}
params.RELEASE_TAG.split(",").each { tag ->
pushGitTag(tag.trim(), params.IS_PRERELEASE, releaseNote, params.AGS_VERSION)
}
}
}
}
stage('Pull') {
when {
expression {
return params.ACTION == 'PULL'
}
}
steps {
script {
sh 'git checkout master'
sshagent(credentials: [githubSsh])
{
sh "git pull ${githubUrl} main"
}
sshagent(credentials: [bitbucketSsh])
{
sh 'git push'
}
}
}
}
}
post {
failure {
script {
message = """
:no_entry: <${env.BUILD_URL}|${env.JOB_NAME}-${env.BUILD_NUMBER}> *failed*
|*Action*
|${params.ACTION}
|""".stripMargin()
slackSend(channel: jobChannel, color: '#FF0000', message: message)
}
}
}
}