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

8 views
Skip to first unread message

bryan.burke@cgifederal.com (JIRA)

unread,
Feb 27, 2020, 10:39:03 AM2/27/20
to jenkinsc...@googlegroups.com
Bryan Burke created an issue
 
Jenkins / New Feature JENKINS-61259
Set environment variables for build ID, artifacts path, etc. on success and failure
Issue Type: New Feature New Feature
Assignee: Subin Mathew
Components: aws-codebuild-plugin
Created: 2020-02-27 15:38
Priority: Minor Minor
Reporter: Bryan Burke

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: 'artifacts.zip', force: true, path: "${env.CB_ARTIFACTS_PATH}", payloadSigningEnabled: true
            unzip dir: 'artifacts', zipFile: 'artifacts.zip'
            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.

Add Comment Add Comment
 
This message was sent by Atlassian Jira (v7.13.12#713012-sha1:6e07c38)
Atlassian logo

bryan.burke@cgifederal.com (JIRA)

unread,
Feb 27, 2020, 10:41:02 AM2/27/20
to jenkinsc...@googlegroups.com
Bryan Burke updated an issue
Change By: Bryan Burke
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:

{code:groovy}

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'
        }
    }
}
{code}

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:

{code:groovy}

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: 'artifacts "${env . zip' CB_ARTIFACTS_NAME}" , force: true, path: "${env.CB_ARTIFACTS_PATH}", payloadSigningEnabled: true
            unzip dir: 'artifacts', zipFile:
'artifacts "${env . zip' CB_ARTIFACTS_NAME}"
            junit testResults: 'artifacts/build/reports/junit.xml'
        }
    }
}
{code}


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

varungor@amazon.com (JIRA)

unread,
Mar 3, 2020, 1:49:03 PM3/3/20
to jenkinsc...@googlegroups.com
Varun Gore commented on New Feature JENKINS-61259
 
Re: Set environment variables for build ID, artifacts path, etc. on success and failure

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@cgifederal.com (JIRA)

unread,
Mar 3, 2020, 1:54:03 PM3/3/20
to jenkinsc...@googlegroups.com

Hi Varun Gore,

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.

varungor@amazon.com (JIRA)

unread,
Mar 3, 2020, 4:04:03 PM3/3/20
to jenkinsc...@googlegroups.com

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()
            }
        }
    }
}
Reply all
Reply to author
Forward
0 new messages