Get GitHub git branch for AWS CodeBuild

I configured AWS CodeBuild to build automatically from GitHub. Other CI services provide an environment variable for the branch, but I cannot find it for AWS CodeBuild. There exists CODEBUILD_SOURCE_VERSIONfor which either pr/7where 7- the number of the request for receipt, or git commit sha.

Given the commit, I tried to get the name of the branch, but so far no luck.

git branch --contains <commitsha>does not work because it is a separate head. How to get git branch for commit from CodeBuild?

+12
source share
6 answers

You can run:

git branch -a --contains <sha>

-ameans all branches. If your gang does not have a branch, which may happen in some cases, you will not see anything.

+5
source

https://github.com/thii/aws-codebuild-extras

⚠️ curl ! - thii/aws-codebuild-extras, !

, (!) , .

#!/bin/bash

export CI=true
export CODEBUILD=true

export CODEBUILD_GIT_BRANCH='git symbolic-ref HEAD --short 2>/dev/null'
if [ "$CODEBUILD_GIT_BRANCH" == "" ] ; then
  CODEBUILD_GIT_BRANCH='git branch -a --contains HEAD | sed -n 2p | awk '{ printf $1 }''
  export CODEBUILD_GIT_BRANCH=${CODEBUILD_GIT_BRANCH#remotes/origin/}
fi

export CODEBUILD_GIT_MESSAGE='git log -1 --pretty=%B'
export CODEBUILD_GIT_AUTHOR='git log -1 --pretty=%an'
export CODEBUILD_GIT_AUTHOR_EMAIL='git log -1 --pretty=%ae'
export CODEBUILD_GIT_COMMIT='git log -1 --pretty=%H'
export CODEBUILD_GIT_TAG='git describe --tags --abbrev=0'

export CODEBUILD_PULL_REQUEST=false
if [[ $CODEBUILD_GIT_BRANCH == pr-* ]] ; then
  export CODEBUILD_PULL_REQUEST=${CODEBUILD_GIT_BRANCH#pr-}
fi

export CODEBUILD_PROJECT=${CODEBUILD_BUILD_ID%:$CODEBUILD_LOG_PATH}
export CODEBUILD_BUILD_URL=https://$AWS_DEFAULT_REGION.console.aws.amazon.com/codebuild/home?region=$AWS_DEFAULT_REGION#/builds/$CODEBUILD_BUILD_ID/view/new

echo "==> AWS CodeBuild Extra Environment Variables:"
echo "==> CI = $CI"
echo "==> CODEBUILD = $CODEBUILD"
echo "==> CODEBUILD_GIT_AUTHOR = $CODEBUILD_GIT_AUTHOR"
echo "==> CODEBUILD_GIT_AUTHOR_EMAIL = $CODEBUILD_GIT_AUTHOR_EMAIL"
echo "==> CODEBUILD_GIT_BRANCH = $CODEBUILD_GIT_BRANCH "
echo "==> CODEBUILD_GIT_COMMIT = $CODEBUILD_GIT_COMMIT"
echo "==> CODEBUILD_GIT_MESSAGE = $CODEBUILD_GIT_MESSAGE"
echo "==> CODEBUILD_GIT_TAG = $CODEBUILD_GIT_TAG"
echo "==> CODEBUILD_PROJECT = $CODEBUILD_PROJECT"
echo "==> CODEBUILD_PULL_REQUEST = $CODEBUILD_PULL_REQUEST"
+11

install pre_build buildspec.yml:

bash -c "$(curl -fsSL https://raw.githubusercontent.com/thii/aws-codebuild-extras/master/install)"

: CI CODEBUILD CODEBUILD_GIT_AUTHOR CODEBUILD_GIT_AUTHOR_EMAIL, CODEBUILD_GIT_BRANCH, CODEBUILD_GIT_COMMIT CODEBUILD_GIT_MESSAGE CODEBUILD_GIT_TAG CODEBUILD_PROJECT, CODEBUILD_PULL_REQUEST.

+10

CodeBuild git . .git , git .

CI/CD CloudFormation:

  GitBranch:
    Description: Github branch to be deployed
    Type: String
    Default: master

Bash, / CI/CD:

readonly git_branch=$(git branch 2>/dev/null | grep "^*" | colrm 1 2)

aws cloudformation create-stack \
  --stack-name ${cicd_stack_name} \
  --parameters ParameterKey=GitBranch,ParameterValue=${git_branch}

CodeBuild:

CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
  Environment:
    Type: LINUX_CONTAINER
    Image: aws/codebuild/java:openjdk-8
    EnvironmentVariables:
      - Name: GIT_BRANCH
        Value: !Ref GitBranch

buildspec.yml:

post_build:
  commands:
    - echo [PHASE] Entered the post_build phase...
    - echo "[DEBUG] Git branch ${GIT_BRANCH}"
0

@ , buildspec? post_build, , , , , .git .

0

CodeBuild:

  • CODEBUILD_WEBHOOK_BASE_REF: webhook, . .
  • CODEBUILD_WEBHOOK_HEAD_REF: webhook, . .
  • CODEBUILD_WEBHOOK_TRIGGER: Shows the webhook event that caused the assembly. This variable is only available for assemblies triggered by a web hook. The value is parsed from the payload sent to CodeBuild Github, Github Enterprise, or Bitbucket. The format of the value depends on what type of event triggered the assembly.
    • For on-demand assemblies, this is pr / pull-request-number.
    • For assemblies launched by creating a new branch or sending a commit to a branch, this is the name of the branch / branch.
    • For assemblies launched by clicking a tag in the repository, this is tag / tag-name.
0
source

Source: https://habr.com/ru/post/1690276/


All Articles