17

I've recently added a static analysis step to my GitHub actions. However, it is somewhat expensive so I'm trying to reduce the number of times that this step is run. I've already added an appropriate filter so that it only runs on one OS and so that it only runs on my "feature" branches. However, I would like to also filter out any checkins that include "WIP" in their commit message. (The theory being that there is no point in performing the full analysis until it is no longer a "Work In Progress".)

I've searched through the docs, expecting I would find an object I could use as part of the github context object, but to no avail.

Any ideas on how I can accomplish this goal?

If you want to see exactly what I'm doing, the action Yaml is as follows. I'm hoping to find some change I can make to the if statement on the Static Analysis item that would accomplish my goal.

name: On Push

on: [push]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
    steps:
    - uses: actions/checkout@v1

    - name: Build
      run: |
        ./configure
        make -j 4

    - name: Run tests
      run: |
        make -j 4 check

    - name: Static Analysis
      if: runner.os == 'macOS' && startsWith(github.ref, 'refs/heads/feature/')
      run: |
        make analyze
Steven W. Klassen
  • 1,401
  • 12
  • 26

4 Answers4

24

You can make use of the event property on the github context to access the push payload.

Add the following to the step that does the analysis:

if: !startsWith(github.event.head_commit.message, 'WIP')
smac89
  • 39,374
  • 15
  • 132
  • 179
  • 4
    This answer was almost, but not quite correct. It led me to the correct solution, so I'm giving smac89 credit, but I have edited the answer to show exactly what I needed. Specifically `github.event.commits[0]` gave me the oldest commit in the push and I needed the newest. It turns out there is an item `github.event.head_commit` that contains that information. – Steven W. Klassen Dec 22 '19 at 20:37
  • 2
    You actually need a space after the `!` and probably to wrap it all with quotes – leoschet Mar 15 '21 at 18:42
  • 1
    if: "!startsWith(github.event.head_commit.message, 'WIP')" – Alwin Sep 06 '21 at 16:36
12

Related to your issue, you now (Feb. 2021) have:

GitHub Actions: Skip pull request and push workflows

with [skip ci]

GitHub Actions now supports skipping push and pull_request workflows by looking for some common keywords in your commit message.

If any commit message in your push or the HEAD commit of your PR contains the strings [skip ci], [ci skip], [no ci], [skip actions], or [actions skip] workflows triggered on the push or pull_request events will be skipped.

This is not as flexible as a custom commit message (like your "WIP"), but it still can help.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

if: ${{ !startsWith(github.event.head_commit.message, '#NORUN') }}

Syntax should be like this

Kaung
  • 1
  • Please read the [markdown guide](https://stackoverflow.com/editing-help) to learn how to style your answers in an easy-to-read format. This will help improve both this answer and any future answers you choose to write on the platform. – Aaron Meese Nov 09 '22 at 00:59
0

github.event.head_commit.message and github.event.commits don't work anymore.

Here is what I did instead to filter out WIP messages in all commits of a pull-request:

jobs:
  block-wip-in-commit:
    runs-on: [ubuntu-latest]
    steps:
      - name: 'Ensure no WIP in commit messages'
        run: |
          PR_NUMBER="${{ github.event.pull_request.number }}"
          PR_REPO="${{ github.event.pull_request.head.repo.full_name }}"
          TOKEN="${{ secrets.GITHUB_TOKEN }}"
          commits_url="https://api.github.com/repos/$PR_REPO/pulls/$PR_NUMBER/commits"
          commit_messages=$(curl -s -H "Authorization: token $TOKEN" $commits_url | jq '.[].commit.message')
          for message in $commit_messages; do
            if [[ $message == *"WIP"* ]]; then
              echo "Found WIP in commit message: $message"
              exit 1
            fi
          done
Vincent J
  • 4,968
  • 4
  • 40
  • 50