Jenkins 在声明性管道上的输入
Jenkins input on declarative pipeline
请问是否可以获取用户输入和运行基于用户选择内容的脚本?逻辑应该是
IF User choose Proceed THEN run a script (in my case promote or Blue/Green deployment)
IF User choose Abort THEN don't terminate the jenkins job, run a script (in my case rollback)
这是我正在使用的脚本,但是我不知道如何在验证阶段后应用
pipeline {
agent any
stages {
stage('Deploy') {
steps {
sh """#!/bin/bash +x
echo "performing sts assume role"
//SCRIPT-HERE//
echo "performing ansible deploy"
//SCRIPT-HERE//
"""
}
}
stage('validate') {
steps {
timeout(30) {
script {
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}
}
}
}
}
}
我看到的另一个问题是,虽然这个管道脚本是 运行ning,但在 jenkins 作业控制台输出上无法点击继续/中止,这是一个错误吗?我指的是下图
我能够添加 nandilov 的建议,但似乎逻辑仍然不适用,请告知此处遗漏的内容
pipeline {
agent any
stages {
stage('Deploy') {
steps {
sh """#!/bin/bash +x
echo "performing sts assume role"
echo "performing ansible deploy"
"""
}
}
stage('validate') {
steps {
script {
env.flagError = "false"
try {
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}catch(e){
println "input aborted or timeout expired, will try to rollback."
env.flagError = "true"
}
}
}
}
stage("If user selects Proceed"){
when{
expression { env.inputValue == "value1" }
}
steps{
sh """#!/bin/bash +x
echo "User selected proceed"
"""
}
}
stage("rollback if flag error true"){
when{
expression { env.inputValue == "value2" }
}
steps{
sh """#!/bin/bash +x
echo "User selected Abort"
"""
}
}
}
}
从管道视图来看,它在选择 "Abort" 或 "Proceed"
时从未触发最后两个阶段之一
这些是选择中止或继续时的日志
中止
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-job-lagot
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
performing sts assume role
performing ansible deploy
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (validate)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided
Proceed or Abort
[Pipeline] echo
input aborted or timeout expired, will try to rollback.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (If user selects Proceed)
Stage "If user selects Proceed" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (rollback if flag error true)
Stage "rollback if flag error true" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
继续
Started by user lagot
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-job-lagot
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
performing sts assume role
performing ansible deploy
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (validate)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided
Proceed or Abort
Approved by lagot
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (If user selects Proceed)
Stage "If user selects Proceed" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (rollback if flag error true)
Stage "rollback if flag error true" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
如果您想中止作业并做某事,您可以try/catch它:
try{
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}catch(e){
println "input aborted or timeout expired, will try to rollback."
// execute rollback
}
您也可以在另一个阶段进行:
pipeline {
agent any
stages {
stage('Deploy') {
steps {
sh """#!/bin/bash +x
echo "performing sts assume role"
echo "performing ansible deploy"
"""
}
}
stage('validate') {
steps {
script {
env.flagError = "false"
try {
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}catch(e){
println "input aborted or timeout expired, will try to rollback."
env.flagError = "true"
}
}
}
}
stage("If user selects Proceed"){
when{
expression { env.flagError == "false" }
}
steps{
sh """#!/bin/bash +x
echo "User selected proceed"
"""
}
}
stage("rollback if flag error true"){
when{
expression { env.flagError == "true" }
}
steps{
sh """#!/bin/bash +x
echo "User selected Abort"
"""
}
}
}
试试这个,您需要单击“继续”,然后在 "deploy" 和 "rollback" 之间进行选择。我们会将结果存储在环境变量中,您可以将其用作下一阶段的条件
stage('validate') {
steps {
timeout(30) {
script {
CHOICES = ["deploy", "rollback"];
env.yourChoice = input message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok : 'Proceed',id :'choice_id',
parameters: [choice(choices: CHOICES, description: 'Do you want to deploy or to rollback?', name: 'CHOICE'),
string(defaultValue: 'rollback', description: '', name: 'rollback value')]
}
}
}
}
}
stage('Deploy') {
when {
expression { env.yourChoice == 'deploy' }
}
steps {
...
}
}
stage('Rollback') {
when {
expression { env.yourChoice == 'rollback' }
}
steps {
...
}
}
请问是否可以获取用户输入和运行基于用户选择内容的脚本?逻辑应该是
IF User choose Proceed THEN run a script (in my case promote or Blue/Green deployment) IF User choose Abort THEN don't terminate the jenkins job, run a script (in my case rollback)
这是我正在使用的脚本,但是我不知道如何在验证阶段后应用
pipeline {
agent any
stages {
stage('Deploy') {
steps {
sh """#!/bin/bash +x
echo "performing sts assume role"
//SCRIPT-HERE//
echo "performing ansible deploy"
//SCRIPT-HERE//
"""
}
}
stage('validate') {
steps {
timeout(30) {
script {
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}
}
}
}
}
}
我看到的另一个问题是,虽然这个管道脚本是 运行ning,但在 jenkins 作业控制台输出上无法点击继续/中止,这是一个错误吗?我指的是下图
我能够添加 nandilov 的建议,但似乎逻辑仍然不适用,请告知此处遗漏的内容
pipeline {
agent any
stages {
stage('Deploy') {
steps {
sh """#!/bin/bash +x
echo "performing sts assume role"
echo "performing ansible deploy"
"""
}
}
stage('validate') {
steps {
script {
env.flagError = "false"
try {
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}catch(e){
println "input aborted or timeout expired, will try to rollback."
env.flagError = "true"
}
}
}
}
stage("If user selects Proceed"){
when{
expression { env.inputValue == "value1" }
}
steps{
sh """#!/bin/bash +x
echo "User selected proceed"
"""
}
}
stage("rollback if flag error true"){
when{
expression { env.inputValue == "value2" }
}
steps{
sh """#!/bin/bash +x
echo "User selected Abort"
"""
}
}
}
}
从管道视图来看,它在选择 "Abort" 或 "Proceed"
时从未触发最后两个阶段之一这些是选择中止或继续时的日志
中止
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-job-lagot
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
performing sts assume role
performing ansible deploy
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (validate)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided
Proceed or Abort
[Pipeline] echo
input aborted or timeout expired, will try to rollback.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (If user selects Proceed)
Stage "If user selects Proceed" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (rollback if flag error true)
Stage "rollback if flag error true" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
继续
Started by user lagot
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-job-lagot
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
performing sts assume role
performing ansible deploy
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (validate)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided
Proceed or Abort
Approved by lagot
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (If user selects Proceed)
Stage "If user selects Proceed" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (rollback if flag error true)
Stage "rollback if flag error true" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
如果您想中止作业并做某事,您可以try/catch它:
try{
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}catch(e){
println "input aborted or timeout expired, will try to rollback."
// execute rollback
}
您也可以在另一个阶段进行:
pipeline {
agent any
stages {
stage('Deploy') {
steps {
sh """#!/bin/bash +x
echo "performing sts assume role"
echo "performing ansible deploy"
"""
}
}
stage('validate') {
steps {
script {
env.flagError = "false"
try {
input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}catch(e){
println "input aborted or timeout expired, will try to rollback."
env.flagError = "true"
}
}
}
}
stage("If user selects Proceed"){
when{
expression { env.flagError == "false" }
}
steps{
sh """#!/bin/bash +x
echo "User selected proceed"
"""
}
}
stage("rollback if flag error true"){
when{
expression { env.flagError == "true" }
}
steps{
sh """#!/bin/bash +x
echo "User selected Abort"
"""
}
}
}
试试这个,您需要单击“继续”,然后在 "deploy" 和 "rollback" 之间进行选择。我们会将结果存储在环境变量中,您可以将其用作下一阶段的条件
stage('validate') {
steps {
timeout(30) {
script {
CHOICES = ["deploy", "rollback"];
env.yourChoice = input message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok : 'Proceed',id :'choice_id',
parameters: [choice(choices: CHOICES, description: 'Do you want to deploy or to rollback?', name: 'CHOICE'),
string(defaultValue: 'rollback', description: '', name: 'rollback value')]
}
}
}
}
}
stage('Deploy') {
when {
expression { env.yourChoice == 'deploy' }
}
steps {
...
}
}
stage('Rollback') {
when {
expression { env.yourChoice == 'rollback' }
}
steps {
...
}
}