Adding a Jenkins "Are you sure ..." dialog

10,780

Solution 1

You could add an "Are you sure?" Parameter to the build. When a user hits "Build Now" they will be asked to enter the parameter, which could be a choice "Yes/No" or string. You could then check this parameter via a shell or batch step and "exit 1" if it is not set to Yes.

Solution 2

I have implemented this exact feature on our Jenkins instance. The way I have accomplished this is by using the "This build is parameterized" feature to add a build parameter that can be used to determine whether commands should be run.

On the job configuration page, select "This build is parameterized", then Add Choice Parameter (you can also use any other type of parameter, just update the IF statement below as appropriate). Enter a name (no spaces or special characters), choices of no and yes (top one is default), and an optional description. Build parameter screenshot


Now you can add an "Execute shell" build step that checks the value of AreYouSure to see if you want to go through with executing the full build. If the value is not "yes" then exit with code 1 so Jenkins reports a build error. All steps below the check will not be executed if the user did not select "yes".

Here is the code to check the variable value:

if [ "${AreYouSure}" = "yes" ]
then
    ##commands to execute
else
    exit 1
fi

Shell script screenshot

Solution 3

If you are using a pipeline, this is something to go in your Jenkinsfile (see https://jenkins.io/doc/pipeline/steps/pipeline-input-step/ )

def confirmDialog = "Release ${chosenApp}:${chosenVersion} now?"
releaseApprover = input message: confirmDialog,
        submitterParameter: 'releaseApprover'
echo "${releaseApprover} is releasing ${chosenApp}:${chosenVersion}"

You get "OK" and "Abort" buttons. You should put it inside a timeout call as well to prevent it blocking nodes for days.

Share:
10,780
badgerduke
Author by

badgerduke

Updated on August 01, 2022

Comments

  • badgerduke
    badgerduke over 1 year

    I have some Jenkins jobs which affect production servers. It would be nice to have an "Are you sure you want to do this?" dialog when a user runs one of these jobs. I have not found a plugin for this. Has anybody out there tried to do this?

  • Jamel Toms
    Jamel Toms about 8 years
    Care to elaborate?
  • zozo6015
    zozo6015 over 6 years
    Not a correct way beause if you don't choose yes then it will still start the job but end up as a failure.
  • gaoithe
    gaoithe about 6 years
    You can use exit 0 to have jobs which are not run appear as success. You could do something more elaborate using PostBuildTask e.g. echo parameter value. Check the log using Textfinder plugin and set build result to Aborted or whatever you desire.
  • tomasat
    tomasat almost 2 years
    This is a very simple way to achieve the prompt. Simply check "This project is parameterized" Add a string parameter. Give the parameter a name and description. Save. The next time you build you will need to choose "Build with Parameters" and then be shown the parameters and need to choose Build. Serve as a prompt for our purposes.