Skip to content

fix(gh-workflow): skip comment when push directly #6179

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions .github/workflows/analyze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ on:
workflow_dispatch:

jobs:
event_type:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Save Event Type
run: echo ${{ github.event_name }} > ./event_type

- name: Upload Event Type
uses: actions/upload-artifact@v2
with:
path: ./event_type
name: event_type

analyze:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -48,10 +62,10 @@ jobs:

- name: Download base branch bundle stats
uses: dawidd6/action-download-artifact@v2
if: success() && github.event.number
if: success()
with:
workflow: analyze.yml
branch: ${{ github.event.pull_request.base.ref }}
branch: ${{ github.event.pull_request.base.ref || 'main' }}
name: bundle_analysis.json
path: .next/analyze/base/bundle

Expand All @@ -69,14 +83,21 @@ jobs:
# Either of these arguments can be changed or removed by editing the `nextBundleAnalysis`
# entry in your package.json file.
- name: Compare with base branch bundle
if: success() && github.event.number
if: success()
run: ls -laR .next/analyze/base && npx -p nextjs-bundle-analysis compare

- name: Upload analysis comment
uses: actions/upload-artifact@v2
with:
name: analysis_comment.txt
path: .next/analyze/__bundle_analysis_comment.txt

number:
runs-on: ubuntu-latest
needs: analyze
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v2

- name: Save PR number
run: echo ${{ github.event.number }} > ./pr_number
Expand Down
21 changes: 17 additions & 4 deletions .github/workflows/analyze_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ on:
jobs:
comment:
runs-on: ubuntu-latest
if: >
${{ github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' }}
Comment on lines -12 to -14
Copy link
Contributor

@alinkedd alinkedd Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the core problem lies in the evaluation of these lines by GitHub's runner. If github.event.workflow_run.event is not even the right parameter (as you implied in another PR), then this job should never run. Nevertheless, it runs, meaning that the condition is always set to true somehow - just like here: actions/runner#1173. Presumably, ${{ and }} syntax along with block formatting sign > result in the text, thus making it a truthy value.

Did you try other variations of that condition without the block sign > and with event_name like this?

if: github.event.workflow_run.event_name == 'pull_request' && github.event.workflow_run.conclusion == 'success'

P.S.
Regarding the PR itself, I think we still need the if success check for the entire comment job. Otherwise, the remaining Comment step is going to produce an error in case steps.get-comment-body.outputs is empty.

P.P.S.
Also, I was thinking that ignoring the default branch for this workflow can serve as an additional precaution as well:

on:
  workflow_run:
    workflows: ["Analyze Bundle"]
    types:
      - completed
    branches-ignore:
      - main # change this if your default branch is named differently

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually checked and this did the trick:

if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'

event is the right property and the evaluation is indeed broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually checked and this did the trick:

How do you check this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alinkedd I remember > was used for multiline?

also I checked what other option we have for multiline and ended up here https://github.com/orgs/community/discussions/25641#discussioncomment-3248571 if we try with | and everything works properly then the problem might be with > only?

Copy link
Contributor

@alinkedd alinkedd Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@awxiaoxian2020 I tried hypothesis with same condition in my test repo and it seemed to be right.

UPD
The fixes for workflows triggered by workflow_run work only if they are added to the default branch, so I could not tested them here with PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harish-sethuraman
It seems, the problem is not about block signs (|, >, etc), but rather about expression within ${{ and }} and some text around it (even if it's | or >). So today I rechecked this more readable version without expression and it works fine:

if: >
  github.event.workflow_run.event == 'pull_request' &&
  github.event.workflow_run.conclusion == 'success'

And I updated my another PR #6651 which also contains some other fixes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes have been moved to #7667

steps:
- name: Download Event Type
uses: dawidd6/action-download-artifact@v2
with:
workflow: analyze.yml
run_id: ${{ github.event.workflow_run.id }}
name: event_type
path: event_type

- name: get type
id: get-type
run: |
event_type=$(cat event_type/event_type)
echo "event-type=$event_type" >> $GITHUB_OUTPUT

- name: Download base branch bundle stats
if: github.event.workflow_run.conclusion == 'success' && steps.get-type.outputs.event-type == 'pull_request'
uses: dawidd6/action-download-artifact@v2
with:
workflow: analyze.yml
Expand All @@ -22,6 +34,7 @@ jobs:
path: analysis_comment.txt

- name: Download PR number
if: github.event.workflow_run.conclusion == 'success' && steps.get-type.outputs.event-type == 'pull_request'
uses: dawidd6/action-download-artifact@v2
with:
workflow: analyze.yml
Expand All @@ -31,7 +44,7 @@ jobs:

- name: Get comment body
id: get-comment-body
if: success()
if: success() && github.event.workflow_run.conclusion == 'success' && steps.get-type.outputs.event-type == 'pull_request'
run: |
echo 'body<<EOF' >> $GITHUB_OUTPUT
echo '' >> $GITHUB_OUTPUT
Expand Down