Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In GitHub Actions, how can I trigger on push but only if a PR is active?

Say I have 2 branches, a feature branch and a develop branch.

There's no SLA on feature branch normally, meaning I can push broken code up to it all day long and no CI build should be triggered.

Then I open a PR into develop. I trigger a CI build action on pull_request: created. Let's say this build fails. By default I can't merge the PR, which is correct.

Now I want to push edits to feature branch to update the PR. I want these pushes to trigger a CI build (because we're now working inside an open PR). I don't want to allow the PR to proceed/be merged until these push-CIs pass.

How can I do that in GitHub Actions? I tried on pull_request: edited but that didn't work for me.

I'm looking for the functional equivalent of:

on:
  push:
    if: inside_open_pr
like image 906
Greg Avatar asked Dec 01 '20 15:12

Greg


People also ask

Can you manually trigger a GitHub Action?

Earlier this week, GitHub announced a new feature which allows developers to trigger workflows manually from within the “Actions” tab.

How do I trigger an action workflow in GitHub?

If you do want to trigger a workflow from within a workflow run, you can use a personal access token instead of GITHUB_TOKEN to trigger events that require a token. You'll need to create a personal access token and store it as a secret.

How to manually trigger GitHub actions workflow from the command line?

We can now manually trigger our GitHub actions workflow using curl from the command line. If we do not specify the types property of the repository_dispatch trigger, our workflow will run in response to any repository_dispatch event. Otherwise it will limit for particular event type.

What is the difference between push and PR workflows in GitHub?

The only differences are the branch check via github.ref, and the specifics of steps. I happen to be using my own Github Action tjtelan/zola-deploy-action but your steps could consist of anything you want to do differently between pull requests and push to your “special” branch. Here’s a template that you can use for your own push vs PR workflows.

What events can be triggered by GitHub workflow?

These events can be: Events that occur outside of GitHub and trigger a repository_dispatch event on GitHub For example, you can configure your workflow to run when a push is made to the default branch of your repository, when a release is created, or when an issue is opened.

How do I check if a GitHub workflow is always push?

One of the variables we can use in out expression is github.event_name, this variable is the name of the event that triggered the workflow run. In our example this can either be push or pull_request. So what we can do is check that the event is always a push on our step.


Video Answer


2 Answers

Use the event pull_request and don't specify activities or specify the activity synchronize:

on:
  pull_request:

Note: By default, a workflow only runs when a pull_request's activity type is opened, synchronize, or reopened. To trigger workflows for more activity types, use the types keyword.

source

if you push a new commit to the HEAD ref of a pull request then this “synchronize” event will be triggered. This is because the system is syncing the pull requests with the latest changes. This will NOT trigger if the base ref is updated.

source

If you also need to react to a change of the base branch, then you have to add the activity edited.

source

like image 151
riQQ Avatar answered Oct 27 '22 08:10

riQQ


If pull_request is not an option, you can try using https://github.com/marketplace/actions/get-current-pull-request

like image 1
Kushagra Gour Avatar answered Oct 27 '22 09:10

Kushagra Gour