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