This commit is contained in:
Semyon Bezrukov 2024-09-12 14:29:31 +03:00
parent 3692781bb6
commit b517d6bccd

52
Jenkinsfile vendored
View File

@ -31,6 +31,7 @@ pipeline {
} }
environment { environment {
GIT_SERVER = 'git.onlyoffice.com' GIT_SERVER = 'git.onlyoffice.com'
GIT_OWNER = 'heatray'
GITEA_TOKEN = credentials('gitea-token') GITEA_TOKEN = credentials('gitea-token')
TELEGRAM_TOKEN = credentials('telegram-bot-token') TELEGRAM_TOKEN = credentials('telegram-bot-token')
} }
@ -55,7 +56,7 @@ pipeline {
) )
booleanParam ( booleanParam (
name: 'protect_branch', name: 'protect_branch',
description: 'Protect branch (for start only)', description: 'Protect branch (for start & rename only)',
defaultValue: defaults.protect_branch defaultValue: defaults.protect_branch
) )
string ( string (
@ -96,7 +97,6 @@ pipeline {
stats.repos.each { repo, status -> stats.repos.each { repo, status ->
pAction = printBranches(repo) pAction = printBranches(repo)
status.primary = (pAction) ? 'success' : 'failure' status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
} }
} else if (params.action_type == 'start') { } else if (params.action_type == 'start') {
@ -120,9 +120,13 @@ pipeline {
} }
} }
if (params.protect_branch && !repo.contains('documents-pipeline')) { if (params.protect_branch) {
if (repo != 'documents-pipeline') {
sAction = protectBranch(repo, branch) sAction = protectBranch(repo, branch)
status.secondary = (sAction) ? 'lock' : '' } else {
sAction = false
}
status.secondary = (sAction) ? 'lock' : 'none'
} }
} }
@ -159,9 +163,13 @@ pipeline {
unprotectBranch(repo, branch) unprotectBranch(repo, branch)
pAction = mergeBranch(repo, branch, baseBranches) pAction = mergeBranch(repo, branch, baseBranches)
status.primary = (pAction) ? 'success' : 'failure' status.primary = (pAction) ? 'success' : 'failure'
if (pAction && !repo.contains('documents-pipeline')) { if (pAction) {
if (repo != 'documents-pipeline') {
sAction = deleteBranch(repo, branch) sAction = deleteBranch(repo, branch)
status.secondary = (sAction) ? 'delete' : '' } else {
sAction = false
}
status.secondary = (sAction) ? 'delete' : 'none'
} }
} }
} }
@ -184,9 +192,13 @@ pipeline {
if (pAction) { if (pAction) {
unprotectBranch(repo, env.BRANCH_NAME) unprotectBranch(repo, env.BRANCH_NAME)
deleteBranch(repo, env.BRANCH_NAME) deleteBranch(repo, env.BRANCH_NAME)
if (params.protect_branch && !repo.contains('documents-pipeline')) { if (params.protect_branch) {
if (repo != 'documents-pipeline') {
sAction = protectBranch(repo, branch) sAction = protectBranch(repo, branch)
status.secondary = (sAction) ? 'lock' : '' } else {
sAction = false
}
status.secondary = (sAction) ? 'lock' : 'none'
} }
} }
} }
@ -203,7 +215,7 @@ pipeline {
dir ('repos/' + repo) { dir ('repos/' + repo) {
checkoutRepo(repo, branch) checkoutRepo(repo, branch)
unprotectBranch(repo, branch) unprotectBranch(repo, branch)
if (!repo.contains('documents-pipeline')) { if (repo != 'documents-pipeline') {
pAction = deleteBranch(repo, branch) pAction = deleteBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure' status.primary = (pAction) ? 'success' : 'failure'
} }
@ -216,7 +228,6 @@ pipeline {
stats.repos.each { repo, status -> stats.repos.each { repo, status ->
pAction = protectBranch(repo, branch) pAction = protectBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure' status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
} }
} else if (params.action_type == 'unprotect') { } else if (params.action_type == 'unprotect') {
@ -224,7 +235,6 @@ pipeline {
stats.repos.each { repo, status -> stats.repos.each { repo, status ->
pAction = unprotectBranch(repo, branch) pAction = unprotectBranch(repo, branch)
status.primary = (pAction) ? 'success' : 'failure' status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
} }
} }
@ -260,7 +270,7 @@ pipeline {
} }
def getRepos() { def getRepos() {
return ['heatray/foo'] return ['foo']
} }
def checkoutRepo(String repo, String branch = 'master') { def checkoutRepo(String repo, String branch = 'master') {
@ -274,7 +284,7 @@ def checkoutRepo(String repo, String branch = 'master') {
git clean -df git clean -df
else else
rm -rfv ./* rm -rfv ./*
git clone -b ${branch} git@\$GIT_SERVER:${repo}.git . git clone -b ${branch} git@\$GIT_SERVER:\$GIT_OWNER/${repo}.git .
fi fi
git branch -vv git branch -vv
""" """
@ -284,7 +294,7 @@ def checkoutRepo(String repo, String branch = 'master') {
def checkRemoteBranch(String repo, String branch = 'master') { def checkRemoteBranch(String repo, String branch = 'master') {
return sh ( return sh (
label: "${repo}: check branch ${branch}", label: "${repo}: check branch ${branch}",
script: "git ls-remote --exit-code git@\$GIT_SERVER:${repo}.git ${branch}", script: "git ls-remote --exit-code git@\$GIT_SERVER:\$GIT_OWNER/${repo}.git ${branch}",
returnStatus: true returnStatus: true
) == 0 ) == 0
} }
@ -322,7 +332,7 @@ def mergeBranch(String repo, String branch, ArrayList baseBranches) {
echo "No new commits." echo "No new commits."
continue continue
fi fi
# gh pr create --repo ${repo} --base \$base --head ${branch} \ # gh pr create --repo \$GIT_OWNER/${repo} --base \$base --head ${branch} \
# --title "Merge branch ${branch} into \$base" --fill || \ # --title "Merge branch ${branch} into \$base" --fill || \
# true # true
if ! git merge ${branch} --no-edit --no-ff \ if ! git merge ${branch} --no-edit --no-ff \
@ -362,7 +372,7 @@ def printBranches(String repo) {
label: "${repo}: branches list", label: "${repo}: branches list",
script: """ script: """
HTTP_CODE=\$(curl -s -X 'GET' \ HTTP_CODE=\$(curl -s -X 'GET' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branches' \ 'https://'"\$GIT_SERVER"'/api/v1/repos/'"\$GIT_OWNER"'/${repo}/branches' \
-H 'Authorization: token '"\$GITEA_TOKEN" \ -H 'Authorization: token '"\$GITEA_TOKEN" \
-w '%{http_code}' \ -w '%{http_code}' \
-o output.json) -o output.json)
@ -386,7 +396,7 @@ def protectBranch(String repo, String branch) {
] ]
}' | \ }' | \
curl -s -X 'POST' \ curl -s -X 'POST' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections' \ 'https://'"\$GIT_SERVER"'/api/v1/repos/'"\$GIT_OWNER"'/${repo}/branch_protections' \
-H 'Authorization: token '"\$GITEA_TOKEN" \ -H 'Authorization: token '"\$GITEA_TOKEN" \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-w '%{http_code}' \ -w '%{http_code}' \
@ -405,7 +415,7 @@ def unprotectBranch(String repo, String branch) {
label: "${repo}: unprotect ${branch}", label: "${repo}: unprotect ${branch}",
script: """ script: """
HTTP_CODE=\$(curl -s -X 'DELETE' \ HTTP_CODE=\$(curl -s -X 'DELETE' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections/${branchUrl}' \ 'https://'"\$GIT_SERVER"'/api/v1/repos/'"\$GIT_OWNER"'/${repo}/branch_protections/${branchUrl}' \
-H 'Authorization: token '"\$GITEA_TOKEN" \ -H 'Authorization: token '"\$GITEA_TOKEN" \
-w '%{http_code}' \ -w '%{http_code}' \
-o output.json) -o output.json)
@ -443,10 +453,10 @@ def sendNotification() {
switch(status.secondary) { switch(status.secondary) {
case 'lock': text += '🔒'; break case 'lock': text += '🔒'; break
case 'delete': text += '♻️'; break case 'delete': text += '♻️'; break
case 'none': text += ''; break case 'none': text += ''; break
default: text += '' default: text += ''
} }
text += " [${repo}](https://${env.GIT_SERVER}/${repo})" text += " [${repo}](https://${env.GIT_SERVER}/${env.GIT_OWNER}/${repo})"
} }
echo text echo text