gitamazon-web-servicesaws-codebuild

Get GitHub git branch for AWS CodeBuild


I'm setup AWS CodeBuild to build automatically from GitHub. Other CI services provide an environment variable for the branch, but I can't find one for AWS CodeBuild. There is a CODEBUILD_SOURCE_VERSION that is set to either pr/7 where 7 is the pull request number or the git commit sha.

Given the commit sha, I've been trying to get the branch name, but no luck so far.

git branch --contains <commitsha> doesn't work because it is a detached head. How do I get the git branch for the commit from CodeBuild?


Solution

  • You can run:

    git branch -a --contains <sha>
    

    -a means all branches. If your sha is in no branch, which could happen in some cases, you won't see anything.

    Update:

    You can also get this information from CodeBuild environment variables. However, it will depend on how you are triggering your build or your specific case (Read the variable docs)