Github Action - receive event when comment added to PR? - github-actions

Is there a way to get a Github Action to trigger on a PR when a comment is added to that PR? I've created a Github action that will trigger on a variety of events that occur on PRs (created, etc). The one piece I haven't figured out is the trigger for when a comment is added. I'm not seeing anything here that would indicate it's supported:
https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows
Am I missing something?

GitHub pull requests are actually issues. So the event you are looking for is issue_comment.
on:
issue_comment:
types: [created]
You can filter out just pull request comment events like this:
on:
issue_comment:
types: [created]
jobs:
example:
runs-on: ubuntu-latest
steps:
- name: Execute for pull request comments only
if: github.event.issue.pull_request
run: echo "This is a pull request comment event"

Here is a way to run a workflow when an "issue_comment" (can be an issue or PR) is created that also contains the text "run workflow")
name: Run Workflow on PR Comment
on:
issue_comment:
types: [created]
jobs:
rerun-tests-job:
if: contains(github.event.comment.body, 'run workflow')
uses: ./.github/workflows/main.yml

Related

How to make a Github Actions workflow update a Check on a Pull Request comment?

I am trying to use GitHub Actions to validate the book-keeping side of pull requests. Basically, the idea is that merging should be blocked unless certain tags, milestones, and other information is present in the PR. The logic I am currently struggling with is this: The PR needs to have one of two labels, "no release notes" or "public release notes" and if the "public release notes" label is present, then a specially formatted comment should be present with the release notes in question.
I have succeeded in getting the action to fire and update the check when the PR is created, or a label is added or removed. These paths modify the check status on the PR itself.
However, while I can get the Action to run when I add a PR comment (issue comment) this does not seem to update the check status. Is it possible to use an issue comment event to modify the check status of the PR directly?
The YML for the action is:
name: Github PR Audit
on:
pull_request:
types:
- opened
- edited
- labeled
- unlabeled
issue_comment:
types:
- created
- edited
- deleted
jobs:
Audit-Pull-Request:
runs-on: ubuntu-latest
steps:
You can use the GitHub Script GH Action + Branch protection rules configuration.
GitHub Script Action provides an easy and elegant way to run scripts in your workflow and Branch protection rules allow configuring which status checks must pass before branches can be merged.
Example workflow:
name: Github PR Audit
on:
pull_request:
types:
- opened
- edited
- labeled
- unlabeled
issue_comment:
types:
- created
- edited
- deleted
jobs:
Audit-Pull-Request:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script#v6
with:
script: |
const requiredLabels = ['no release notes', 'public release notes'];
let labels = [];
if (context.payload.pull_request) {
labels = context.payload.pull_request.labels;
} else if (context.payload.issue) {
labels = context.payload.issue.labels;
}
if (labels.filter(l => requiredLabels.includes(l.name)).length === 0) {
throw new Error(`Required labels: ${requiredLabels.join(', ')}`);
}
This script will check if its context has the corresponding labels and will fail if not.
Example failing run:
Branch protection rule configuration:
Creating a branch protection rule

How to not run a job when pull request is closed without merge

I have a workflow with 2 jobs, One job should run Terraform Plan when a pull request is created without a label and the other job should run Terraform Apply when a pull request is merged and does not have a label. currently my workflow is running on both occasions. The issue I'm having is that Terraform Plan is also running when the pull request is closed without merge. I only want Terraform Plan to run when a pull request is created without a label.
Below is a snippet of the triggers if I am missing something
name: Plan and merge to Main
on:
pull_request:
types: [ labeled, closed, opened ]
jobs:
Plan:
name: "Terraform Plan"'
if: github.base_ref == 'main' && join(github.event.pull_request.labels.*.name, '') == ''
Apply:
name: "Run Terraform Apply"
if: github.base_ref == 'main' && github.event.pull_request.merged == true && join(github.event.pull_request.labels.*.name, '') == ''
There’s no way to specify that a workflow should be triggered when a pull request is merged. However, because a merged pull request always results in a push, you can use the push event to accomplish your goal.
For example, let’s say that you want to run a workflow whenever a pull request is merged to your main branch. You can do something like this:
on:
push:
branches:
- main
also if you want to prevent push directly to main it's part of github pro plan.

How to find out if the Github Actions event that triggered the current workflow is a new pull request?

I have a Github Actions workflow that fires on:
on:
pull_request:
types:
- synchronize
- opened
that runs my custom action:
jobs:
my_job:
uses: "org/repo/.github/workflows/main.yml#master"
In the action org/repo I want to do an additional thing when a pull request is opened, but not when it is synchronized. So in org/repo/.github/workflows/main.yml I do:
- if: ${{ condition }}
name: Do that additional thing
What should be the condition to differentiate between a newly open pull request event and a "synchronize" event (pushing new commits etc)? I guess that would involve checking something in ${{ github.event.pull_request }}, but I couldn't find it in the documentation.
The condition is:
- if: ${{ github.event_name == 'pull_request' && github.event.action == 'opened' }}

How to get a GitHub Action step to run only on branches with a particular name prefix?

I am writing a GitHub Action. I want some of my steps to run only on certain branches.
The whole action is set to run only on master and on branches beginning with features/lr.
on:
push:
branches:
- master
- features/lr*
I have a "deploy" step that I want to run on master and on branches beginning with features/lrd. (So for example if my branch is named features/lr-foo, then the deployment step should be skipped.)
I know I can do if conditionals like this:
- name: Deploy application
if: github.ref == 'refs/heads/master'
Can I also check whether github.ref matches a certain prefix or pattern? What is the syntax for that?
Something like this pseudocode:
- name: Deploy application
if: github.ref == 'refs/heads/master' || github.ref.matches('refs/heads/lrd*')
Thanks in advance!
The branches, branches-ignore, tags, and tags-ignore keywords accept glob patterns. You can check details in docs - filter pattern.
As for using expressions, docs don't mention matches function, but maybe you could use something like contains, startsWith or endsWith. See here for details.
Inspired by the answer from frennky, I ended up doing the following in my step, which is ugly but works:
- name: Deploy application
if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/features/lrd')

Github actions 'pull_request_review' on a specific target branch

We are trying to setup a github action which only triggers on pull_request_review for pull requests targeted to a specific base branch pattern release/*
This is so we have a GH action be able to check if the review is from a specific release gatekeeper group.
Doing something like so, does not seem to work only for PR's targeted to release/* branches. It seems to trigger on PR reviews target to all base branches
on:
pull_request_review:
branches:
- release/*
Seems to us that we cannot chain the base branch like so
You can make it work by setting condition as in:
on:
pull_request_review:
branches:
- release/**
jobs:
release-job:
if: startsWith(github.event.pull_request.base.ref, 'release/')
This basically checks base reference for text (not case sensitive).
If the condition is false the wf will be still displayed among other ones, but will be shown as skipped (0s execution time).
Note that you would need to set this if condition for every job in a workflow.
I don't think you need branch filter anymore.
You can perform filtering at job level.
Example:
name: Filtering PR Reviews
on:
pull_request_review:
branches:
- 'releases/**'
jobs:
Releases:
name: Releases
runs-on: ubuntu-latest
if: ${{ startsWith( github.event.pull_request.base.ref, 'releases/' )}}
steps:
- name: trigger
run: echo "we are in releases/**"
ReleasesNext:
name: ReleasesNext
runs-on: ubuntu-latest
if: ${{ startsWith( github.event.pull_request.base.ref, 'releases/next' )}}
steps:
- name: trigger
run: echo "we are in releases/next"