Using GitHub Actions on pull requests

Kenny DuMez
Kenny DuMez
Graphite software engineer

GitHub Actions allows you to create custom software development life cycle workflows directly in your repository. This guide explores how to utilize GitHub Actions specifically for pull request events, covering various triggers, common issues like actions not triggering, and how to manage workflows effectively.

GitHub Actions allows you to automate, customize, and execute your software development workflows right within your GitHub repository. You can set up workflows to build, test, package, release, or deploy any code project on GitHub. With the ability to run workflows based on a variety of events, such as pushes, pull requests, or issue comments, GitHub Actions can significantly streamline how you manage changes.

  1. Getting started with a workflow file:

    • Workflows are defined in the .github/workflows directory of your repository. These are written in YAML format.
    • To create a workflow that triggers on pull request events, you start by defining the name and the event that triggers the workflow in a YAML file.

    Here’s a basic example of a workflow file that triggers on pull request events:

    Terminal
    name: Example PR Workflow
    on:
    pull_request:
    types: [opened, synchronize, reopened, closed]
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Run a one-line script
    run: echo "The event type is ${{ github.event.action }}"
    • on: pull_request: This specifies that the workflow should trigger on pull request events. You can specify further what types of pull request actions trigger the workflow, such as when pull requests are opened, synchronized, reopened, or closed.
  2. Reacting to different types of pull request actions:

    • When a pull request is merged, you might want to trigger a deployment or further integration tests. Use the conditional if: github.event.pull_request.merged == true within your job steps to specify actions that should only occur if the pull request has been merged.
    • To handle push events on pull requests, you can include push in the on field. However, typically pull request workflows are triggered by the synchronize event, which occurs when new commits are pushed to the pull request branch.
  3. Fetching branch names in workflows:

    • To get the branch name from which the pull request originated, you can use github.head_ref in your workflow. This is particularly useful for workflows that need to deploy or further process code based on branch-specific configurations.

    Example step to echo the branch name:

    Terminal
    - name: Get branch name
    run: echo "Branch name is ${{ github.head_ref }}"
  4. Troubleshooting common issues:

    • Actions not triggering on pull request: If your workflows are not triggering as expected, ensure you've correctly set the event types under the pull_request key. It’s also important to check if there are branch protection rules that might be preventing the workflows from running.
  5. Advanced use cases:

    • Conditional workflows based on the changed files: You can make your workflows more efficient by triggering jobs only when specific files are changed in a pull request. This is done using the paths or paths-ignore filters under the on field.

    For example, this snippet triggers a workflow only when JavaScript files are changed:

    Terminal
    on:
    pull_request:
    paths:
    - '**.js'

By defining specific events to trigger workflows, such as on pull request creation or merging, and utilizing conditional logic to tailor the workflow steps based on the nature of the pull request, you can significantly enhance the efficiency and reliability of your development process.

For further reading see the official GitHub documentation.

On this page
Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2