如何轻松地让 Github Actions 在某些条件下跳过后续作业执行?

Pat*_*tus 18 jobs yaml skip devops github-actions

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

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

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

我试图将if条件放在工作本身上,但它不起作用。if即使条件值为 false,作业也会执行。如果存储库过时,除了使工作失败之外,还有其他更好或更优雅的解决方案吗?

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 
Run Code Online (Sandbox Code Playgroud)

sol*_*tex 18

根据文档

当您在 if 条件中使用表达式时,您可以省略表达式语法 (${{ }}),因为 GitHub 会自动将 if 条件计算为表达式,除非表达式包含任何运算符。如果表达式包含任何运算符,则表达式必须包含在 ${{ }} 中以显式将其标记为求值。

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

if: ${{ needs.check-if-there-are-commits.outputs.alive == 'true' }}
Run Code Online (Sandbox Code Playgroud)