I have a Cloud Build trigger which runs only when a new Pull Request is created on master branch of a Github repo.
However, right now, pytest is running on the whole repo, while I require pytest to be run only on files that have been created/edited in the Pull Request.
My cloudbuild file is as follows:
#Testing
steps:
- name: 'python:3.7'
id: Testing
entrypoint: 'bash'
args:
- '-c'
- |
pip install --upgrade pip \
&& pip install -r requirements.txt \
&& pytest **/test_*.py
Any idea how to implement this?
CodePudding user response:
I believe that Cloud Build does not expose the list of modified files in pull requests, but as a workaround you can use Github's webhooks.
Get the “_PULL_REQUEST_ID” in cloud build using substitution process as per documentation
_PULL_REQUEST_ID $(pull_request.pull_request.id)
Use the above pull request id received in the substitution process in github rest api and get the list of modified/created files.
a) To know how this api works refer api link.
b) How to use rest api refer rest link.
c) Refer to example code at github github link.
Update GITHUB_REPOSITORY to $REPO_NAME as per doc and update github.event.pull_request.number to the number received in step 1
URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${{ github.event.pull_request.number }}/files" FILES=$(curl -s -X GET -G $URL | jq -r '.[] | .filename') echo “$FILES” > modified.txt
As per above example code, list of files will be saved to modified.txt. As you get the list of modified/created files you can perform tests on those by getting the file names from modified.txt by using bash while loops or python loop.