How to call git commands during AWS CodeBuild
Asked Answered
S

2

7

I need to show the "build version" in my application. For that purpose, I am using the npm package 'git-describe', which fetches the 'distance' of the current Git commit from the last Git tag. It works fine locally where the .git folder is available along with the source code. But on the server, it does not work maybe because the '.git' folder is not part of the deployed code. Therefore, I, added a script in package.json to write the 'distance' to a file so that code can read its value later to show in the application.

I was assuming that the '.git' folder would be available for executing git commands during the CodeBuild. So, I added the above package.json script to the AWS CodeBuild pipeline. But I think I was wrong since the git command does not seem to work on the server.

Can someone please suggest how I can achieve the required functionality?

Somali answered 15/5, 2022 at 5:33 Comment(0)
H
5

When using AWS CodePipeline + CodeBuild, there are differences versus CodeBuild as a standalone service.

Suppose you take the first case, AWS CodePipeline + CodeBuild. To ensure the .git metadata folder exists, you need to enable a full clone through the CodePipeline Source Action. Subsequently, you will need to give your CodeBuild Service Role the IAM permission codecommit:GitPull if using CodeCommit, or codestar-connections:UseConnection for other SCM providers.

So for a standalone CodeBuild use case. The .git metadata folder should exist by default, with the clone depth controlled during setup.

Hardeman answered 17/5, 2022 at 12:18 Comment(0)
N
0

As in this article from Srđan Milovanović, your AWS codebuidl buildspec.yml must include a variable with your expected information.

The buildspec.yml can call a shell script with the relevant git commands, before deploying the code.

Nance answered 15/5, 2022 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.