Skip to content

bahmutov/should-run-github-action

Repository files navigation

should-run-github-action @actions/core version

A reusable GitHub action to determine if the user clicked a checkbox in the PR text

Inputs

name type default description
checkbox_text string re-run the tests The checkbox text to display

Outputs

See action.yml for the current outputs

  • shouldRun Boolean value indicating if next actions should run
  • branch The source (head) branch of the PR or commit
  • commit The head commit SHA of the source branch of the PR commit

You can access the above outputs using ${{ steps.<step name>.outputs.<output name> }} expression syntax, see the pr-edit.yml workflow.

Use

Create a GitHub Actions workflow that runs on pull request edits.

# .github/workflows/pr.yml
name: pr
on:
  pull_request:
    types:
      - edited
jobs:
  trigger-tests:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v4

      - name: Check the PR
        # https://github.com/bahmutov/should-run-github-action
        uses: bahmutov/should-run-github-action@v1
        id: check-pr
        with:
          checkbox_text: 're-run the tests'
        env:
          GITHUB_EVENT: ${{ toJson(github.event) }}

      - name: Run tests if the user clicked the checkbox
        if: ${{ steps.check-pr.outputs.shouldRun }}
        run: echo "Running tests..."

In your GitHub pull request template, use a checkbox

# .github/PULL_REQUEST_TEMPLATE.md

To re-run the tests, pick the tags above then click the checkbox below

- [ ] re-run the tests

When the user clicks on the checkbox and changes its state from empty to filled, the action sets its output to true to trigger the other workflow steps.

Examples

For more actions see

Debugging

Run this action with the environment variable DEBUG=should-run-github-action

Ref names

  • commit pushed to the main branch has event.ref "refs/heads/main"
  • commit pushed to the pr1 branch has event.ref "refs/heads/pr1"
  • for pull requests, from the pr1 branch the event.pull_request.head.ref is "pr1"

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2022

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on GitHub

MIT License

Copyright (c) 2022 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A reusable GitHub action to determine if the user clicked a checkbox inn the PR text

Resources

Stars

Watchers

Forks