From 3f38e4f69e555b984fb0071a8b2eec714626af31 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 18 Mar 2020 14:33:39 -0700 Subject: [PATCH] fix(actions/gapic): only push if necessary --- .github/workflows/test-actions.yml | 19 +++++++++++++++++++ actions/gapic/entrypoint.sh | 28 +++++++++++++++++++--------- 2 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/test-actions.yml diff --git a/.github/workflows/test-actions.yml b/.github/workflows/test-actions.yml new file mode 100644 index 0000000..a8b6e6a --- /dev/null +++ b/.github/workflows/test-actions.yml @@ -0,0 +1,19 @@ +name: Test Actions +on: + push: + schedule: + - cron: '0 * * * *' +jobs: + gapic: + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v2 + - name: Run the gapic action + uses: ./actions/gapic + with: + target: //google/maps/routes/v1:gapic-maps-routes-v1-go + github_token: ${{ secrets.GITHUB_TOKEN }} + tar_path: gapic-maps-routes-v1-go/cloud.google.com/go/maps + tar_strip_components: 4 + cache_service_account: ${{ secrets.SERVICE_ACCOUNT_CACHE }} diff --git a/actions/gapic/entrypoint.sh b/actions/gapic/entrypoint.sh index f63432a..b51df23 100755 --- a/actions/gapic/entrypoint.sh +++ b/actions/gapic/entrypoint.sh @@ -29,7 +29,7 @@ bazel build $INPUT_TARGET popd -#### PUBLISH CHANGES #### +#### MAKE LOCAL CHANGES #### pushd $TEMP_GIT_LOCAL @@ -46,17 +46,27 @@ git reset --hard origin/master # extract the tar to the correct location tar xf "${TEMP_GIT_GOOGLEAPIS}/bazel-bin/${TARGET_OUTPUT}" --strip-components $INPUT_TAR_STRIP_COMPONENTS $INPUT_TAR_PATH -# commit the changes if any to the branch -if [[ -n $(git status -s -uall) ]]; then +#### PUBLISH COMMITS #### + +[[ -n $(git diff "origin/master") ]] && differs_from_master=1 || differs_from_master=0 + +if [ $differs_from_master ]; then git add -A git commit -m 'feat: regenerate gapic' - git push -f -u origin $BRANCH - curl \ - -H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \ - -H "Content-Type:application/json" \ - -X POST https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls \ - -d "{\"title\":\"GAPIC Client Update: ${INPUT_TARGET}\", \"body\": \"\", \"head\": \"$BRANCH\", \"base\": \"master\"}" + [[ -n $(git ls-remote --heads origin ${BRANCH}) ]] && has_branch=1 || has_branch=0 + [[ -n $(git diff "origin/${BRANCH}") ]] && differs_from_branch=1 || differs_from_branch=0 + + if [[ !$has_branch || $differs_from_branch ]]; then + git push -f -u origin $BRANCH + + curl \ + -H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \ + -H "Content-Type:application/json" \ + -X POST https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls \ + -d "{\"title\":\"GAPIC Client Update: ${INPUT_TARGET}\", \"body\": \"\", \"head\": \"$BRANCH\", \"base\": \"master\"}" + fi + fi popd