Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify an agent for post action in Jenkins declarative pipeline

I want to run the post part of my declarative pipeline inside a docker container. And I don't know how to set the agent only for post.

Sample of my pipeline

post {
        always {
            echo 'This will always run'
        }
        success {
            mail ## my mail content;
        }
}

And I want to run this mail command in a docker container.

like image 600
Gawain Avatar asked Sep 03 '19 08:09

Gawain


People also ask

What is an agent specified while writing the Jenkinsfile Declarative Pipeline?

agent. The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed. The section must be defined at the top-level inside the pipeline block, but stage-level usage is optional.

What is agent in Declarative Pipeline?

In declarative pipelines the agent directive is used for specifying which agent/slave the job/task is to be executed on. This directive only allows you to specify where the task is to be executed, which agent, slave, label or docker image.

How will you define post in Jenkins?

Post Actions are just like other normal stages but that running in specific conditions. Jenkins supports 10 special action conditions which are running when these conditions meet. They are related to run status and can be defined in the post block both for the whole pipeline and per-stage.

What is Post section in Jenkins Pipeline?

Since the post section of a Pipeline is guaranteed to run at the end of a Pipeline's execution, we can add some notification or other steps to perform finalization, notification, or other end-of-Pipeline tasks. See Glossary - Build Status for the different build statuses: SUCCESS, UNSTABLE, and FAILED.


1 Answers

You can use the node(...){...} block within the post steps, with or without the script {...} block as applicable:

post {
    always {
        echo 'This will always run'
    }
    success {
        node('docker') {
            script {
                mail ## my mail content;
            }
        }
    }
}
like image 107
Dibakar Aditya Avatar answered Oct 04 '22 01:10

Dibakar Aditya