github actions: Push several times, but I want the build to be done only for the last push

Viewed 3628
name: master builder
on:
  push:
    branches:
      - master
    ~~~

I have a workflow like this. So, Whenever I push to the master branch, the actions run. But I want the build to work only on the last push. For example,

master branch - feature1 (person1)

master branch - feature2 (person2)

master branch - feature3 (person3)

In this structure, if features1,2,3 are merged at almost the same time, the build will run 3 times. But I want the master branch to be built only on the last merge. Just once. Is there anyway to do this? like.. run the build only once after waiting for about 1 minute when pushing.


This is a sample code that I proceeded in the way you answered. But I get an error "The key 'concurrency' is not allowed". What's wrong?

name: test

on:
  push:
    branches:
      - feature/**

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

jobs:
~~~
1 Answers

You may try to achieve this with concurrency and cancel-in-progress: true

Concurrency ensures that only a single job or workflow using the same concurrency group will run at a time. A concurrency group can be any string or expression. The expression can only use the github context. For more information about expressions, see "Context and expression syntax for GitHub Actions."

You can also specify concurrency at the job level. For more information, see jobs.<job_id>.concurrency.

When a concurrent job or workflow is queued, if another job or workflow using the same concurrency group in the repository is in progress, the queued job or workflow will be pending. Any previously pending job or workflow in the concurrency group will be canceled. To also cancel any currently running job or workflow in the same concurrency group, specify cancel-in-progress: true.

However

Note: Concurrency is currently in beta and subject to change.

Here is an example workflow:

name: Deploy

on:
  push:
    branches:
      - main
      - production
    paths-ignore:
      - '**.md'


# Ensures that only one deploy task per branch/environment will run at a time.
concurrency:
  group: environment-${{ github.ref }}
  cancel-in-progress: true

jobs:

  build:
    runs-on: ubuntu-latest
    steps:
      - name: Extract commit
        run: |
          echo "Sending commit $GITHUB_SHA for $GITHUB_REPOSITORY"
Related