Compare commits

...

4 Commits

Author SHA1 Message Date
7f72daf054 BM5 2024-09-12 14:35:23 +03:00
3692781bb6 BM4 2024-09-12 12:43:48 +03:00
8ddeceb0f9 BM3 2024-09-12 11:03:42 +03:00
dfd04e1bf4 BM2 2024-09-11 18:42:29 +03:00

124
Jenkinsfile vendored
View File

@ -5,21 +5,24 @@ defaults = [
protect_branch: true
]
if (env.BRANCH_NAME == 'master') {
defaults.action_type.add('start_hotfix')
if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME.startsWith('hotfix/v')) {
defaults.branch_type = 'hotfix'
}
if (env.BRANCH_NAME == 'develop') {
defaults.action_type.add('start_release')
if (env.BRANCH_NAME == 'develop' || env.BRANCH_NAME.startsWith('release/v')) {
defaults.branch_type = 'release'
}
if (env.BRANCH_NAME ==~ /^(hotfix)\/.+/) {
defaults.action_type.addAll(['merge_hotfix', 'finish_hotfix', 'rename_hotfix', 'delete_hotfix', 'unprotect_hotfix'])
defaults.branch_type = 'hotfix'
if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'develop') {
defaults.action_type.add('start')
defaults.action_type.add('protect')
defaults.action_type.add('unprotect')
}
if (env.BRANCH_NAME ==~ /^(release)\/.+/) {
defaults.action_type.addAll(['merge_release', 'finish_release', 'rename_release', 'delete_release', 'unprotect_release'])
defaults.branch_type = 'release'
if (env.BRANCH_NAME ==~ /^(hotfix|release)\/v.+/) {
defaults.action_type.add('merge')
defaults.action_type.add('finish')
defaults.action_type.add('rename')
defaults.action_type.add('delete')
defaults.action_type.add('protect')
defaults.action_type.add('unprotect')
}
pipeline {
@ -29,7 +32,6 @@ pipeline {
environment {
GIT_SERVER = 'git.onlyoffice.com'
GITEA_TOKEN = credentials('gitea-token')
GITHUB_TOKEN = credentials('github-token')
TELEGRAM_TOKEN = credentials('telegram-bot-token')
}
options {
@ -48,12 +50,12 @@ pipeline {
)
string (
name: 'version',
description: 'Release version (for start only)',
description: 'Release version (for start only) [' + defaults.branch_type + ']',
defaultValue: defaults.version
)
booleanParam (
name: 'protect_branch',
description: 'Protect branch (for start only)',
description: 'Protect branch (for start & rename only)',
defaultValue: defaults.protect_branch
)
string (
@ -72,7 +74,7 @@ pipeline {
steps {
script {
currentBuild.displayName += ' - ' + params.action_type
if (params.action_type in ['start_hotfix', 'start_release'])
if (params.action_type == 'start')
currentBuild.displayName += ' ' + params.version
if (params.wipe) {
@ -96,7 +98,7 @@ pipeline {
status.primary = (pAction) ? 'success' : 'failure'
}
} else if params.action_type.startsWith('start') {
} else if (params.action_type == 'start') {
branch = defaults.branch_type + '/v' + params.version
baseBranches = [env.BRANCH_NAME]
@ -118,12 +120,16 @@ pipeline {
}
if (params.protect_branch) {
if (!repo.contains('documents-pipeline')) {
sAction = protectBranch(repo, branch)
status.secondary = (sAction) ? 'lock' : ''
} else {
sAction = false
}
status.secondary = (sAction) ? 'lock' : 'none'
}
}
} else if params.action_type.startsWith('merge') {
} else if (params.action_type == 'merge') {
baseBranches = ['master']
@ -140,7 +146,7 @@ pipeline {
}
}
} else if params.action_type.startsWith('finish') {
} else if (params.action_type == 'finish') {
baseBranches = ['master', 'develop']
if (!params.extra_branch.isEmpty())
@ -156,15 +162,19 @@ pipeline {
unprotectBranch(repo, branch)
pAction = mergeBranch(repo, branch, baseBranches)
status.primary = (pAction) ? 'success' : 'failure'
if (pAction && !repo.contains('documents-pipeline')) {
if (pAction) {
if (!repo.contains('documents-pipeline')) {
sAction = deleteBranch(repo, branch)
status.secondary = (sAction) ? 'delete' : ''
} else {
sAction = false
}
status.secondary = (sAction) ? 'delete' : 'none'
}
}
}
}
} else if params.action_type.startsWith('rename') {
} else if (params.action_type == 'rename') {
branch = defaults.branch_type + '/v' + params.version
baseBranches = [env.BRANCH_NAME]
@ -182,15 +192,19 @@ pipeline {
unprotectBranch(repo, env.BRANCH_NAME)
deleteBranch(repo, env.BRANCH_NAME)
if (params.protect_branch) {
if (!repo.contains('documents-pipeline')) {
sAction = protectBranch(repo, branch)
status.secondary = (sAction) ? 'lock' : ''
} else {
sAction = false
}
status.secondary = (sAction) ? 'lock' : 'none'
}
}
}
}
}
} else if params.action_type.startsWith('delete') {
} else if (params.action_type == 'delete') {
stats.repos.each { repo, status ->
if (!checkRemoteBranch(repo, branch)) {
@ -208,20 +222,18 @@ pipeline {
}
}
} else if params.action_type.startsWith('protect') {
} else if (params.action_type == 'protect') {
stats.repos.each { repo, status ->
pAction = protectBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
}
} else if params.action_type.startsWith('unprotect') {
} else if (params.action_type == 'unprotect') {
stats.repos.each { repo, status ->
pAction = unprotectBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
}
}
@ -358,11 +370,13 @@ def printBranches(String repo) {
return sh (
label: "${repo}: branches list",
script: """
curl -X 'GET' \
'https://\$GIT_SERVER/api/v1/repos/${repo}/branches' \
-H 'accept: application/json' \
-H 'Authorization: \$GITEA_TOKEN' | \
jq -r '.[] | [.name, .protected] | @tsv'
HTTP_CODE=\$(curl -s -X 'GET' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branches' \
-H 'Authorization: token '"\$GITEA_TOKEN" \
-w '%{http_code}' \
-o output.json)
test \$HTTP_CODE -eq 200
jq -r '.[] | [.name, .protected] | @tsv' output.json
""",
returnStatus: true
) == 0
@ -372,29 +386,41 @@ def protectBranch(String repo, String branch) {
return sh (
label: "${repo}: protect ${branch}",
script: """
echo '{
"branch_name": "master"
HTTP_CODE=\$(echo '{
"branch_name": "${branch}",
"enable_push": true,
"enable_push_whitelist": true,
"push_whitelist_usernames": [
"heatray"
]
}' | \
curl -X 'POST' \
'https://\$GIT_SERVER/api/v1/repos/${repo}/branch_protections?token=\$GITEA_TOKEN' \
-H 'accept: application/json' \
-H 'Authorization: \$GITEA_TOKEN' \
curl -s -X 'POST' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections' \
-H 'Authorization: token '"\$GITEA_TOKEN" \
-H 'Content-Type: application/json' \
-d -
-w '%{http_code}' \
-o output.json \
-d @-)
jq '.' output.json
test \$HTTP_CODE -eq 201
""",
returnStatus: true
) == 0
}
def unprotectBranch(String repo, String branch) {
String branchUrl = URLEncoder.encode(branch)
return sh (
label: "${repo}: unprotect ${branch}",
script: """
curl -X 'DELETE' \
'https://\$GIT_SERVER/api/v1/repos/${repo}/branch_protections/${branch}?token=\$GITEA_TOKEN' \
-H 'accept: application/json' \
-H 'Authorization: \$GITEA_TOKEN'
"""
HTTP_CODE=\$(curl -s -X 'DELETE' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections/${branchUrl}' \
-H 'Authorization: token '"\$GITEA_TOKEN" \
-w '%{http_code}' \
-o output.json)
jq '.' output.json
test \$HTTP_CODE -eq 204 || test \$HTTP_CODE -eq 404
""",
returnStatus: true
) == 0
}
@ -402,13 +428,13 @@ def unprotectBranch(String repo, String branch) {
def sendNotification() {
String text = ''
switch(params.action_type) {
case ['start_hotfix', 'start_release']:
case 'start':
text = "Branch `${stats.branch}` created from `${stats.baseBranches[0]}`"
break
case ['merge_hotfix', 'merge_release']:
case 'merge':
text = "Branch `${stats.branch}` merged into `${stats.baseBranches[0]}`"
break
case ['finish_hotfix', 'finish_release']:
case 'finish':
text = "Branch `${stats.branch}` merged into "
text += stats.baseBranches.collect({"`$it`"}).join(', ')
break
@ -426,8 +452,8 @@ def sendNotification() {
switch(status.secondary) {
case 'lock': text += '🔒'; break
case 'delete': text += '♻️'; break
case 'none': text += ''; break
default: text += ''
case 'none': text += ''; break
default: text += ''
}
text += " [${repo}](https://${env.GIT_SERVER}/${repo})"
}