Jenkins管道选择特定分支 [英] Jenkins Pipeline Choose Specific Branch
问题描述
我有一条Jenkins管道,我想让用户输入以检查他们选择的特定分支.即如果我创建一个分支'foo'并提交,我希望能够从菜单上建立该分支.由于有多个用户都在创建分支,因此我希望它位于声明性管道而不是GUI中.在下面显示的这个阶段,我希望用户输入在詹金斯(Jenkins)对git进行轮询以找出可用的分支之后检出该分支.这可能吗?
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?
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']]
]);
}
}
我目前有这个,但还不漂亮;
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: '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()
}
}
}
推荐答案
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 .
通过使用插件,您可以指示Jenkins从GIT存储库中获取所有可用的分支,标签.
By using the plugin you can instruct the Jenkins to fetch all the available branches, tags from GIT repository.
使用参数BRANCH_TO_BUILD在管道中获取分支名称,并签出所选分支.
Get the branch name in the pipeline with the parameter BRANCH_TO_BUILD and checkout the chosen branch .
这篇关于Jenkins管道选择特定分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!