如何轻松地使 Github 操作在特定条件下跳过后续作业执行?

How can I easily make Github Actions skip subsequent jobs execution on certain condition?

我有一个 YAML Github 动作脚本,它包含三个作业。每晚脚本应检查是否有任何用户提交(不是来自自动作业),然后执行每晚发布构建并将构建部署到测试环境。

我正在努力使用一个点,如果存储库中除了自动提交之外没有最近的提交,我可以跳过整个第二个和第三个作业的执行。

据我所知,我应该要么使脚本失败以跳过任何进一步的操作,要么将 if 条件设置为我拥有的每项工作的每一步,这看起来并不简洁。

我试图将 if 条件放在作业本身上,但它不起作用。即使 if 条件值为假,作业也会执行。如果存储库陈旧,是否还有其他更好或更优雅的解决方案来使工作失败?

name: Nightly script

on:
  workflow_dispatch:
  schedule:
    - cron: "0 1 * * *"  

jobs:
  check-if-there-are-commits:
    runs-on: ubuntu-latest
    outputs:
      alive: ${{ steps.check.outputs.alive }}
    steps:
      ### Activity check
      ### uses GitHub API to check last non-automagic commit in repository
      ### if it's older than a week, all other steps are skipped
      - name: Activity check
        id: "check"
        run: |
          curl -sL  -H "Authorization: bearer ${{secrets.REPO_BEARER_TOKEN}}" https://api.github.com/repos/$GITHUB_REPOSITORY/commits?sha=dev | jq -r '[.[] | select(.author.type != "Bot")][0]' > $HOME/commit.json
          echo $GITHUB_REPOSITORY
          echo $HOME
          echo $(cat $HOME/commit.json)

          date="$(jq -r '.commit.author.date' $HOME/commit.json)"
          echo "Date: $date"
          timestamp=$(date --utc -d "$date" +%s)
          echo "Timestamp: $timestamp"
          echo "Current date: $(date --utc +%s)"
          echo "Difference between the current date and time of the last commit: $(( ( $(date --utc +%s) - $timestamp ) ))"
          days=$(( ( $(date --utc +%s) - $timestamp ) / 86400 ))
          echo "Days: $days"

          alive=0

          echo "Date: $date"
          echo "timestamp: $timestamp"
          echo "days: $days"

          if [ $days -lt 1 ]; then
            echo Repository active : $days days
            alive=1
          else
            echo "[WARNING] Repository not updated : event<${{ github.event_name }}> not allowed to modify stale repository"
          fi
          echo "Alive? $alive"
          if [ $alive -eq 1 ]; then
            echo "REPO_ALIVE=true" >> $GITHUB_ENV
            echo "::set-output name=alive::true"
          else
            echo "REPO_ALIVE=false" >> $GITHUB_ENV
            echo "::set-output name=alive::false"
          fi
          echo "REPO_ACTIVITY=$days" >> $
          echo "::set-output name=days::$days"

  release:
    needs: check-if-there-are-commits
    if: ${{needs.check-if-there-are-commits.outputs.alive}} == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: "Verify"
        run: |
          echo "Alive? ${{needs.check-if-there-are-commits.outputs.alive}}"
          alive=${{needs.check-if-there-are-commits.outputs.alive}}
          if [ $alive == "true" ]; then
            echo "Alive"
          else
            echo "Dead"
            exit 1;
          fi

      - name: Next step
        if: ${{needs.check-if-there-are-commits.outputs.alive}} == 'true'
        run: |
          ...

      #- other steps...

  deploy:
    needs: [check-if-there-are-commits, release]
    if: ${{needs.check-if-there-are-commits.outputs.alive}} == 'true'
    runs-on: ubuntu-latest
    steps:
      #- other steps 

根据 documentation:

When you use expressions in an if conditional, you may omit the expression syntax (${{ }}) because GitHub automatically evaluates the if conditional as an expression, unless the expression contains any operators. If the expression contains any operators, the expression must be contained within ${{ }} to explicitly mark it for evaluation.

这意味着您的 if 必须定义为:

if: ${{ needs.check-if-there-are-commits.outputs.alive == 'true' }}