-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.generate
256 lines (217 loc) · 9.55 KB
/
Jenkinsfile.generate
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
library(
identifier: 'jenkins-shared-library@master',
retriever: modernSCM(
[
$class: 'GitSCMSource',
remote: 'https://github.com/dhanarab/jenkins-pipeline-library.git'
]
)
)
bitbucketCredentialsHttps = "bitbucket-build-extend-https"
bitbucketCredentialsSsh = "bitbucket-build-extend-ssh"
bitbucketCommitHref = null
sdkSpecRepoName = "justice-codegen-sdk-spec"
sdkCodegenRepoName = "justice-codegen-modular-sdk"
mockServerRepoName = "justice-codegen-sdk-mock-server"
sdkRepoName = "justice-codegen-python-modular-sdk"
sdkCodegenGenerateBranchName = "master"
sdkGenerateBranchName = "rc-nightly"
sdkSpecCommitHash = null
sdkCommitHash = null
sdkSpecPath = null
sdkCodegenPath = null
mockServerPath = null
sdkPath = null
sdkCodegenExe = "accelbyte-codegen-linux_amd64"
breakingChangeCheckExitCode = 0
outstandingDeprecationCount = 0
jobChannel = env.SLACK_CHANNEL_ACTIVITY_EXTEND_CODEGEN_MODULAR_SDK ? env.SLACK_CHANNEL_ACTIVITY_EXTEND_CODEGEN_MODULAR_SDK : "#activity-justice-codegen-modular-sdk"
def getTimestampUtc() {
now = new Date()
return now.format("yyyy-MM-dd'T'HH:mm:ss+00:00", TimeZone.getTimeZone('UTC'))
}
pipeline {
agent {
label "extend-builder-batch"
}
stages {
// -------------------------------------- Prepare --------------------------------------
stage("Prepare") {
steps {
script {
sdkCodegenPath = "${env.WORKSPACE}/${sdkCodegenRepoName}"
mockServerPath = "${env.WORKSPACE}/${mockServerRepoName}"
sdkSpecPath = "${env.WORKSPACE}/${sdkSpecRepoName}"
sdkPath = "${env.WORKSPACE}/${sdkRepoName}"
sh "rm -rf ${sdkCodegenRepoName}"
sh "rm -rf ${mockServerRepoName}"
sh "rm -rf ${sdkSpecRepoName}"
sshagent(credentials: [bitbucketCredentialsSsh]) {
sh "git clone --branch ${sdkCodegenGenerateBranchName} --depth 1 git@bitbucket.org:accelbyte/${sdkCodegenRepoName}.git ${sdkCodegenPath}"
sh "git clone --depth 1 git@bitbucket.org:accelbyte/${mockServerRepoName}.git ${mockServerPath}"
sh "git clone --depth 50 git@bitbucket.org:accelbyte/${sdkSpecRepoName}.git ${sdkSpecPath}"
}
dir(sdkSpecPath) {
sdkSpecCommitHash = sh(returnStdout: true, script: "git rev-list -1 HEAD spec/stage_extend-sdk/TIMESTAMP").trim()
}
}
}
}
// -------------------------------------- Generate --------------------------------------
stage("Generate SDK") {
steps {
sh "sudo apt install -y jq"
// Setup generate branch
dir(sdkRepoName) {
sh "git branch ${sdkGenerateBranchName}"
sh "git checkout ${sdkGenerateBranchName}"
}
// Copy latest OpenAPI spec from spec repository to SDK repository
sh "rm -fv ${sdkPath}/spec/*"
sh "find ${sdkSpecPath}/spec/stage_extend-sdk/ -maxdepth 1 -type f -exec cp -v -t ${sdkPath}/spec/ {} \\;"
sh "cp -v ${sdkSpecPath}/docs/openapi-2.0-vendor-extensions.md ${sdkPath}/spec/"
// Build Linux Executable
dir(sdkCodegenPath) {
sh script: "make build-linux", label: "Build Linux Executable"
}
// Generate SDK (SDK only)
dir(sdkCodegenPath) {
script {
version = sh(returnStdout: true, script: " jq -r '.\"src/all\"' ${sdkPath}/sh/package.json").trim()
}
sh script: "cd templates/python-modular-extend-sdk && make sdk_all CODEGEN_PATH=${sdkCodegenPath}/${sdkCodegenExe} SDK_PATH=${sdkPath} SPEC_PATH=${sdkPath}/spec VERSION=${version}", label: "Generate SDK (SDK only)"
}
// Run CLI tests against the Mock Server
dir(sdkPath) {
script {
breakingChangeCheckExitCode = sh script: "make test_cli SDK_MOCK_SERVER_PATH=${mockServerPath}", returnStatus: true, label: "Run CLI tests against the Mock Server"
}
}
// Generate SDK (CLI, CLI tests, Docs, Style, Revision)
dir(sdkCodegenPath) {
sh script: "cd templates/python-modular-extend-sdk && make cli_cmd_all cli_test_all doc_index_all style revision CODEGEN_PATH=${sdkCodegenPath}/${sdkCodegenExe} SDK_PATH=${sdkPath} SPEC_PATH=${sdkPath}/spec", label: "Generate SDK (CLI, CLI tests, Docs, Style, Revision)"
}
// Bump SDK version and Commit the generated code to branch, overwrite existing
dir(sdkPath) {
sh "sudo apt install -y jq"
sh "make bumpif_all"
sh "git add --all"
sh "git commit -m 'chore(sdk): generate python extend sdk (${getTimestampUtc()})' -m 'generated from openapi spec commit: ${sdkSpecCommitHash}'"
sshagent(credentials: [bitbucketCredentialsSsh]) {
sh "git push --set-upstream origin +${sdkGenerateBranchName}:${sdkGenerateBranchName}"
}
script {
sdkCommitHash = git.getCommitHash()
bitbucketCommitHref = "https://api.bitbucket.org/2.0/repositories/accelbyte/${sdkRepoName}/commit/${sdkCommitHash}"
bitbucket.setBuildStatus(bitbucketCredentialsHttps, bitbucketCommitHref, "INPROGRESS", env.JOB_NAME.take(30), "${env.JOB_NAME}-${env.BUILD_NUMBER}", "Jenkins", "${env.BUILD_URL}console")
}
}
// Check outstanding deprecation
script {
dir(sdkPath) {
// Ignore exit code, just need the outstanding_deprecation.out file
sh script: "make outstanding_deprecation || true", label: "Check outstanding deprecation"
if (!fileExists('outstanding_deprecation.out')) {
error("File 'outstanding_deprecation.out' is not found")
}
outstandingDeprecationCount = sh(returnStdout: true, script: "grep -c '^not ok' outstanding_deprecation.out || true").trim() as Integer
}
}
}
}
// -------------------------------------- Tests --------------------------------------
stage('Core Tests') {
steps {
dir(sdkRepoName) {
sh "make test_core SDK_MOCK_SERVER_PATH=${mockServerPath}"
}
}
}
stage('CLI Tests') {
steps {
dir(sdkRepoName) {
sh "make test_cli SDK_MOCK_SERVER_PATH=${mockServerPath}"
}
}
post {
always {
script {
dir(sdkRepoName) {
archiveArtifacts artifacts: 'samples/cli/tests/*.tap'
step([$class: "TapPublisher", testResults: "samples/cli/tests/*.tap"])
}
}
}
}
}
stage('Integration Tests') {
options {
lock("justice-demo-serversdktest")
}
environment {
DEFAULT_IFACE = sh(script: 'ip route get 1.1.1.1 | awk \'NR==2 {print $1}\' RS=dev', returnStdout: true)
}
steps {
dir(sdkRepoName) {
sh 'sudo tc qdisc add dev $DEFAULT_IFACE root netem delay 100ms' // Delay sending packets due to database eventual consistency
withCredentials([
file(credentialsId: 'justice-codegen-sdk-integration-test-env', variable: 'ENV_FILE_PATH'),
]) {
sh 'make test_integration ENV_FILE_PATH=$ENV_FILE_PATH'
}
}
}
post {
always {
script {
sh 'sudo tc qdisc del dev $DEFAULT_IFACE root netem delay 100ms'
dir(sdkRepoName) {
sh 'rm -f tests/integration/.env'
archiveArtifacts artifacts: 'test_integration.tap'
step([$class: "TapPublisher", testResults: "test_integration.tap"])
}
}
}
}
}
}
post{
success {
script {
if (bitbucketCommitHref != null) {
bitbucket.setBuildStatus(bitbucketCredentialsHttps, bitbucketCommitHref, "SUCCESSFUL", env.JOB_NAME.take(30), "${env.JOB_NAME}-${env.BUILD_NUMBER}", "Jenkins", "${env.BUILD_URL}console")
}
additionalInfo = ""
if (breakingChangeCheckExitCode != 0) {
additionalInfo += """
| :warning: Possible Breaking Changes Detected: <${env.BUILD_URL}/flowGraphTable/#:~:text=Check%20code%2Dlevel%20breaking%20changes|here>"""
}
if (outstandingDeprecationCount != 0) {
additionalInfo += """
| :warning: Outstanding Deprecation Detected: <${env.BUILD_URL}/flowGraphTable/#:~:text=Check%20outstanding%20deprecation|${outstandingDeprecationCount}>"""
}
specLink = "https://bitbucket.org/accelbyte/justice-codegen-sdk-spec/commits/${sdkSpecCommitHash}"
sdkLink = "https://bitbucket.org/accelbyte/${sdkRepoName}/commits/${sdkCommitHash}"
message = """
:white_check_mark: <${env.BUILD_URL}|${env.JOB_NAME}-${env.BUILD_NUMBER}> *Generate Python Extend SDK Successful :python:*
|*OpenAPI Spec:* <${specLink}|${sdkSpecCommitHash}>
|*Generated SDK:* <${sdkLink}|${sdkCommitHash}>
|<https://bitbucket.org/accelbyte/${sdkRepoName}/pull-requests/new?source=${sdkGenerateBranchName}|Compare or Create Pull Request>
${additionalInfo}
|""".stripMargin()
slackSend(channel: jobChannel, color: '#36B37E', message: message)
}
}
failure {
script {
if (bitbucketCommitHref != null) {
bitbucket.setBuildStatus(bitbucketCredentialsHttps, bitbucketCommitHref, "FAILED", env.JOB_NAME.take(30), "${env.JOB_NAME}-${env.BUILD_NUMBER}", "Jenkins", "${env.BUILD_URL}console")
}
message = """
:no_entry: <${env.BUILD_URL}|${env.JOB_NAME}-${env.BUILD_NUMBER}> *failed*
|""".stripMargin()
slackSend(channel: jobChannel, color: '#FF0000', message: message)
}
}
}
}