7

Tangentially related to: AWS CodeBuild with GitHub - only for specific directory

I have a codebuild project and a github repo with many files in it. A user may update any of these files in the git repo. I want to pass the name of the altered file(s) into my buildspec.yaml somehow; IE my merge job logic, specified in the buildspec.yaml, needs to know what files changed to do a per-file operation.

I am not talking about filters; Ie "only trigger this if X,Y,Z changed". Becuase the filter is there for a large number of XYZ, but I need to know which file(s) changed in my buildspec. IE something like $CHANGED_FILE_LIST.

I don't see this here: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html

Maybe we have to do something like this: how to find out list of all changed files in git for full jenkins build and not for a particular commit?

git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT

but one would think this meta info could be provided by codebuild

Tommy
  • 11,318
  • 11
  • 58
  • 94

1 Answers1

0

I don't know if there's a blessed CodeBuild way to do this, but assuming you have access to a GitHub token in the build, you can query the GitHub metadata endpoint to get the info you need. Something like

curl -H "Authorization: token ${yourtoken}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/OWNER/REPO/commits/${CODEBUILD_SOURCE_VERSION} | jq '.files[].filename'

will return a list of files in the commit.

See https://docs.github.com/en/rest/commits/commits#get-a-commit for more details.

Klugscheißer
  • 1,455
  • 1
  • 11
  • 22