如何在所有条件作业之后运行 github 操作作业,即使它没有运行?

net*_*djw 14 github github-actions

我想创建一个带有一些条件作业的 GitHub Action。我想并行运行这些条件作业,但完成后我想继续执行另一项需要等待完成所有条件作业的作业。

这是我想要实现的工作流程:

  1. 检测模板文件夹中的文件更改
  2. 在每个模板文件夹中运行npm run prod更改的内容
  3. 等待所有模板构建完成
  4. 继续部署过程

这是我的deploy.yml文件:

name: Deploy

on:
  push:
    branches:
      - deploy

jobs:
  check-theme-changes:
    name: Check template changes
    outputs:
      run_job: ${{ steps.check_files.outputs.run_job }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

      - name: check modified files
        id: check_files
        run: |
          echo "=============== list modified files ==============="
          git diff --name-only HEAD^ HEAD
          
          echo "========== check paths of modified files =========="
          git diff --name-only HEAD^ HEAD > files.txt
          while IFS= read -r file
          do
            echo $file
            if [[ $file != laravel/resources/themes/project_a/* ]]; then
              echo "::set-output name=run_project_a_build::false"
              break
            else
              echo "::set-output name=run_project_a_build::true"
              break
            fi
          done < files.txt
            
          while IFS= read -r file
          do
            if [[ $file != laravel/resources/themes/project_b/* ]]; then
              echo "::set-output name=run_project_b_build::false"
              break
            else
              echo "::set-output name=run_project_b_build::true"
              break
            fi
          done < files.txt

  build-theme-project-a:
    name: Build project_a theme
    needs: check-theme-changes
    if: needs.check-theme-changes.outputs.run_project_a_build == 'true'
    runs-on: ubuntu-latest
    steps:
    - name: Read current version
      id: CURRENT
      run: echo "::set-output name=VERSION::$(cat /var/www/latest)"
    - name: Install dependencies
      run: |
        cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
        npm ci
    - name: npm run prod --theme=project_a
      run: |
        cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
        npm run prod --theme=project_a
    - name: Publish build
      run: # some git command here

  build-theme-project-b:
    name: Build project_b theme
    needs: check-theme-changes
    if: needs.check-theme-changes.outputs.run_project_b_build == 'true'
    runs-on: ubuntu-latest
    steps:
    - name: Read current version
      id: CURRENT
      run: echo "::set-output name=VERSION::$(cat /var/www/latest)"
    - name: Install dependencies
      run: |
        cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
        npm ci
    - name: npm run prod --theme=project_b
      run: |
        cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
        npm run prod --theme=project_b
    - name: Publish build
      run: # some git command here

  prepare:
    name: Prepare to clone a new version
    needs: [
      build,
      build-theme-project-a,
      build-theme-project-b
    ]
    runs-on: self-hosted
    steps:
    - name: Save current date
      run: echo "$(date +'%s')" > /var/www/latest
Run Code Online (Sandbox Code Playgroud)

问题是,如果主题没有改变,变量就会改变false,然后prepare工作的需要也将跳过程序和所有下一步,因为他们期望之前的工作完成。

如何在跳过步骤后运行作业,但等待它完成?

Grz*_*ski 18

答案是使用:

if: always()
Run Code Online (Sandbox Code Playgroud)

关于你的prepare工作。

然后您可以将其与检查结果结合起来,如下所示:

prepare:
    name: Prepare to clone a new version
    needs: [
       build,
       build-theme-project-a,
       build-theme-project-b
    ]
    if: ${{ always() && !cancelled() && needs.build.result == 'success' }}
    
Run Code Online (Sandbox Code Playgroud)

以避免在作业被取消时运行它。

  • 检查上游作业中的 _any_ 失败也可能会有所帮助:`if:always() &amp;&amp; !cancelled() &amp;&amp; !contains(needs.*.result, 'failure')` 另外,在 GitHub 的最新版本中,您可以省略条件周围的“${{”和“}}”。 (6认同)