Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Commit ID in CodePipeline

I am using CodePipeline with CodeCommit. Builds are triggered automatically with push to master branch. In CodePipeline console it is clearly visible that i am receiving commit id but i need to get it in the build environment so i can add them as a tag to the ECS image when i build it. Is there a way to get in in build environment. This is the id i am looking for

like image 322
Nephilimrising Avatar asked Nov 13 '17 12:11

Nephilimrising


People also ask

How do I find my parent commit ID CodeCommit?

To view information about a commitRun the aws codecommit get-commit command, specifying: The name of the CodeCommit repository (with the --repository-name option). The full commit ID.

How do I get AWS CodePipeline Arn?

View the pipeline ARN and service role ARN (console)Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home . The names of all pipelines associated with your AWS account will be displayed.

What is artifact in CodePipeline?

Artifacts are the files that are worked on by actions in the pipeline. See the action configuration for each action for details about artifact parameters. For example, the S3 source action artifact is a file name (or file path), and the files are generally provided as a ZIP file.


5 Answers

Adding an answer that explains how to achieve this in CloudFormation, as it took me a while to figure it out. You need to define your stage as:

Name: MyStageName
Actions:
    -
        Name: StageName
        InputArtifacts:
            - Name: InputArtifact
        ActionTypeId:
            Category: Build
            Owner: AWS
            Version: '1'
            Provider: CodeBuild
        OutputArtifacts:
            - Name: OutputArtifact
        Configuration:
            ProjectName: !Ref MyBuildProject
            EnvironmentVariables:
                '[{"name":"COMMIT_ID","value":"#{SourceVariables.CommitId}","type":"PLAINTEXT"}]'

In your actions you need to have this kind of syntax. Note that the EnvironmentVariables property of a CodePipeline stage is different from a AWS::CodeBuild::Project property. If you were to add #{SourceVariables.CommitId} as an env variable there, it wouldn't be resolved properly.

like image 183
Bar Avatar answered Oct 10 '22 19:10

Bar


You can use the CODEBUILD_RESOLVED_SOURCE_VERSION environment variable to retrieve the commit hash displayed in CodePipeline at build time.

like image 37
Unsigned Avatar answered Oct 10 '22 18:10

Unsigned


Additionally to @Bar's answer: just adding EnvironmentVariables is not enough, you need to set Namespace also.

For example:

      pipeBackEnd:
        Type: AWS::CodePipeline::Pipeline
        Properties:
          ...
          Stages:
            - Name: GitSource
              Actions:
                - Name: CodeSource
                  ActionTypeId:
                    Category: Source
                    ...
                  Configuration: (...)
                  Namespace: SourceVariables  # <<< === HERE, in Source
            - Name: Deploy
              Actions:
                - Name: BackEnd-Deploy
                  ActionTypeId:
                    Category: Build
                    Provider: CodeBuild (...)
                  Configuration:
                    ProjectName: !Ref CodeBuildBackEnd
                    EnvironmentVariables: '[{"name":"BranchName","value":"#{SourceVariables.BranchName}","type":"PLAINTEXT"},{"name":"CommitMessage","value":"#{SourceVariables.CommitMessage}","type":"PLAINTEXT"}]'

Also, it may be useful: list of CodePipeline variables

like image 36
Putnik Avatar answered Oct 10 '22 19:10

Putnik


CodePipeline now also allows you to configure your pipeline with variables that are generated at execution time. In this example your CodeCommit action will produce a variable called CommitId that you can pass into a CodeBuild environment variable via the CodeBuild action configuration.

Here is a conceptual overview of the feature: https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-variables.html

For an example walk through of passing the commit id into your build action you can go here: https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-variables.html

It would also be worth considering tagging the image with the CodePipeline execution id instead of the commit id, that way it prevents future builds with the same commit from overwriting the image. Using the CodePipeline execution id is also shown in the example above.

like image 4
ccundiff Avatar answered Oct 10 '22 19:10

ccundiff


Is this what you're looking for?

http://docs.aws.amazon.com/codepipeline/latest/userguide/monitoring-source-revisions-view.html#monitoring-source-revisions-view-cli

Most (if not all) of the language SDKs have this API built in also.

like image 3
smcstewart Avatar answered Oct 10 '22 20:10

smcstewart