How to repeat a stage in Jenkins Workflow

10,481

Solution 1

This is only possible in the enterprise version of Jenkins. As @jesse-glick pointed out, you have the Checkpoint Plugin available there, see the documentation.

There's is currently no plan to support this feature in the OSS-version according to CloudBees. See this issue: JENKINS-33846

Solution 2

Supposing it is the last (Ansible) stage you want to be able to restart from, you could place a checkpoint just before it.

checkpoint 'about to deploy'
stage 'DevOps - Ansible'
input message: 'Release to Production', ok: 'Release'
node {
    // TODO
}

If you want to deploy to a selectable target, you could use input:

checkpoint 'about to deploy'
stage 'DevOps - Ansible'
def target = input message: 'Where to release?',
    parameters: [[$class: 'StringParameterDefinition', name: 'target']]
node {
    // TODO
}

The more complicated scenario is that you want to always deploy to a standard target the first time around, but when resuming from a checkpoint you want to ask the user for an alternate target. For that you need to know when you are resuming. Currently checkpoint does not offer this information directly (CJP-1620 in the CloudBees internal issue tracker), but there is a workaround:

def origBuildNumber = env.BUILD_NUMBER
checkpoint 'about to deploy'
stage 'DevOps - Ansible'
def target
if (origBuildNumber == env.BUILD_NUMBER) { // original
    target = 'production'
} else { // resumed
    target = input message: 'Where to release?',
        parameters: [[$class: 'StringParameterDefinition', name: 'target']]
}
node {
    // TODO
}
Share:
10,481
TechCrunch
Author by

TechCrunch

Updated on July 26, 2022

Comments

  • TechCrunch
    TechCrunch almost 2 years

    I'm trying to use Jenkins on Cloudbees to automate deployment of my software. I setup my workflow as following.

    Jenkins Workflow

    There may be times I want to redeploy. (assuming that stage takes a manual input parameter). How do I do that in workflow ? Here is my Groovy script.

    def src = 'https://git.repo.url/proj.git'
    
    stage 'Build'
    node {
        env.JAVA_HOME="${tool name: 'Pre-Installed OpenJDK 8 (Latest) on DEV@Cloud nodes', type: 'hudson.model.JDK'}"
        sh 'javac -version'
        git credentialsId: 'abcdef', url: src
        sh 'ant -f build.xml proj.jar report'
    }
    
    stage 'Generate Release Version'
    input message: 'Create Tar and Push to S3', ok: 'Generate Release'
    node {
        // TODO
    }
    
    stage 'QA Approved'
    input message: 'Enter a Tag Name to approve this build and tag in GIT', ok: 'Approve and Tag', parameters: [[$class: 'StringParameterDefinition', defaultValue: '', description: 'Eg: Sprint73', name: 'TAG_NAME']]
    node {
        // TODO
    }
    
    stage 'DevOps - Ansible'
    input message: 'Release to Production', ok: 'Release'
    node {
        // TODO
    }
    

    I tried using Job Chaining using Build Pipeline instead of Workflow so that I can repeat stages, but thats another story with too many jobs.