Blairg23
Blairg23

Reputation: 12045

How do you pass environment variables from the Source to the Build in AWS CodePipelines?

In AWS CodeBuild, it's incredibly easy to pass environment variables, as shown in the documentation. If I want to get the event trigger reference, I can use the variable CODEBUILD_WEBHOOK_TRIGGER, which is context-sensitive: for a GitHub PUSH event, this will be the branch name, but for a PULL_REQUEST_CREATED or PULL_REQUEST_UPDATED event, this will be the PR number.

So the problem is this: when using AWS CodePipeline, the CodeBuild project "source" is the CodePipeline instead of the GitHub webhook. Suddenly, CODEBUILD_WEBHOOK_TRIGGER is an empty string and doesn't seem to know about anything about the original GitHub webhook event that triggered the CodePipeline.

How does one access those environment variables using a CodeBuild project that is triggered by a CodePipeline? It seems to be a use case that AWS overlooked, so it might be a bug. Unfortunately, very difficult to submit a bug report with only a basic access account.

Upvotes: 2

Views: 2624

Answers (1)

shariqmaws
shariqmaws

Reputation: 8890

You are correct. In this particular case, CodePipeline is the one making start-build API call to start the build. CODEBUILD_WEBHOOK_TRIGGER is CodeBuild specific and will only be set when the webhook invokes CodeBuild.

If you want to know the webhook that triggered pipeline, you can use list-webhooks [1] API call with additional filters based on pipeline name to get the webhook details.

Ref: [1] https://docs.aws.amazon.com/cli/latest/reference/codepipeline/list-webhooks.html

Edit 1:

I was wrong that list-webhooks will get you the required information. I did some tests and it only gives you the list of webhooks defined for the Source action.

The closest I can get is using "list-pipeline-executions" [2] CLI call in your CodeBuild buildspec.

If you run this command:

$ aws codepipeline list-pipeline-executions --pipeline-name <Pipeline_Name> --region us-east-1 --max-items 1

It will give you output similar to this:

{
    "pipelineExecutionSummaries": [
        {
            "pipelineExecutionId": "ccdd87a0-41e4-4489-9332-0720dc526b37",
            "status": "InProgress",
            "startTime": 1573037463.245,
            "lastUpdateTime": 1573037463.245,
            "sourceRevisions": [
                {
                    "actionName": "Source",
                    "revisionId": "4d3bcb17e4a71e3d4bf15215954172639716c326",
                    "revisionSummary": "Merge pull request #3 from shariqmus/readme-edits\n\nUpdate Code.py",
                    "revisionUrl": "https://github.com/shariqmus/hello-world/commit/4d3bcb17e4a71e3d4bf15215954172639716c326"
                }
            ]
        }
    ],
    "NextToken": "eyJuZXh0VG9rZW4iOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAxfQ=="
}

The 'revisionSummary' has the PR details. You can filter this value using 'jq' [3], so the command in your build spec will look something like:

  1. Make sure your CodeBuild project's service role has permission to do 'ListPipelineExecutions' on the Pipeline
  2. Add the following in Buildspec 'Install' phase:
    • apt-get install jq
  3. Add the following in Buildspec where you need to get the commit message:
    • COMMIT_MSG=$(aws codepipeline list-pipeline-executions --pipeline-name --max-items 1 | jq -r '.pipelineExecutionSummaries[0].sourceRevisions[0].revisionSummary')
    • echo $COMMIT_MSG

I hope this answer was helpful.

Ref:

[2] https://docs.aws.amazon.com/cli/latest/reference/codepipeline/list-pipeline-executions.html

[3] https://stedolan.github.io/jq/

Upvotes: 1

Related Questions