Jenkins流水线input步骤超时后发Slack通知并持续等待用户输入
解决方案:异步定时提醒+持续等待用户输入
要实现15分钟未收到用户输入时发送Slack提醒,且保持等待状态不自动进入下一阶段,不能用timeout直接包裹input(会终止输入等待),而是需要通过异步线程来实现定时提醒,主线程持续等待用户输入。
完整流水线脚本示例
pipeline { agent any stages { stage('A') { steps { script { def reminderThread try { echo 'Starting Stage A' // 启动后台线程,15分钟后发送Slack提醒 reminderThread = Thread.start { sleep(time: 15, unit: 'MINUTES') // 发送Slack消息,需确保已安装Slack Notification插件并配置好凭证 slackSend( channel: '#your-slack-channel', color: 'warning', message: "⚠️ Jenkins流水线[${env.JOB_NAME}]等待您确认进入Stage B,请及时处理!链接:${env.BUILD_URL}" ) } // 主线程持续等待用户输入,无超时限制 input message: 'Continue to the next stage?' } finally { // 用户输入后,若提醒线程还未执行,中断它避免多余通知 if (reminderThread?.isAlive()) { reminderThread.interrupt() } } } } } stage('B') { steps { echo 'Starting Stage B' } } } }
关键部分说明
异步提醒线程:
- 使用
Thread.start启动后台线程,线程内先休眠15分钟,之后执行slackSend发送提醒。 - 需提前安装Jenkins的「Slack Notification Plugin」,并在全局配置中完成Slack的webhook、凭证等设置。
- 使用
主线程持续等待:
- 主线程直接调用无超时限制的
input,确保一直等待用户点击「继续」或「终止」操作。
- 主线程直接调用无超时限制的
线程清理:
- 在
finally块中检查提醒线程状态,若用户在15分钟内完成输入,及时中断线程避免发送不必要的提醒。
- 在
替代方案(使用Parallel分支)
如果不想用原生线程,也可以借助Jenkins的parallel语法实现,一个分支负责定时提醒,另一个分支持续等待输入:
pipeline { agent any stages { stage('A') { steps { script { echo 'Starting Stage A' parallel( reminder: { sleep(time: 15, unit: 'MINUTES') slackSend( channel: '#your-slack-channel', color: 'warning', message: "⚠️ Jenkins流水线[${env.JOB_NAME}]等待确认,链接:${env.BUILD_URL}" ) }, waitForInput: { input message: 'Continue to the next stage?' }, failFast: false // 确保一个分支完成后,另一个分支仍持续执行 ) } } } stage('B') { steps { echo 'Starting Stage B' } } } }
内容的提问来源于stack exchange,提问作者user21150497
相关产品推荐
相关产品推荐

