DEV Community

Arseny Zinchenko
Arseny Zinchenko

Posted on • Originally published at rtfm.co.ua on

Jenkins: Scripted Pipeline – Production environment job confirmation step

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.

Similar posts

Top comments (5)

Collapse
 
david_j_eddy profile image
David J Eddy

Automation and CI/CD does not get enough love here on dev.to. Know I am reading though. ;)

This really inspires me to write about some of my experience with Jenkins recently. Especially the Config-as-Code and declarative pipelines using Python libraries as helpers.

Collapse
 
setevoy profile image
Arseny Zinchenko

Thanks, David)
Well, as I'm doing more DevOops tasks - so and posts are more about it.
Anyway - will try to add different topics here.

Collapse
 
jeikabu profile image
jeikabu

Sorry, but "DevOops" made me chuckle. I realize it's a typo, but I may need to start using that.

I too agree with David. I throw in a Jenkins reference every now and then, but I've started to wonder if Jenkins is passe. Had a partial post about our experience over last 2 years and abandoned it because it was mostly ranting about everything we got wrong...

Thread Thread
 
setevoy profile image
Arseny Zinchenko

Hi, jeikabu.

No, in fact, "DevOops" is not a typo)
Can't remember where I heard it for the first time but I liked it and use sometimes to make something a bit less serious.

Regarding Jenkins - I like it. Its Pipelines really cool and comfortable for me so I still will prefer it over any other CI/CD tools although I have experience with TeamCity/Bamboo/GoCD/Travis.

Collapse
 
david_j_eddy profile image
David J Eddy

I am enjoying the DevOops articles, keep them coming!