Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-61259

Set environment variables for build ID, artifacts path, etc. on success and failure

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor Minor
    • aws-codebuild-plugin
    • None

      To be easier to use in declarative pipelines, the aws-codebuild plugin could set environment variables for build ID, artifacts location, and other relevant information. The plugin could set the variables for both successful and failed builds.

      Currently, to get this information in a declarative pipeline, you must embed the pipeline step inside a script block:

      pipeline {
          agent any
          stages {
              stage('Build') {
                  steps {
                      script {
                          def cbResponse = awsCodeBuild credentialsType: 'keys', projectName: 'hello-world', region: 'us-east-1', sourceControlType: 'jenkins'
                          env.ARTIFACTS_PATH = cbResponse.getArtifactsLocation().split('/', 2)[-1]
                      }
                  }
              }
          }
          post {
              always {
                  sh 'rm -fr artifacts'
                  s3Download bucket: 'my-artifacts-bucket', file: 'artifacts.zip', force: true, path: "${env.ARTIFACTS_PATH}", payloadSigningEnabled: true
                  unzip dir: 'artifacts', zipFile: 'artifacts.zip'
                  junit testResults: 'artifacts/build/reports/junit.xml'
              }
          }
      }
      

      Additionally, if the CodeBuild build fails, the returned object's getArtifactsLocation() method returns null, so it becomes impossible to download the artifacts from S3 (for example, to publish a failed test report to JUnit).

      If the plugin were to set environment variables instead (for both success and failure), the above pipeline might look like this:

      pipeline {
          agent any
          stages {
              stage('Build') {
                  steps {
                      awsCodeBuild credentialsType: 'keys', projectName: 'hello-world', region: 'us-east-1', sourceControlType: 'jenkins'
                  }
              }
          }
          post {
              always {
                  sh 'rm -fr artifacts'
                  echo "CodeBuild build ${env.CB_BUILD_ID} completed with status ${env.CB_BUILD_STATUS}."
                  echo "Downloading artifacts from ${env.CB_ARTIFACTS_ARN}..."
                  s3Download bucket: "${env.CB_ARTIFACTS_LOCATION}", file: "${env.CB_ARTIFACTS_NAME}", force: true, path: "${env.CB_ARTIFACTS_PATH}", payloadSigningEnabled: true
                  unzip dir: 'artifacts', zipFile: "${env.CB_ARTIFACTS_NAME}"
                  junit testResults: 'artifacts/build/reports/junit.xml'
              }
          }
      }
      

      Regardless of success or failure, the post block would have access to all the information it needs to download the artifacts.

            subinataws Subin Mathew
            bryanburke Bryan Burke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: