我有一个 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()
}
}
}
最佳答案
您可以将“Build with Parameter”与https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin 一起使用,而不是将输入作为字符串。 .
通过使用该插件,您可以指示 Jenkins 从 GIT 存储库中获取所有可用的分支和标签。
使用参数 BRANCH_TO_BUILD 获取管道中的分支名称并检查所选分支。
关于linux - Jenkins 管道选择特定分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48404013/