'Github action test if a commit containing a specific word was previously made

I need to make sure to test with github action, if a commit has previously been made that contains the word build. If the commit does not contain the word build then tests with github action should not be run.

Can you give me some advice?

Test:

name: "Testing"

on:
  push:
    branches:
      - master

jobs:
  test_the_action:
    name: Test the action
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2          
      - uses: suisei-cn/actions-download-file@master
        id: downloadfile
        name: Download a file
        with:
          url: "[API Endpoint](https://api.github.com/repos/suisei-cn/actions-download-file)"
          target: public/
          auto-match: true

      - name: Display the file
        run: head -n8 public/actions-download-file          


Solution 1:[1]

From a push event, it's possible to extract the commit message by using github.event.commit.message

Here is an example of the github context for a push event.

Note that if there are several commit messages:

  • commit[0] contains the oldest commit
${{ github.event.commits[0].message }}
  • head_commit contains the youngest commit
${{ github.event.head_commit.message }}

Then, you can check in your job if the commit message contains or not the word you want, for example by using:

if: "!contains(github.event.head_commit.message, 'build')"

Therefore, your workflow could look like this if you don't want the job to be run if the commit message contains the 'build' word:

name: "Testing"

on:
  push:
    branches:
      - master

jobs:
  test_the_action:
    if: "!contains(github.event.head_commit.message, 'build')"
    name: Test the action
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2          
      - uses: suisei-cn/actions-download-file@master
        id: downloadfile
        name: Download a file
        with:
          url: "[API Endpoint](https://api.github.com/repos/suisei-cn/actions-download-file)"
          target: public/
          auto-match: true

      - name: Display the file
        run: head -n8 public/actions-download-file 

Finally, you now also have the option to skip ci workflows with key words in the commit messages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1