[英]Jenkins Pipeline Choose Specific Branch
我有一個 Jenkins 管道,我希望用戶輸入它來結帳他們選擇的特定分支。 即,如果我創建一個分支 'foo' 並提交它,我希望能夠從菜單中在該分支上進行構建。 由於有幾個用戶都在創建分支,我希望這在聲明性管道中而不是 GUI 中。 在如下所示的這個階段,在 Jenkins 輪詢 git 以找出可用的分支之后,我想要一個用戶輸入來簽出分支。 這可能嗎?
stage('Checkout') {
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CloneOption', noTags: true, reference: '', shallow: true]],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'secretkeys', url: 'git@github.com:somekindofrepo']]
]);
}
}
我目前有這個,但它不漂亮;
pipeline {
agent any
stages {
stage("Checkout") {
steps {
checkout([$class: 'GitSCM',
branches: [
[name: '**']
],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'LocalBranch', localBranch: "**"]],
submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: 'repo.notification-sender', url: 'git@github.com:repo/notification-sender.git']
]
])
}
}
stage("Branch To Build") {
steps {
script {
def gitBranches = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref --all | sed s:origin/:: | sort -u')
env.BRANCH_TO_BUILD = input message: 'Please select a branch', ok: 'Continue',
parameters: [choice(name: 'BRANCH_TO_BUILD', choices: gitBranches, description: 'Select the branch to build?')]
}
git branch: "${env.BRANCH_TO_BUILD}", credentialsId: 'repo.notification-sender', url: 'git@github.com:repo/notification-sender.git'
}
}
}
post {
always {
echo 'Cleanup'
cleanWs()
}
}
}
您可以將“帶參數構建”與https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin一起使用,而不是將輸入作為字符串。
通過使用該插件,您可以指示 Jenkins 從 GIT 存儲庫中獲取所有可用的分支和標簽。
使用參數 BRANCH_TO_BUILD 獲取管道中的分支名稱並簽出所選分支。
這是使用 Jenkins 2.249.2 和聲明性管道的沒有任何插件的解決方案:
以下模式通過動態下拉菜單提示用戶(讓他選擇一個分支):
(周圍的 withCredentials 塊是可選的,僅當您的腳本和 jenkins configuratoin 確實使用憑據時才需要)
節點{
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'user-credential-in-gitlab',
usernameVariable: 'GIT_USERNAME',
passwordVariable: 'GITSERVER_ACCESS_TOKEN']]) {
BRANCH_NAMES = sh (script: 'git ls-remote -h https://${GIT_USERNAME}:${GITSERVER_ACCESS_TOKEN}@dns.name/gitlab/PROJS/PROJ.git | sed \'s/\\(.*\\)\\/\\(.*\\)/\\2/\' ', returnStdout:true).trim()
}
} 管道{
agent any
parameters {
choice(
name: 'BranchName',
choices: "${BRANCH_NAMES}",
description: 'to refresh the list, go to configure, disable "this build has parameters", launch build (without parameters)to reload the list and stop it, then launch it again (with parameters)'
)
}
stages {
stage("Run Tests") {
steps {
sh "echo SUCCESS on ${BranchName}"
}
}
}
}
缺點是應該刷新 jenkins 配置並使用空白運行來使用腳本刷新列表......解決方案(不是我的):使用用於專門刷新值的附加參數可以減少這種限制:
parameters {
booleanParam(name: 'REFRESH_BRANCHES', defaultValue: false, description: 'refresh BRANCH_NAMES branch list and launch no step')
choice(
name: 'BranchName',
choices: "${BRANCH_NAMES}",
description: 'choose a branch (ignored if refresh_branches is selected)'
)
}
然后在階段:
stage('a stage') {
when {
expression {
return ! params.REFRESH_BRANCHES.toBoolean()
}
}
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.