如何在推送事件中跳过 GitHub 操作作业?

How to skip GitHub Actions job on push event?

使用 Travis CI,我们可以通过向提交添加后缀来跳过特定提交的构建。 Travis CI 中对此进行了描述。当我只编辑与代码无关并且不需要触发飞行前构建的 README.md 时,我发现此功能很实用。

[skip ci]

如何使用 GitHub 操作跳过触发 on: push 事件的作业?

name: Maven Build
on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Check-out project
      uses: actions/checkout@v1
    - name: Set up JDK 11.0.3
      uses: actions/setup-java@v1
      with:
        java-version: 11.0.3
    - name: Build with Maven
      run: mvn -B package --file pom.xml

答案摘要:

非常感谢所有提供各种实现方法的回答者。我敢打赌,每个人都需要一些关于他们问题的起源和 CI 方法的一些不同的东西。以下是为快速导航而列出的答案:

所有答案都值得点赞!如果你喜欢我的问题,你应该双重喜欢答案。

更新: 请参阅 Helmisek , which points out that Github has the functionality built-in now

如果您想跳过一些内容,我的回答才有意义 jobs/steps。


您可以尝试以下方法:

name: Maven Build
on: [push]

jobs:
  build:
    if: "!contains(github.event.commits[0].message, '[skip ci]')"
    runs-on: ubuntu-latest

    steps:
    - name: Check-out project
      uses: actions/checkout@v1
    - name: Set up JDK 11.0.3
      uses: actions/setup-java@v1
      with:
        java-version: 11.0.3
    - name: Build with Maven
      run: mvn -B package --file pom.xml

Git 2.27(2020 年第 2 季度)说明了另一种方法:用户不必总是通过 Actions 在 GitHub 上构建所有分支,用户可以指定 哪个 分支建造。

参见 commit e76eec3 (07 May 2020) by Jeff King (peff)
(由 Junio C Hamano -- gitster -- in commit dd4a287 合并,2020 年 5 月 13 日)

ci: allow per-branch config for GitHub Actions

Signed-off-by: Jeff King

Depending on the workflows of individual developers, it can either be convenient or annoying that our GitHub Actions CI jobs are run on every branch.

As an example of annoying: if you carry many half-finished work-in-progress branches and rebase them frequently against master, you'd get tons of failure reports that aren't interesting (not to mention the wasted CPU).

This commit adds a new job which checks a special branch within the repository for CI config, and then runs a shell script it finds there to decide whether to skip the rest of the tests.

The default will continue to run tests for all refs if that branch or script is missing.

There have been a few alternatives discussed:

One option is to carry information in the commit itself about whether it should be tested, either in the tree itself (changing the workflow YAML file) or in the commit message (a "[skip ci]" flag or similar). But these are frustrating and error-prone to use:

  • you have to manually apply them to each branch that you want to mark
  • it's easy for them to leak into other workflows, like emailing patches

We could likewise try to get some information from the branch name. But that leads to debates about whether the default should be "off" or "on", and overriding still ends up somewhat awkward.
If we default to "on", you have to remember to name your branches appropriately to skip CI.
And if "off", you end up having to contort your branch names or duplicate your pushes with an extra refspec.

By comparison, this commit's solution lets you specify your config once and forget about it, and all of the data is off in its own ref, where it can be changed by individual forks without touching the main tree.

There were a few design decisions that came out of on-list discussion. I'll summarize here:

  • we could use GitHub's API to retrieve the config ref, rather than a real checkout (and then just operate on it via some javascript).
    We still have to spin up a VM and contact GitHub over the network from it either way, so it ends up not being much faster.
    I opted to go with shell to keep things similar to our other tools (and really could implement allow-refs in any language you want). This also makes it easy to test your script locally, and to modify it within the context of a normal git.git tree.

  • we could keep the well-known refname out of refs/heads/ to avoid cluttering the branch namespace. But that makes it awkward to manipulate.
    By contrast, you can just "git checkout ci-config" to make changes.

  • we could assume the ci-config ref has nothing in it except config (i.e., a branch unrelated to the rest of git.git).
    But dealing with orphan branches is awkward. Instead, we'll do our best to efficiently check out only the ci/config directory using a shallow partial clone, which allows your ci-config branch to be just a normal branch, with your config changes on top.

  • we could provide a simpler interface, like a static list of ref patterns.
    But we can't get out of spinning up a whole VM anyway, so we might as well use that feature to make the config as flexible as possible.
    If we add more config, we should be able to reuse our partial-clone to set more outputs.

所以脚本是ci/config/allow-refs.sample:

#!/bin/sh
#
# Sample script for enabling/disabling GitHub Actions CI runs on
# particular refs. By default, CI is run for all branches pushed to
# GitHub. You can override this by dropping the ".sample" from the script,
# editing it, committing, and pushing the result to the "ci-config" branch of
# your repository:
#
#   git checkout -b ci-config
#   cp allow-refs.sample allow-refs
#   $EDITOR allow-refs
#   git commit -am "implement my ci preferences"
#   git push
#
# This script will then be run when any refs are pushed to that repository. It
# gets the fully qualified refname as the first argument, and should exit with
# success only for refs for which you want to run CI.

case "" in
# allow one-off tests by pushing to "for-ci" or "for-ci/mybranch"
refs/heads/for-ci*) true ;;
# always build your integration branch
refs/heads/my-integration-branch) true ;;
# don't build any other branches or tags
*) false ;;
esac

the action .github/workflows所要做的就是

  • 检查特殊分支和其中的特殊脚本:

即:

git -c protocol.version=2 clone \
  --no-tags \
  --single-branch \
  -b ci-config \
  --depth 1 \
  --no-checkout \
  --filter=blob:none \
  https://github.com/${{ github.repository }} config-repo \
&& \
cd config-repo \
&& \
git checkout HEAD -- ci/config
  • 检查推送的分支是否授权:

即:

enabled=yes
if test -x config-repo/ci/config/allow-ref &&
         ! config-repo/ci/config/allow-ref '${{ github.ref }}'
then
  enabled=no
fi

此外,对于您希望在所有推送时忽略的文件和目录 you can configure the workflow 本身:

on:
  push:
    paths-ignore:
    - 'README.md'

截至目前(2021 年 2 月)GitHub 操作默认支持此行为。默认情况下不再进行解析等。

参见:

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.

Link: Github Actions Changelog, February 2021