Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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          
like image 894
Paul Avatar asked Jun 09 '26 23:06

Paul


1 Answers

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.

like image 187
GuiFalourd Avatar answered Jun 11 '26 22:06

GuiFalourd