Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci] PR Review Companion should be called iff there is diffed content from PR Test #28579

Closed
bsmth opened this issue Aug 16, 2023 · 5 comments
Closed
Labels
infra Infrastructure issues (npm, GitHub Actions, linting) for this repo

Comments

@bsmth
Copy link
Member

bsmth commented Aug 16, 2023

summary:

  1. PR is opened with no changes to ./files directory
  2. "PR Test" = completed with some steps skipped
  3. PR review companion will fail

Current behavior:

.github/workflows/pr-review-companion.yml:

name: PR review companion

on:
  workflow_run:
    workflows: ["PR Test"]
    types:
      - completed
    # This will run even if steps are skipped (e.g. if no `./files/**/*` changed)

jobs:
  review:
    # ...

We call the PR review companion from the PR Test workflow if it passes:

.github/workflows/pr-test.yml:

name: PR Test

on:
  pull_request:
    branches:
      - main
    paths:
      - .nvmrc
      - ".github/workflows/pr-test.yml"
      - "files/**"

jobs:
  tests:
    steps:
      # ...

      # Steps skipped if no files changed
      - uses: actions/upload-artifact@v3
        if: ${{ env.GIT_DIFF_CONTENT }}
        env:
          BUILD_OUT_ROOT: /tmp/build
        with:
          name: build
          path: ${{ env.BUILD_OUT_ROOT }}

      # Etc. This will complete the job and run PR companion even if no files changed

proposal:

What we want to do is to call the PR companion only if there are changes in the files we care about:

.github/workflows/pr-review-companion.yml:

name: PR review companion

# We only want to run this when called from the PR Test workflow
on:
  workflow_call:

jobs:
  review:
    runs-on: ubuntu-latest

We call the PR companion from "PR Test" if we have changes in the files we care about:

.github/workflows/pr-test.yml:

name: PR Test

on:
  pull_request:
    # no changes ...

jobs:
  tests:
    steps:
      # business as usual ...

      # Last step, everything looking good so far
      - name: Check changed files
        if: ${{ env.GIT_DIFF_FILES }}
        env:
          CONTENT_ROOT: ${{ github.workspace }}/mdn/content/files
          CONTENT_TRANSLATED_ROOT: ${{ github.workspace }}/files
        working-directory: ${{ github.workspace }}/mdn/content
        run: |
          echo ${{ env.GIT_DIFF_FILES }}

          yarn filecheck ${{ env.GIT_DIFF_FILES }}

      # ADDITIONS:
      # This is the end of the current job, now we want to call the PR companion
      - name: Call PR companion
        if: ${{ env.GIT_DIFF_FILES }}
        uses: ./.github/workflows/pr-review-companion.yml
@bsmth bsmth added the infra Infrastructure issues (npm, GitHub Actions, linting) for this repo label Aug 16, 2023
@github-actions github-actions bot added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Aug 16, 2023
@bsmth bsmth removed the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Aug 16, 2023
@yin1999
Copy link
Member

yin1999 commented Aug 17, 2023

We should also consider that the workflow triggered by pull_request target can not access secret.

If we use the pull_request_target instead. We should try to avoid using secret.GITHUB_TOKEN (which has the write permission, instead we could setup a read-only secret token for this) in the build job, and add a spread job to use the secrets and upload built asset. (maybe we could set the output of the build job and add a if condition for the second job. I've made a test in my fork:

Append:

We could set read-only permission for the build job!

Implementation: #28617

@bsmth
Copy link
Member Author

bsmth commented Aug 17, 2023

Thanks @yin1999 - I like the after-build job.

In your PR I think we can go the route of reusable workflows to keep the logic isolated and avoid huge yml files.

There's some examples here that we might want to consider: https://github.com/bsmth/reusable-workflow-example/blob/main/.github/workflows/upload-artifact.yml

Calling the workflow from another will keep the context of the "main" workflow: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context

I agree we could use outputs or define a workflow-scoped boolean ('needs_preview' or something) and in the job that calls the review companion, we can check if it needs to be run or not like:

  call-workflow:
    needs: upload-artifact
    if: ${{ env.NEEDS_PREVIEW }}
    uses: ./.github/workflows/review_companion.yml

What do you think?

@yin1999
Copy link
Member

yin1999 commented Aug 17, 2023

In your PR I think we can go the route of reusable workflows to keep the logic isolated and avoid huge yml files.

There's some examples here that we might want to consider: https://github.com/bsmth/reusable-workflow-example/blob/main/.github/workflows/upload-artifact.yml

Calling the workflow from another will keep the context of the "main" workflow: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context

I agree we could use outputs or define a workflow-scoped boolean ('needs_preview' or something) and in the job that calls the review companion, we can check if it needs to be run or not like:

  call-workflow:
    needs: upload-artifact
    if: ${{ env.NEEDS_PREVIEW }}
    uses: ./.github/workflows/review_companion.yml

What do you think?

Yes. I like this idea :)

@yin1999
Copy link
Member

yin1999 commented Nov 10, 2023

@bsmth This issue has been resolved by #30064 :)

@bsmth
Copy link
Member Author

bsmth commented Nov 13, 2023

Super, thank you! closing as done now 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infra Infrastructure issues (npm, GitHub Actions, linting) for this repo
Projects
None yet
Development

No branches or pull requests

2 participants