From 79f77ea02c4e25feeb705052ebef4e8e5798a887 Mon Sep 17 00:00:00 2001 From: Arjun Verma Date: Wed, 31 Jul 2024 20:56:40 +0530 Subject: [PATCH] automate docker workflow (#8) * #458 Disable running workflow on PRs & enable push on main * Ping PR author & the person who merged * Do not remove `workflow_dispatch` * Try again after setting gh_token * Secret names must not start with GITHUB_ * Skip CLI auth * Also print PR info * Apply suggestions from code review Co-authored-by: Philip Meier * Enhance workflow, remove dependency on GH Token * Now try with token * Update .github/workflows/update-docker-requirements.yml Co-authored-by: Philip Meier * Remove has_pr, have author in body * Avoid tagging author if none * Rely on exit code instead setting `PR_NUMBER` to None --------- Co-authored-by: Philip Meier --- .../workflows/update-docker-requirements.yml | 52 +++++++++++++------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/.github/workflows/update-docker-requirements.yml b/.github/workflows/update-docker-requirements.yml index 1fcf0d89..069e9e70 100644 --- a/.github/workflows/update-docker-requirements.yml +++ b/.github/workflows/update-docker-requirements.yml @@ -1,7 +1,9 @@ name: docker-requirements on: - pull_request: + push: + branches: + - main paths: - ".github/workflows/update-docker-requirements.yml" - "pyproject.toml" @@ -42,24 +44,44 @@ jobs: NEEDS_UPDATE=$(git diff --quiet && echo 'false' || echo 'true') echo "needs-update=${NEEDS_UPDATE}" | tee --append $GITHUB_OUTPUT + - name: Check if commit is associated with a PR + id: pr-check + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + run: | + PR_NUMBER=$(gh api \ + -H 'Accept: application/vnd.github+json' \ + /repos/${{ github.repository }}/commits/${{ github.sha }}/pulls \ + --jq '.[0].number') + if [ $? -eq 0 ]; then + PR_INFO=$(gh pr view ${PR_NUMBER} --json author,mergedBy --jq '{author: .author.login, merger: .mergedBy.login}') + + if [ $? -eq 0 ]; then + AUTHOR=$(echo ${PR_INFO} | jq -r .author) + MERGER=$(echo ${PR_INFO} | jq -r .merger) + else + AUTHOR=${{ github.actor }} + MERGER="none" + fi + else + AUTHOR=${{ github.actor }} + MERGER="none" + fi + + echo "author=${AUTHOR}" | tee --append $GITHUB_OUTPUT + echo "merger=${MERGER}" | tee --append $GITHUB_OUTPUT + - name: Open a PR to update the requirements - # prettier-ignore - if: - ${{ steps.update.outputs.needs-update && github.event_name == 'workflow_dispatch' }} + if: ${{ steps.update.outputs.needs-update }} uses: peter-evans/create-pull-request@v5 with: commit-message: update requirements-docker.lock branch: update-docker-requirements branch-suffix: timestamp - base: ${{ github.head_ref || github.ref_name }} + base: main title: Update requirements-docker.lock - # prettier-ignore - body: - https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} - reviewers: ${{ github.actor }} - - - name: Show diff - # prettier-ignore - if: - ${{ steps.update.outputs.needs-update && github.event_name != 'workflow_dispatch' }} - run: git diff --exit-code + body: | + Automatic update of requirements-docker.lock. + [Workflow run details](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) + ${{ steps.pr-check.outputs.author != 'none' && format('cc @{0}', steps.pr-check.outputs.author) || '' }} + reviewers: ${{ steps.pr-check.outputs.merger }}