Jenkins pipeline执行用户输入步骤后提交仍无法进入后续阶段问题
Jenkins Pipeline input阶段卡住问题解决
问题根因
- 分支列表格式不符合要求:通过
git branch -a读取的远程分支列表自带remotes/origin/前缀、多余空行、行首不可见空格,导致choice参数提交后Jenkins无法正常解析参数值,触发流程阻塞 - 执行器占用冲突:全局配置
agent any后Pipeline全程占用agent执行槽,若Jenkins执行器资源不足,input提交后后续步骤无法分配到执行资源会持续卡住 - 参数取值逻辑兼容问题:多版本Jenkins对input单参数返回值的解析存在兼容问题,隐式取值可能出现异常
修复方案
调整后的完整Jenkinsfile如下:
pipeline { // 全局不绑定agent,仅需要执行命令的阶段单独绑定,避免input阶段占用执行器 agent none stages { stage("Get Branches from Git"){ agent any steps{ sh """ # 过滤远程分支并去掉remotes/origin前缀、排除HEAD指针、去掉空行,输出干净的分支名 git branch -a | grep remotes | sed 's/.*remotes\\/origin\\///' | grep -v 'HEAD' | grep -v '^$' > ${WORKSPACE}/branchesList.txt """ } } stage('User Input'){ // 该阶段不需要执行命令,不绑定agent,不占用执行资源 steps{ script{ // 读取文件后trim去掉首尾空行 listBranchesAvailable = readFile('branchesList.txt').trim() env.branchToDeploy = timeout (time:1, unit:"HOURS") { def userInput = input( message: 'Branch to deploy', ok: 'Confirm!', parameters: [ choice( name: 'targetBranch', choices: listBranchesAvailable, description: 'Which branch do you want to build?' ) ] ) // 显式取参数值,避免版本兼容问题 return userInput } } } } stage('Checkout Code'){ agent any steps{ cleanWs() // ensure workspace is empty git branch: env.branchToDeploy, changelog: false, credentialsId: 'my-creds-Id', poll: false, url: "${GIT_URL}" } } stage("Build") { agent any steps{ echo "Starting Build stage" } } } }
关键修改点说明
- 全局改为
agent none,仅需要执行shell命令的阶段单独配置agent any,避免input阶段长时间占用执行器资源 - 预处理分支列表,去掉
remotes/origin前缀、HEAD指针、空行和多余空格,保证choice参数的选项格式符合Jenkins要求 - input步骤显式返回参数值,避免不同版本Jenkins的解析兼容问题
- checkout阶段明确引用
env.branchToDeploy变量,避免变量作用域问题
内容的提问来源于stack exchange,提问作者Shanil
相关产品推荐
相关产品推荐

