Let’s say that I have a workflow:
---
name: 'DEV Test'
on:
issue_comment:
types: [created]
pull_request:
branches:
- 'develop'
defaults:
run:
shell: python
jobs:
test:
# yamllint disable rule:line-length
if: ${{ (github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, 'continue_even_if_fail')) || github.event_name == 'pull_request' }}
name: 'DEV Test'
runs-on: ubuntu-latest
steps:
- name: Check if specific directives exists
env:
PR_MSG: ${{ github.event.pull_request.body }}
PR_COMMENT: ${{ github.event.comment.body }}
run: |
from os import environ
global_envs_file = environ.get('GITHUB_ENV')
# if event type is comment get message from comment body
if '${{ github.event_name }}' == 'issue_comment':
msg = environ.get('PR_COMMENT', '')
# if event type is PR create or sync get message from PR message
else:
msg = environ.get('PR_MSG', '')
# set global ENV `CONTINUE` to 0 or 1 depending on 'continue_even_if_fail' existance
with open(global_envs_file, 'a') as f:
if 'continue_even_if_fail' in msg:
f.write('CONTINUE=1n')
else:
f.write('CONTINUE=0n')
- name: fail
if: ${{ env.CONTINUE == 0 }}
uses: actions/github-script@v3
with:
script: |
core.setFailed('Set to fail')
- name: complete
run: |
print('''
${{ toJSON(github) }}
''')
So if we have continue_even_if_fail
identifier in message or PR comment, workflow should end successfully in theory. In other way it will fail (again in theory)
*It is just a synthetic simplified example and actual logic is more complicated and meaningful
Problem
This workflow works correctly if I update or create Pull Request. But nothing happens if I add comment to PR conversation.
Question
How to trigger test job in PR that will successfully end by new specific comment with continue_even_if_fail
message
PS
even if I simplify condition
from
if: ${{ (github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, 'continue_even_if_fail')) || github.event_name == 'pull_request' }}
to
if: ${{ github.event_name == 'issue_comment' || github.event_name == 'pull_request' }}
Nothing will be triggered
Documentation that I used to create the workflow: about events, about triggers, especially about issue_comment
trigger, about issue_comment
in Japanese
2
Answers
The problem was obvious but not intuitive at same time. So, I decided to share cause as answer.
Let's imagine that you have three branches:
feature
,develop
,main
. Wheremain
is a default branch.And now you trying to do Pull Request
feature
→develop
.The import point is that
issue_comment
is always triggered under default branch (main
in my case). Butpull_request
is triggered under Pull Request's branch (merged version offeature
anddevelop
in my case)It means that
run_test.yaml
workflow should exists in default branchmain
to be triggered byissue_comment
. In other waypull_request
will be triggered ifrun_test.yaml
workflow exists infeature
ordevelop
branches.To be able trigger both actions
issue_comment
andpull_request
we have to addrun_test.yaml
file atmain
andfeature
(develop
) branches first.And that was cause in my case, because workflow file was newly created inside Pull Request and only
pull_request
event was triggered.Here is a caution point: Because triggered branch is
main
you have to manually set PR's branch when checkout:And here is a second problem why
issue_comment
can't be used as Pull Request internal trigger for CI tests: because it triggered under defaultmain
branch it runs outside of the Pull Request as separated Action. And even if it completes with success PR's last CI test still will be marked as failed (with×
mark)The following workflow works very well for the described use case:
It uses the GitHub Script action only. The script already has access to the payload context.
The last step has the
if
condition. According to this condition, this step will run in case the previous step was successful or anyway if thedirective-validation
step returnedtrue
.In addition, You can extend the workflow triggers to handle even a PR body update or a comment update: