Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile nested stage(s) throwing error

I have the following Jenkinsfile which I believe is setup correctly. I used https://jenkins.io/doc/book/pipeline/syntax/#sequential-stages as an example but for some reason when I run this in jenkins I am receiving,

WorkflowScript: 11: Unknown stage section "stages". Starting with version 0.5, steps in a stage must be in a steps block

Can someone tell me what I am missing or doing wrong?

pipeline {
    agent {label 'windows'}

    stages {
        stage('Quick Build') {
            steps {
                echo 'Building'
            }
        }
        stage('Deploy to Dev') {
            // when {
            //     branch 'develop' 
            // }
            stages {
                stage('Building Distributable Package') {
                    steps {
                        echo 'Building'
                    }
                }
                stage('Archiving Package') {
                    steps {
                        echo 'Archiving Aritfacts'
                        archiveArtifacts artifacts: '/*.zip', fingerprint: true
                    }
                }
                stage('Deploying Dev') {
                    steps {
                        echo 'Deploying'
                        timeout(time:3, unit:'DAYS') {
                            input message: "Approve build?"
                        }
                    }
                }
            }

        }
        stage('Deploy to Test') {
            when {
                branch 'develop' 
            }
            steps {
                echo 'deploying..'
                timeout(time:3, unit:'DAYS') {
                    input message: "Approve build?"
                }
            }
        }
        stage('Deploy to Prod') {
            when {
                branch 'release' 
            }
            steps {
                timeout(time:3, unit:'DAYS') {
                    input message: "Deploy to Prod?"
                }
                echo 'Deploying....'
            }
        }
    }
}

Thanks in advance!

like image 535
Grady D Avatar asked Jul 31 '18 14:07

Grady D


People also ask

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)

How do I catch exceptions in Jenkins pipeline?

try/catch is scripted syntax. So any time you are using declarative syntax to use something from scripted in general you can do so by enclosing the scripted syntax in the scripts block in a declarative pipeline. So your try/catch should go inside stage >steps >script.

How do you skip stage in Jenkins scripted pipeline?

So to mark a stage as skipped you need to call static method markStageSkippedForConditional passing the name of the stage you are skipping. If you're using a version of Jenkins older than mid 2019, you must uncheck Use Groovy Sandbox checkbox, since the Utils method was not yet whitelisted for external use.

Can we use different nodes for each stage in Jenkinsfile?

You can also mix and match node { stage {..}} and stage { node {..}} within your pipeline codes. By design (in Jenkins, as well as in the concept of continuous delivery), stages do not execute in parallel. Only the steps within a single stage are executed in parallel.


1 Answers

This ended up being a problem in version 2.107.3. Once upgraded to 2.121.2 this functionality started working.

like image 139
Grady D Avatar answered Oct 07 '22 06:10

Grady D