1

Simple question I got input in declarative pipeline in jenkins. When I click abort on prompt I do not want it to mark build as aborted. To prevent answers that stack already have, I am looking for solution in declarative pipeline, without escaping to scripting.


 options {
    timeout(time: 1, unit: 'HOURS')
 }

 steps {
   input 'Deploy to UAT?'
   deploy()
 }

 post {
   aborted {
     script {
       //Throws exception(not allowed to use rawBuild)
       currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
       //Do not change status because it can only be worse
       currentBuild.result = 'SUCCESS'
       //Do not change status because it can only be worse
       currentBuild.currentResult = 'SUCCESS'
     }
   }
 }

Tomasz Kryński
  • 513
  • 4
  • 23

2 Answers2

2

I don't think it's possible to not abort pipeline with the simple input field since that the purpose of it.

What You could do is to use checkbox in the input like


def deployToUat
steps {
    script {
        deployToUat = input(
                id: 'Proceed', message: 'Deploy to UAT?', parameters: [
                [$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Proceed with deployment?']
        ])
    }
}

stage('UAT deployment') {
    when {
        expression { deployToUat == true }
    }
    steps {
        deploy()
    }
}
0

Well, you could do

script {
    try {
        input 'Deploy to UAT?'
    } catch(err) {
       currentBuild.result = 'SUCCESS'
       return
    }
}

I guess the above is the only correct way since the result can only worsen.

public void setResult(@Nonnull Result r) {
    if (state != State.BUILDING) {
        throw new IllegalStateException("cannot change build result while in " + state);
    }

    // result can only get worse
    if (result==null || r.isWorseThan(result)) {
        result = r;
        LOGGER.log(FINE, this + " in " + getRootDir() + ": result is set to " + r, LOGGER.isLoggable(Level.FINER) ? new Exception() : null);
    }
}
hakamairi
  • 4,464
  • 4
  • 30
  • 53
  • First thing there is not try/catch in declarative pipeline, second if you read comments, setting result this way do not work in declarative pipeline – Tomasz Kryński May 30 '19 at 13:16
  • Ah, I assumed you know the workarounds. Updated my answer. Also giving random people that want to help downvotes does feel abusive. Not to mention that it would help to see the exception you get as well. – hakamairi May 30 '19 at 14:10
  • Thats the thing, I am looking for 'proper' solution not workarounds with script. I wasn't able to find anything, your answer is same as many on stack for this topic, non of them answer my question, is that possible to do this in declarative pipeline, not get down to scripting. I don't think it is abusive, if you are giving answer that I can already find on stack. – Tomasz Kryński May 30 '19 at 15:31
  • Wait a second, you wanted to handle input's abort, which is provided above ;) Also you are already using `script`. – hakamairi May 31 '19 at 09:11