Github actions 'pull_request_review' on a specific target branch - github-actions

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"

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 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')

How to define a variable with a list of files in a YAML file for Azure DevOps or GitHub

How to avoid duplication of file lists in Azure DevOps / GitHub pipelines?
Suppose I have the following YAML file:
name: Build
on:
push:
branches: [ master ]
paths:
- 'SRC/define.inc'
- 'SRC/SrvMain.pas'
- 'SRC/Tiny.dpr'
- 'SRC/xBase.pas'
pull_request:
branches: [ master ]
paths:
- 'SRC/define.inc'
- 'SRC/SrvMain.pas'
- 'SRC/Tiny.dpr'
- 'SRC/xBase.pas'
workflow_dispatch:
jobs:
...skipped remaining lines...
(I'm using this pipeline for the TinyWeb repository on GitHub)
As you see, the list of files under the paths: section repeats for the "push" and "pull_request" sections. How can I define a list of files just once, so I would not need to copy it to each of the sections, but only add a reference to the list variable? I've tried to define the list using the variables: keyword and then reference the list as ${{ variables.my_variable_name }}, but it didn't work. I tried multiple variants to no avail.
Please give an example of the YAML file where I can define the list of files once and then use it from multiple sections under on:.
Hi #Maxim Masiutin I have tried with both env and secrets but it didn't work out. You are asking for using one path for both push and pull_request events and it is not possible for now. There is no statements that shows supporting for variables at workflow level. You can check this for paths/paths_ignore.
However you are using both push and pull_request events exactly same I think you may use like below:
name: Build
on:
push:
pull_request:
branches: [ master ]
paths:
- 'SRC/define.inc'
- 'SRC/SrvMain.pas'
- 'SRC/Tiny.dpr'
- 'SRC/xBase.pas'
workflow_dispatch:
jobs:
I hope this works for you.
This syntax worked for me on Azure.
name: Build
variables:
src_define: 'SRC/define.inc'
src_srvmain: 'SRC/SrvMain.pas'
src_tiny: 'SRC/Tiny.dpr'
src_xbase: 'SRC/xBase.pas'
on:
push:
branches: [ master ]
paths:
- variables['src_define']
- variables['src_srvmain']
- variables['src_tiny']
- variables['src_xbase']
pull_request:
branches: [ master ]
paths:
- variables['src_define']
- variables['src_srvmain']
- variables['src_tiny']
- variables['src_xbase']
workflow_dispatch:
jobs:

Github Action - receive event when comment added to PR?

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