Search code examples
jenkinsamazon-s3post-build

Use Jenkins Parametrised Build to control execution of Post-Build Actions


Is there any way that I can use Jenkins Choice Parameters to control the execution of Post-Build jobs in a project?

I have my project which is building a war file and sometimes (not all the times) I would like this war file to be send to Amazon S3 bucket so I can deploy it to an EC2 Tomcat Container. I don't want Jenkins to keep sending the war file to S3 every time it builds the project (which happens many times a day) and I want a method to control when to send the war file to S3.

The only option I can see is to duplicate my current Jenkins project and have the new project send war file to S3 but this means that I will end up having multiple Jenkins projects doing 'almost' exactly the same thing which I am trying to avoid.


Solution

  • nah, don't make a new build plan for that. even declarative pipelines support this use case like no man's business. once you have a parameter (from the parameters step. these will allow you to "Build with parameters" and have an actual person check a checkbox when you want a war shipped off), you can simply reference it via params.MY_PARAM_NAME. just replace the echos below with actual code:

    pipeline {
      agent { label 'docker' }
    
      parameters { booleanParam(name: 'SEND_WAR_TO_S3', defaultValue: false, description: 'Send resulting war file to s3?') }
    
      stages {
        stage('hot_stage') {
          steps {
            echo 'generating war...'
          }
        }
      }
      post {
        always {
          script {
            if (params.SEND_WAR_TO_S3) {
              echo 'sending war to s3'
            } else {
              echo 'not sending war to s3'
            }
          }
        }
      }
    }