Trigger an action within Jenkins declarative pipeline right after a stage ends or just before a stage begins?











up vote
1
down vote

favorite












I have the following Jenkinsfile:



pipeline {
agent any
environment { }
stages {
stage('stageA') {
steps {
... Do something with arg1, arg2 or arg3
}
}
stage('stageB') {
steps {
... Do something with arg1, arg2 or arg3
}
}
...
}
}


Is there anywhere I can specify a universal "pre-stage" or "post-stage" set of actions to perform? A use-case would be sending logging information at the end of a stage to a log manager, but it would be preferable to not copy and paste those invocations at the end of each and every stage.










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have the following Jenkinsfile:



    pipeline {
    agent any
    environment { }
    stages {
    stage('stageA') {
    steps {
    ... Do something with arg1, arg2 or arg3
    }
    }
    stage('stageB') {
    steps {
    ... Do something with arg1, arg2 or arg3
    }
    }
    ...
    }
    }


    Is there anywhere I can specify a universal "pre-stage" or "post-stage" set of actions to perform? A use-case would be sending logging information at the end of a stage to a log manager, but it would be preferable to not copy and paste those invocations at the end of each and every stage.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have the following Jenkinsfile:



      pipeline {
      agent any
      environment { }
      stages {
      stage('stageA') {
      steps {
      ... Do something with arg1, arg2 or arg3
      }
      }
      stage('stageB') {
      steps {
      ... Do something with arg1, arg2 or arg3
      }
      }
      ...
      }
      }


      Is there anywhere I can specify a universal "pre-stage" or "post-stage" set of actions to perform? A use-case would be sending logging information at the end of a stage to a log manager, but it would be preferable to not copy and paste those invocations at the end of each and every stage.










      share|improve this question













      I have the following Jenkinsfile:



      pipeline {
      agent any
      environment { }
      stages {
      stage('stageA') {
      steps {
      ... Do something with arg1, arg2 or arg3
      }
      }
      stage('stageB') {
      steps {
      ... Do something with arg1, arg2 or arg3
      }
      }
      ...
      }
      }


      Is there anywhere I can specify a universal "pre-stage" or "post-stage" set of actions to perform? A use-case would be sending logging information at the end of a stage to a log manager, but it would be preferable to not copy and paste those invocations at the end of each and every stage.







      jenkins jenkins-declarative-pipeline






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 17:52









      Sean Pianka

      855822




      855822
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          As far as I know there is no generic post- or pre-stage hook in Jenkins pipelines. You can define post steps in a post section but you need one per stage.



          However, if you don't want to repeat yourself, you have some options.



          Use a shared lib



          The place to put repeating code to it a shared library. That way allows you to declare your own steps using Groovy.



          You need another repository to define a shared lib, but apart from that it is a pretty strait forward way and you can reuse the code in all of your Jenkins' pipelines.



          Use a function



          If you declare a function outside of the pipeline, you can call it from any stage. This is not really documented and might be prevented in the future. As far as I understand it messes with the coordination between master and agents. However, it works:



          pipeline {

          agent any

          stages {
          stage ("First") {
          steps {
          writeFile file: "resultFirst.txt", text: "all good"
          }
          post {
          always {
          cleanup "first"
          }
          }
          }
          stage ("Second") {
          steps {
          writeFile file: "resultSecond.txt", text: "all good as well"
          }
          post {
          always {
          cleanup "second"
          }
          }
          }

          post {
          always {
          cleanup "global" // this is only triggered after all stages, not after every
          }
          }
          }
          }

          void cleanup(String stage) {
          echo "cleanup ${stage}"
          archiveArtifacts artifacts: "result*"
          }





          share|improve this answer























          • Are you aware of any plugins which may offer solutions to this feature request?
            – Sean Pianka
            Nov 19 at 19:20






          • 1




            No, I'm not. I guess it is not impossible but I wouldn't know how to declare it in the pipeline for it needs to be outside of the stages. Most plugins I know define additional steps, which can be used in a stage, the environment or that alike.
            – Michael
            Nov 19 at 19:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380180%2ftrigger-an-action-within-jenkins-declarative-pipeline-right-after-a-stage-ends-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          As far as I know there is no generic post- or pre-stage hook in Jenkins pipelines. You can define post steps in a post section but you need one per stage.



          However, if you don't want to repeat yourself, you have some options.



          Use a shared lib



          The place to put repeating code to it a shared library. That way allows you to declare your own steps using Groovy.



          You need another repository to define a shared lib, but apart from that it is a pretty strait forward way and you can reuse the code in all of your Jenkins' pipelines.



          Use a function



          If you declare a function outside of the pipeline, you can call it from any stage. This is not really documented and might be prevented in the future. As far as I understand it messes with the coordination between master and agents. However, it works:



          pipeline {

          agent any

          stages {
          stage ("First") {
          steps {
          writeFile file: "resultFirst.txt", text: "all good"
          }
          post {
          always {
          cleanup "first"
          }
          }
          }
          stage ("Second") {
          steps {
          writeFile file: "resultSecond.txt", text: "all good as well"
          }
          post {
          always {
          cleanup "second"
          }
          }
          }

          post {
          always {
          cleanup "global" // this is only triggered after all stages, not after every
          }
          }
          }
          }

          void cleanup(String stage) {
          echo "cleanup ${stage}"
          archiveArtifacts artifacts: "result*"
          }





          share|improve this answer























          • Are you aware of any plugins which may offer solutions to this feature request?
            – Sean Pianka
            Nov 19 at 19:20






          • 1




            No, I'm not. I guess it is not impossible but I wouldn't know how to declare it in the pipeline for it needs to be outside of the stages. Most plugins I know define additional steps, which can be used in a stage, the environment or that alike.
            – Michael
            Nov 19 at 19:27















          up vote
          1
          down vote



          accepted










          As far as I know there is no generic post- or pre-stage hook in Jenkins pipelines. You can define post steps in a post section but you need one per stage.



          However, if you don't want to repeat yourself, you have some options.



          Use a shared lib



          The place to put repeating code to it a shared library. That way allows you to declare your own steps using Groovy.



          You need another repository to define a shared lib, but apart from that it is a pretty strait forward way and you can reuse the code in all of your Jenkins' pipelines.



          Use a function



          If you declare a function outside of the pipeline, you can call it from any stage. This is not really documented and might be prevented in the future. As far as I understand it messes with the coordination between master and agents. However, it works:



          pipeline {

          agent any

          stages {
          stage ("First") {
          steps {
          writeFile file: "resultFirst.txt", text: "all good"
          }
          post {
          always {
          cleanup "first"
          }
          }
          }
          stage ("Second") {
          steps {
          writeFile file: "resultSecond.txt", text: "all good as well"
          }
          post {
          always {
          cleanup "second"
          }
          }
          }

          post {
          always {
          cleanup "global" // this is only triggered after all stages, not after every
          }
          }
          }
          }

          void cleanup(String stage) {
          echo "cleanup ${stage}"
          archiveArtifacts artifacts: "result*"
          }





          share|improve this answer























          • Are you aware of any plugins which may offer solutions to this feature request?
            – Sean Pianka
            Nov 19 at 19:20






          • 1




            No, I'm not. I guess it is not impossible but I wouldn't know how to declare it in the pipeline for it needs to be outside of the stages. Most plugins I know define additional steps, which can be used in a stage, the environment or that alike.
            – Michael
            Nov 19 at 19:27













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          As far as I know there is no generic post- or pre-stage hook in Jenkins pipelines. You can define post steps in a post section but you need one per stage.



          However, if you don't want to repeat yourself, you have some options.



          Use a shared lib



          The place to put repeating code to it a shared library. That way allows you to declare your own steps using Groovy.



          You need another repository to define a shared lib, but apart from that it is a pretty strait forward way and you can reuse the code in all of your Jenkins' pipelines.



          Use a function



          If you declare a function outside of the pipeline, you can call it from any stage. This is not really documented and might be prevented in the future. As far as I understand it messes with the coordination between master and agents. However, it works:



          pipeline {

          agent any

          stages {
          stage ("First") {
          steps {
          writeFile file: "resultFirst.txt", text: "all good"
          }
          post {
          always {
          cleanup "first"
          }
          }
          }
          stage ("Second") {
          steps {
          writeFile file: "resultSecond.txt", text: "all good as well"
          }
          post {
          always {
          cleanup "second"
          }
          }
          }

          post {
          always {
          cleanup "global" // this is only triggered after all stages, not after every
          }
          }
          }
          }

          void cleanup(String stage) {
          echo "cleanup ${stage}"
          archiveArtifacts artifacts: "result*"
          }





          share|improve this answer














          As far as I know there is no generic post- or pre-stage hook in Jenkins pipelines. You can define post steps in a post section but you need one per stage.



          However, if you don't want to repeat yourself, you have some options.



          Use a shared lib



          The place to put repeating code to it a shared library. That way allows you to declare your own steps using Groovy.



          You need another repository to define a shared lib, but apart from that it is a pretty strait forward way and you can reuse the code in all of your Jenkins' pipelines.



          Use a function



          If you declare a function outside of the pipeline, you can call it from any stage. This is not really documented and might be prevented in the future. As far as I understand it messes with the coordination between master and agents. However, it works:



          pipeline {

          agent any

          stages {
          stage ("First") {
          steps {
          writeFile file: "resultFirst.txt", text: "all good"
          }
          post {
          always {
          cleanup "first"
          }
          }
          }
          stage ("Second") {
          steps {
          writeFile file: "resultSecond.txt", text: "all good as well"
          }
          post {
          always {
          cleanup "second"
          }
          }
          }

          post {
          always {
          cleanup "global" // this is only triggered after all stages, not after every
          }
          }
          }
          }

          void cleanup(String stage) {
          echo "cleanup ${stage}"
          archiveArtifacts artifacts: "result*"
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 19:29

























          answered Nov 19 at 19:14









          Michael

          1,0581712




          1,0581712












          • Are you aware of any plugins which may offer solutions to this feature request?
            – Sean Pianka
            Nov 19 at 19:20






          • 1




            No, I'm not. I guess it is not impossible but I wouldn't know how to declare it in the pipeline for it needs to be outside of the stages. Most plugins I know define additional steps, which can be used in a stage, the environment or that alike.
            – Michael
            Nov 19 at 19:27


















          • Are you aware of any plugins which may offer solutions to this feature request?
            – Sean Pianka
            Nov 19 at 19:20






          • 1




            No, I'm not. I guess it is not impossible but I wouldn't know how to declare it in the pipeline for it needs to be outside of the stages. Most plugins I know define additional steps, which can be used in a stage, the environment or that alike.
            – Michael
            Nov 19 at 19:27
















          Are you aware of any plugins which may offer solutions to this feature request?
          – Sean Pianka
          Nov 19 at 19:20




          Are you aware of any plugins which may offer solutions to this feature request?
          – Sean Pianka
          Nov 19 at 19:20




          1




          1




          No, I'm not. I guess it is not impossible but I wouldn't know how to declare it in the pipeline for it needs to be outside of the stages. Most plugins I know define additional steps, which can be used in a stage, the environment or that alike.
          – Michael
          Nov 19 at 19:27




          No, I'm not. I guess it is not impossible but I wouldn't know how to declare it in the pipeline for it needs to be outside of the stages. Most plugins I know define additional steps, which can be used in a stage, the environment or that alike.
          – Michael
          Nov 19 at 19:27


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380180%2ftrigger-an-action-within-jenkins-declarative-pipeline-right-after-a-stage-ends-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)