git Jenkins Pipeline 选择特定的分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48404013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jenkins Pipeline Choose Specific Branch
提问by eekfonky
I have a Jenkins Pipeline that I would like to have a user input on to checkout a specific branch of their choosing. i.e. If I create a branch 'foo' and commit it, I'd like to be able to build on that branch from a menu. As there are several users all creating branches I want this to be in a declarative pipeline rather than GUI. At this stage shown below, I'd like a user input to checkout the branch after Jenkins has polled git to find out the branches available. Is this possible?
我有一个 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: '[email protected]:somekindofrepo']]
]);
}
}
I currently have this but it's not pretty;
我目前有这个,但它不漂亮;
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: '[email protected]: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: '[email protected]:repo/notification-sender.git'
}
}
}
post {
always {
echo 'Cleanup'
cleanWs()
}
}
}
采纳答案by Samy
Instead of taking the input as a string you can use "Build with Parameter" along with https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin.
您可以将“带参数构建”与https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin一起使用,而不是将输入作为字符串。
By using the plugin you can instruct the Jenkins to fetch all the available branches, tags from GIT repository.
通过使用该插件,您可以指示 Jenkins 从 GIT 存储库中获取所有可用的分支和标签。
Get the branch name in the pipeline with the parameter BRANCH_TO_BUILD and checkout the chosen branch .
使用参数 BRANCH_TO_BUILD 获取管道中的分支名称并签出所选分支。