github 动作:多次推送,但我希望只在最后一次推送时完成构建

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

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

我有这样的工作流程。所以,每当我推送到 master 分支时,操作 运行。 但我希望构建仅在最后一次推送时起作用。 例如,

master 分支 - feature1 (person1)

master 分支 - feature2 (person2)

master 分支 - feature3 (person3)

在这个结构中,如果features1,2,3几乎同时合并,build会运行3次。 但我希望主分支只在最后一次合并时构建。就一次。 有没有办法做到这一点?喜欢.. 运行 推送时等待约1分钟后仅构建一次。


这是我按照您回答的方式进行的示例代码。但是我收到错误消息“不允许使用密钥 'concurrency'”。怎么了?

name: test

on:
  push:
    branches:
      - feature/**

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

jobs:
~~~

您可以尝试使用 concurrency 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.

但是

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

这是一个示例工作流程:

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"