foo/Jenkinsfile

454 lines
14 KiB
Plaintext
Raw Normal View History

2024-09-11 13:30:33 +00:00
defaults = [
action_type: ['print_branches'],
action_help: '',
version: '0.0.0',
protect_branch: true
]
2024-09-12 09:07:37 +00:00
if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME.startsWith('hotfix/v')) {
2024-09-11 13:30:33 +00:00
defaults.branch_type = 'hotfix'
}
2024-09-12 09:07:37 +00:00
if (env.BRANCH_NAME == 'develop' || env.BRANCH_NAME.startsWith('release/v')) {
2024-09-11 13:30:33 +00:00
defaults.branch_type = 'release'
}
2024-09-12 09:07:37 +00:00
if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'develop') {
defaults.action_type.add('start')
defaults.action_type.add('protect')
defaults.action_type.add('unprotect')
2024-09-11 13:30:33 +00:00
}
2024-09-12 09:07:37 +00:00
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')
2024-09-11 13:30:33 +00:00
}
2024-09-09 16:30:13 +00:00
pipeline {
agent {
2024-09-11 13:30:33 +00:00
label 'branch_manager'
}
environment {
GIT_SERVER = 'git.onlyoffice.com'
GITEA_TOKEN = credentials('gitea-token')
TELEGRAM_TOKEN = credentials('telegram-bot-token')
}
options {
disableConcurrentBuilds()
2024-09-09 16:30:13 +00:00
}
parameters {
2024-09-11 13:30:33 +00:00
booleanParam (
name: 'wipe',
description: 'Wipe out current workspace',
defaultValue: false
)
choice (
name: 'action_type',
description: "Action type",
choices: defaults.action_type
)
string (
name: 'version',
2024-09-12 09:07:37 +00:00
description: 'Release version (for start only) [' + defaults.branch_type + ']',
2024-09-11 13:30:33 +00:00
defaultValue: defaults.version
)
booleanParam (
name: 'protect_branch',
description: 'Protect branch (for start only)',
defaultValue: defaults.protect_branch
)
string (
name: 'extra_branch',
description: 'Extra branch (for finish only)',
defaultValue: ''
)
booleanParam (
name: 'notify',
description: 'Telegram notification',
defaultValue: true
)
2024-09-09 16:30:13 +00:00
}
stages {
2024-09-11 13:30:33 +00:00
stage('Branch Manager') {
2024-09-09 16:30:13 +00:00
steps {
2024-09-11 13:30:33 +00:00
script {
currentBuild.displayName += ' - ' + params.action_type
2024-09-12 09:07:37 +00:00
if (params.action_type == 'start')
2024-09-11 13:30:33 +00:00
currentBuild.displayName += ' ' + params.version
if (params.wipe) {
deleteDir()
checkout scm
}
stats = [repos: [:]]
String branch = env.BRANCH_NAME
ArrayList baseBranches = []
getRepos().each {
stats.repos.put(it, [:])
}
Boolean pAction
Boolean sAction
if (params.action_type == 'print_branches') {
stats.repos.each { repo, status ->
pAction = printBranches(repo)
status.primary = (pAction) ? 'success' : 'failure'
2024-09-11 13:38:58 +00:00
status.secondary = 'none'
2024-09-11 13:30:33 +00:00
}
2024-09-12 09:07:37 +00:00
} else if (params.action_type == 'start') {
2024-09-11 13:30:33 +00:00
branch = defaults.branch_type + '/v' + params.version
baseBranches = [env.BRANCH_NAME]
stats.repos.each { repo, status ->
if (checkRemoteBranch(repo, branch)) {
echo "${repo}: Branch already ${branch} exists."
status.primary = 'skip'
} else {
dir ('repos/' + repo) {
checkoutRepo(repo)
if (!checkRemoteBranch(repo, 'develop')
&& !createBranch(repo, 'develop', 'master'))
error("Can't create develop branch.")
pAction = createBranch(repo, branch, baseBranches[0])
status.primary = (pAction) ? 'success' : 'failure'
}
}
2024-09-11 13:38:58 +00:00
if (params.protect_branch && !repo.contains('documents-pipeline')) {
2024-09-11 13:30:33 +00:00
sAction = protectBranch(repo, branch)
status.secondary = (sAction) ? 'lock' : ''
}
}
2024-09-12 09:07:37 +00:00
} else if (params.action_type == 'merge') {
2024-09-11 13:30:33 +00:00
baseBranches = ['master']
stats.repos.each { repo, status ->
if (!checkRemoteBranch(repo, branch)) {
echo "${repo}: Branch doesn't ${branch} exist."
status.primary = 'skip'
} else {
dir ('repos/' + repo) {
checkoutRepo(repo)
pAction = mergeBranch(repo, branch, baseBranches)
status.primary = (pAction) ? 'success' : 'failure'
}
}
}
2024-09-12 09:07:37 +00:00
} else if (params.action_type == 'finish') {
2024-09-11 13:30:33 +00:00
baseBranches = ['master', 'develop']
if (!params.extra_branch.isEmpty())
baseBranches.add(params.extra_branch)
stats.repos.each { repo, status ->
if (!checkRemoteBranch(repo, branch)) {
echo "${repo}: Branch doesn't ${branch} exist."
status.primary = 'skip'
} else {
dir ('repos/' + repo) {
checkoutRepo(repo)
unprotectBranch(repo, branch)
pAction = mergeBranch(repo, branch, baseBranches)
status.primary = (pAction) ? 'success' : 'failure'
if (pAction && !repo.contains('documents-pipeline')) {
sAction = deleteBranch(repo, branch)
status.secondary = (sAction) ? 'delete' : ''
}
}
}
}
2024-09-12 09:07:37 +00:00
} else if (params.action_type == 'rename') {
2024-09-11 13:30:33 +00:00
branch = defaults.branch_type + '/v' + params.version
baseBranches = [env.BRANCH_NAME]
stats.repos.each { repo, status ->
if (checkRemoteBranch(repo, branch)) {
echo "${repo}: Branch already ${branch} exists."
status.primary = 'skip'
} else {
dir ('repos/' + repo) {
checkoutRepo(repo, env.BRANCH_NAME)
pAction = createBranch(repo, branch, env.BRANCH_NAME)
status.primary = (pAction) ? 'success' : 'failure'
if (pAction) {
unprotectBranch(repo, env.BRANCH_NAME)
deleteBranch(repo, env.BRANCH_NAME)
2024-09-11 13:38:58 +00:00
if (params.protect_branch && !repo.contains('documents-pipeline')) {
2024-09-11 13:30:33 +00:00
sAction = protectBranch(repo, branch)
status.secondary = (sAction) ? 'lock' : ''
}
}
}
}
}
2024-09-12 09:07:37 +00:00
} else if (params.action_type == 'delete') {
2024-09-11 13:30:33 +00:00
stats.repos.each { repo, status ->
if (!checkRemoteBranch(repo, branch)) {
echo "${repo}: Branch doesn't ${branch} exist."
status.primary = 'skip'
} else {
dir ('repos/' + repo) {
checkoutRepo(repo, branch)
unprotectBranch(repo, branch)
if (!repo.contains('documents-pipeline')) {
pAction = deleteBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure'
}
}
}
}
2024-09-12 09:07:37 +00:00
} else if (params.action_type == 'protect') {
2024-09-11 13:30:33 +00:00
stats.repos.each { repo, status ->
pAction = protectBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
}
2024-09-12 09:07:37 +00:00
} else if (params.action_type == 'unprotect') {
2024-09-11 13:30:33 +00:00
stats.repos.each { repo, status ->
pAction = unprotectBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
}
}
stats.putAll([
branch: branch,
baseBranches: baseBranches,
success: stats.repos.findAll { repo, status ->
status.primary in ['skip', 'success']
}.size(),
total: stats.repos.size()
])
println stats
}
2024-09-09 16:30:13 +00:00
}
}
}
2024-09-11 13:30:33 +00:00
post {
success {
script {
if (stats.success == 0)
currentBuild.result = 'FAILURE'
else if (stats.success != stats.total)
currentBuild.result = 'UNSTABLE'
else if (stats.success == stats.total)
currentBuild.result = 'SUCCESS'
sendNotification()
}
}
}
}
def getRepos() {
return ['heatray/foo']
}
def checkoutRepo(String repo, String branch = 'master') {
sh (
label: "${repo}: checkout",
script: """
if [ "\$(GIT_DIR=.git git rev-parse --is-inside-work-tree)" = 'true' ]; then
git fetch --all --prune
git switch -f ${branch}
git reset --hard origin/${branch}
git clean -df
else
rm -rfv ./*
git clone -b ${branch} git@\$GIT_SERVER:${repo}.git .
fi
git branch -vv
"""
)
}
def checkRemoteBranch(String repo, String branch = 'master') {
return sh (
label: "${repo}: check branch ${branch}",
script: "git ls-remote --exit-code git@\$GIT_SERVER:${repo}.git ${branch}",
returnStatus: true
) == 0
}
def createBranch(String repo, String branch, String baseBranch) {
return sh (
label: "${repo}: start ${branch}",
script: """
git switch ${baseBranch}
git reset --hard origin/${baseBranch}
git checkout -B ${branch}
git push origin ${branch}
git branch -vv
echo "Branch created."
""",
returnStatus: true
) == 0
}
def mergeBranch(String repo, String branch, ArrayList baseBranches) {
return sh (
label: "${repo}: merge ${branch} into ${baseBranches.join(' ')}",
script: """#!/bin/bash -xe
git switch ${branch}
git reset --hard origin/${branch}
base_branches=(${baseBranches.join(' ')})
merged=0
rev_branch=\$(git rev-parse @)
for base in "\${base_branches[@]}"; do
git switch \$base || (((++merged)) && continue)
git reset --hard origin/\$base
rev_base=\$(git rev-parse @)
if [[ \$rev_branch == \$rev_base ]]; then
((++merged))
echo "No new commits."
continue
fi
# gh pr create --repo ${repo} --base \$base --head ${branch} \
# --title "Merge branch ${branch} into \$base" --fill || \
# true
if ! git merge ${branch} --no-edit --no-ff \
-m "Merge branch ${branch} into \$base"; then
git merge --abort
continue
fi
git push origin \$base
((++merged))
done
git branch -vv
if [[ \$merged -ne \${#base_branches[@]} ]]; then
echo "Not fully merged."
exit 2
fi
echo "Branch merged."
""",
returnStatus: true
) == 0
}
def deleteBranch(String repo, String branch) {
return sh (
label: "${repo}: delete ${branch}",
script: """
git switch -f master
git branch -D ${branch}
git push --delete origin ${branch}
echo "Branch deleted."
""",
returnStatus: true
) == 0
}
def printBranches(String repo) {
return sh (
label: "${repo}: branches list",
script: """
2024-09-11 13:38:58 +00:00
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
2024-09-11 13:30:33 +00:00
""",
returnStatus: true
) == 0
}
def protectBranch(String repo, String branch) {
return sh (
label: "${repo}: protect ${branch}",
script: """
2024-09-11 13:38:58 +00:00
HTTP_CODE=\$(echo '{
2024-09-11 15:55:27 +00:00
"branch_name": "${branch}",
"enable_push": true,
"enable_push_whitelist": true,
"push_whitelist_usernames": [
"heatray"
]
2024-09-11 13:30:33 +00:00
}' | \
2024-09-11 13:38:58 +00:00
curl -s -X 'POST' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections' \
-H 'Authorization: token '"\$GITEA_TOKEN" \
2024-09-11 13:30:33 +00:00
-H 'Content-Type: application/json' \
2024-09-11 13:38:58 +00:00
-w '%{http_code}' \
-o output.json \
-d @-)
jq '.' output.json
test \$HTTP_CODE -eq 201
2024-09-11 13:30:33 +00:00
""",
returnStatus: true
) == 0
}
def unprotectBranch(String repo, String branch) {
2024-09-11 13:38:58 +00:00
String branchUrl = URLEncoder.encode(branch)
2024-09-11 13:30:33 +00:00
return sh (
label: "${repo}: unprotect ${branch}",
script: """
2024-09-11 13:38:58 +00:00
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
""",
2024-09-11 13:30:33 +00:00
returnStatus: true
) == 0
}
def sendNotification() {
String text = ''
switch(params.action_type) {
2024-09-12 09:07:37 +00:00
case 'start':
2024-09-11 13:30:33 +00:00
text = "Branch `${stats.branch}` created from `${stats.baseBranches[0]}`"
break
2024-09-12 09:07:37 +00:00
case 'merge':
2024-09-11 13:30:33 +00:00
text = "Branch `${stats.branch}` merged into `${stats.baseBranches[0]}`"
break
2024-09-12 09:07:37 +00:00
case 'finish':
2024-09-11 13:30:33 +00:00
text = "Branch `${stats.branch}` merged into "
text += stats.baseBranches.collect({"`$it`"}).join(', ')
break
default: text = 'Stats'
}
text += " \\[${stats.success}/${stats.total}]"
stats.repos.each { repo, status ->
text += '\n'
switch(status.primary) {
case 'skip': text += '🔘'; break
case 'success': text += '☑️'; break
case 'failure': text += '🚫'; break
default: text += ''
}
switch(status.secondary) {
case 'lock': text += '🔒'; break
case 'delete': text += '♻️'; break
case 'none': text += ''; break
default: text += ''
}
text += " [${repo}](https://${env.GIT_SERVER}/${repo})"
}
echo text
2024-09-09 16:30:13 +00:00
}