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

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

    • 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.

          [JENKINS-61259] Set environment variables for build ID, artifacts path, etc. on success and failure

          Varun Gore added a comment -

          Hi Bryan,

          Could you please elaborate your use case. Is the goal to download build artifacts locally? The plugin downloads all the build artifacts to jenkins workspace(but only if the build succeeds. We can extend it to download build artifacts irrespective of build status).

          Varun Gore added a comment - Hi Bryan, Could you please elaborate your use case. Is the goal to download build artifacts locally? The plugin downloads all the build artifacts to jenkins workspace(but only if the build succeeds. We can extend it to download build artifacts irrespective of build status).

          Bryan Burke added a comment -

          Hi vgamz,

          Yes, our main use case is to download the artifacts (if they exist) into the Jenkins workspace for the job, even if the build failed. It would also be nice to have relevant metadata available from the CodeBuild plugin after execution of the `awsCodeBuild` pipeline step without having to wrap the step in a script block to inspect the returned object.

          Bryan Burke added a comment - Hi vgamz , Yes, our main use case is to download the artifacts (if they exist) into the Jenkins workspace for the job, even if the build failed. It would also be nice to have relevant metadata available from the CodeBuild plugin after execution of the `awsCodeBuild` pipeline step without having to wrap the step in a script block to inspect the returned object.

          Varun Gore added a comment -

          Hi Bryan,

          While we are working on plugin to download artifacts even if the build fails, could you please try this workaround to get artifact path:

          def cbResult = null
          pipeline {
              agent any
              stages {
                  stage('Build') {
                      steps {
                          script {
                              try {
                                  cbResult = awsCodeBuild credentialsType: .......
                              } catch (Exception cbEx) {
                                  cbResult = cbEx.getCodeBuildResult()
                              }
                          
                              echo cbResult.getArtifactsLocation()
                          }
                      }
                  }
              }
              post {
                  always {
                      script {
                          echo cbResult.getArtifactsLocation()
                      }
                  }
              }
          }
          

          Varun Gore added a comment - Hi Bryan, While we are working on plugin to download artifacts even if the build fails, could you please try this workaround to get artifact path: def cbResult = null pipeline { agent any stages { stage( 'Build' ) { steps { script { try { cbResult = awsCodeBuild credentialsType: ....... } catch (Exception cbEx) { cbResult = cbEx.getCodeBuildResult() } echo cbResult.getArtifactsLocation() } } } } post { always { script { echo cbResult.getArtifactsLocation() } } } }

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

              Created:
              Updated: