Home > Software design >  jenkins question - Adding post section to script based & difference between methods
jenkins question - Adding post section to script based & difference between methods

Time:05-12

I have 2 questions regarding jenkins

1. What is the difference between the 2 methods?

Method 1

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    } 

Method 2

node ("jenkins-nodes") {
    stage("git clone"){
        echo 'Hello World'    }
    }
  1. As I understand in the first method I can add Post section that will be running regardless of the result of the job. I wish to add the same post section for the second method, but it is not working. Any ideas?

CodePudding user response:

What is the difference between the 2 methods?

As Noam Helmer wrote, pipeline{} is declarative syntax and and node{} is scripted syntax.

In general I recommend to always use pipeline{} as it makes common tasks easier to write and visualization using Blue Ocean plugin works best with declarative pipeline.

When declarative pipeline becomes too unflexible, you can insert scripted blocks using the script{} step in a declarative pipeline:

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                script {
                    echo 'Hello World'               
                }
            }
        }
    }
}

A cleaner approach is to define a function, which is scripted by definition and use that as a custom step in declarative pipeline. Note this works even without script{}!

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                myStep 42
            }
        }
    }
}

void myStep( def x ) {
    echo "Hello World $x"        // prints "Hello World 42"
}

In complex pipelines that use lots of custom code, I usually have one function per stage. This keeps the pipeline{} clean and makes it easy to see the overall structure of the pipeline, without script{} clutter all over the place.

As I understand in the first method I can add Post section that will be running regardless of the result of the job. I wish to add the same post section for the second method, but it is not working. Any ideas?

post{} is only available in declarative pipeline, but not within a scripted pipeline or a scripted section of a declarative pipeline. You can use try{} catch{} finally{} instead. catch{} runs only when an error occurs, finally{} always runs. You can use both or either of catch{} and finally{}.

node ("jenkins-nodes") {
    stage("git clone"){
        echo 'Hello World'    
        try {
            // some steps that may fail
        }
        catch( Exception e ) {
            echo "An error happened: $e"
            // Rethrow exception, to let the build fail.
            // If you remove "throw", the error would be ignored by Jenkins.
            throw  
        }
        finally {
            echo "Cleaning up some stuff"
        }
    }
}
  • Related