We have jobs which have to be executed on a Dev and Production environments via a Scripted Pipeline.
In such jobs, there are tasks to execute CloudFormation upgrades on an infrastructure or Ansible playbooks to update servers configuration.
To avoid an accidental execution of a Production job I want to have some confirmation step before this job will be started and will do any changes in an environment.
Let’s use pipeline-input-step
and its class BooleanParameterDefinition
.
Add a verify()
function:
def verify() { stage('Verify') { def userInput = input( id: 'userInput', message: 'This is PRODUCTION!', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: '', name: 'Please confirm you sure to proceed'] ]) if(!userInput) { error "Build wasn't confirmed" } } }
In the if(!userInput)
condition here the userInput
value will be check for true or false, by default the false will be set here.
Now add this function execution:
node { switch("${env.LIMIT}") { case "backend-production": verify() echo "Run job" break; default: echo "Dev deploy" break; } }
Here we checking the LIMIT
parameter value which can contain a backend-production, backend-dev, or backend-test.
If its value will be equal to the backend-production – then the verify()
first will be executed and only after it – other build/deploy steps.
If any other – then just tasks will be started.
Let’s check it – start a job:
User input request appeared:
Set mark, press Proceed:
And build is started:
Now let’s re-run it but at this time let’s “forget” to set a mark and just press Proceed:
Here the if(!userInput)
was applied and the result is the error
.
And last check – with the Abort:
Done.