Tommy
Tommy

Reputation: 13632

AWS Code Build with Github - get changed files

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

Upvotes: 8

Views: 2412

Answers (3)

You can use git diff --name-only $$CODEBUILD_RESOLVED_SOURCE_VERSION $$CODEBUILD_WEBHOOK_PREV_COMMIT

Where $CODEBUILD_WEBHOOK_PREV_COMMIT is the commit id of the previous commit. And $CODEBUILD_RESOLVED_SOURCE_VERSION is the commit id of the actual one.

Inside a build phase you can check the change with:

-

 |
    if [ "$(git diff --name-only $CODEBUILD_WEBHOOK_PREV_COMMIT $CODEBUILD_RESOLVED_SOURCE_VERSION | grep -e <filde_path>)" != "" ]; then
      #your code;
    fi

Upvotes: 0

slushi
slushi

Reputation: 1434

git diff --name-status $CODEBUILD_WEBHOOK_BASE_REF should do the trick

Upvotes: 1

Klugschei&#223;er
Klugschei&#223;er

Reputation: 1614

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.

Upvotes: 1

Related Questions