我的 GitHub 工作流程如下所示:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "build"
test:
runs-on: ubuntu-latest
needs: build
steps:
- run: echo "build"
deploy:
runs-on: ubuntu-latest
needs: [build, test]
steps:
- run: echo "deploy"
Run Code Online (Sandbox Code Playgroud)
现在,我想使该deploy步骤仅tests在分支为 时才需要该步骤main。
那可能吗?
本质上我想做这样的事情:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "build"
test:
runs-on: ubuntu-latest
needs: build
steps:
- run: echo "build"
deploy:
runs-on: ubuntu-latest
needs: contains(branch, "master") ? [build, test] : [build] #notice this line
steps:
- run: echo "deploy"
Run Code Online (Sandbox Code Playgroud)
Jan*_*uak 17
GitHub 操作不允许有条件的需求(不幸的是,恕我直言)。
然而,有一个解决方法:通过在needs. 无论先决条件发生什么,所有需要运行的作业都需要将if条件设置为always()。
如果先前作业的失败应该导致终止,您需要添加一个步骤来验证作业结果。
这是一个通用的适配解决方案:
jobs:
init_job:
runs-on: ubuntu-latest
steps:
- run: echo "initial run"
conditional_job:
runs-on: ubuntu-latest
needs: init_job
if: ${{ your condition here }}
steps:
- run: echo "conditonal run"
final_job:
needs: conditional_job
if: always()
steps:
- name: fail if conditional job failed
if: ${{ needs.conditional_job.result == 'failure' }}
run: exit 1
- run: echo "final run"
Run Code Online (Sandbox Code Playgroud)
不理想,但你可以使用这个:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "build"
test:
runs-on: ubuntu-latest
needs: build
steps:
- run: echo "build"
deploy-main:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
needs: [build, test]
steps:
- run: echo "deploy"
deploy:
runs-on: ubuntu-latest
if: github.ref != 'refs/heads/main'
needs: build
steps:
- run: echo "deploy"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16323 次 |
| 最近记录: |