如何执行需要在 github actions 中跳过的作业的作业

问题描述 投票:0回答:2

下面的工作流程有 4 个作业。

我希望作业

conditional-job
执行,无论它所依赖的作业是否被跳过,即
skip-job
build

请让我知道是否以及如何可能。

name: My GitHub Actions Workflow

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: This job runs only on pull requests
        run: echo "JOB name build"    

  deploy:
    if: ${{ false }}  
    runs-on: ubuntu-latest
    steps:
      - name: This job runs only on pull requests
        run: echo "JOB name deploy"    
      # Your deployment steps go here...

  skip-job:
    if: ${{ false }} # This job will never run
    runs-on: ubuntu-latest
    steps:
      - name: This job is skipped
        run: echo "This job won't run because the if condition is set to false"

  conditional-job:
    needs:
      - skip-job  # This sets the 'skip-job' job as a dependency
      - deploy   # This sets the 'deploy' job as a dependency
    if: ${{ true }}
    runs-on: ubuntu-latest
    steps:
      - name: This job runs only on pull requests
        run: echo "JOB name conditional-job"

请告诉我。

if-statement github-actions jobs skip jobexecutiondecider
2个回答
0
投票

请使用表达方式

needs.skip-job.result == 'skipped' || needs. deploy.result == 'skipped'

如果您不确定结果值,您可以在添加条件之前打印它

  conditional-job:
    needs:
      - skip-job  # This sets the 'skip-job' job as a dependency
      - deploy   # This sets the 'deploy' job as a dependency
    if: ${{ true }}
    runs-on: ubuntu-latest
    steps:
      - name: This job runs only on pull requests
        run: echo "JOB name conditional-job"
      - name: check result
        run: |
         echo 'needs.skip-job.result'
         echo 'needs.deploy.result'


0
投票

如果您需要在另一个作业成功或跳过时才运行该作业,则需要将它们添加为

needs
。它使该作业等待其他作业。然后需要使用
if
使其成为条件。另外,请确保您具备
always()
以及其他条件。

name: build

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo build

  skipped:
    name: Skipped
    if: ${{ false }}
    runs-on: ubuntu-latest
    steps:
      - run: echo skipped

  conditional:
    needs: 
      - skipped
      - build
    name: Conditional
    if: |
      always() && 
      (needs.skipped.result == 'skipped' || needs.skipped.result == 'success') &&
      (needs.build.result == 'skipped' || needs.build.result == 'success')
    runs-on: ubuntu-latest
    steps:
      - run: echo ${{ needs.skipped.result }}
© www.soinside.com 2019 - 2024. All rights reserved.