Description
Subject
guides
Description
The guide mentions many times how to obtain git commit information that'd be sent to Cypress Dashboard.
https://docs.cypress.io/guides/continuous-integration/aws-codebuild
The thing is, the docs use git
CLI which is no good when you're importing the code from a GitHub repository. It turns out that the CodeBuild downloads the codebase (not clones) by fetching a zip file, so using the example results in fatal: not a git repository
error.
The solution is to make use of CODEBUILD_RESOLVED_SOURCE_VERSION
env variable which holds an SHA of a commit (the only git related piece of information here!) and speak to GitHub API to fetch the rest. One needs a Personal Access Token and jq
tool for that.
Yes, that's only for GitHub repos.
The complete working example.
GITHUB_COMMIT="$CODEBUILD_RESOLVED_SOURCE_VERSION"
# obtaining the credentials is up to the developer, but here's an example
#GITHUB_USER=$(aws ssm get-parameter --name PIPELINES_GITHUB_USER | jq -r .Parameter.Value)
#GITHUB_TOKEN=$(aws secretsmanager get-secret-value --secret-id PIPELINES_GITHUB_TOKEN | jq -r .SecretString)
COMMIT_INFO=$(curl -s -u ${GITHUB_USER}:${GITHUB_TOKEN} -X GET https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/git/commits/${GITHUB_COMMIT})
export COMMIT_INFO_MESSAGE="$(echo $COMMIT_INFO | jq -r .message)"
export COMMIT_INFO_EMAIL="$(echo $COMMIT_INFO | jq -r .author.email)"
export COMMIT_INFO_AUTHOR="$(echo $COMMIT_INFO | jq -r .author.name)"
export COMMIT_INFO_SHA="$(echo $COMMIT_INFO | jq -r .sha)"
export COMMIT_INFO_REMOTE="$(echo $COMMIT_INFO | jq -r .html_url)"