如何有条件地运行github工作流作业,并在作业之间传递env-var



我的github工作流.yml文件中有这个

name: Release workflow (pipeline)
# Enable Buildkit and let compose use it to speed up image building
env:
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
RELEASE_ID: ''  # this env var will be modified in Create release job > Set release number step, so to be used in the deploy job
on:
workflow_dispatch:
inputs:
environment:
description: Environment to create the release for
required: true
type: choice
options:
# - dev
- prod
default: prod
should_auto_deploy:
description: Auto-deploy the release
required: false
type: boolean
default: 'false'
# some other lines
jobs:
create_release:
name: Create release
needs: run_tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
# i have this line in the bash script for this step, to modify the global env var above
# echo "RELEASE_ID=$releaseId" >> $GITHUB_ENV
- name: Set release number
run: |
chmod +x ./scripts/github-workflow/set-release-number.sh
./scripts/github-workflow/set-release-number.sh 
-e ${{ github.event.inputs.environment }} 
-n $(date +%Y-%m-%d)-${{ github.ref_name }}-${{ github.run_id }}
# some other lines
deploy:
name: Deploy release
needs: create_release
# this is the problematic condition.
# this job ALWAYS runs even when i dont tick the checkbox to set `github.event.inputs.should_auto_deploy` to true
if: ${{ github.event.inputs.should_auto_deploy && needs.create_release.result == 'success' }}
uses: Aplanke/ApiBackend/.github/workflows/deployment.yml@main
with:
environment: ${{ github.event.inputs.environment }}
# the updated env var should be passed to this job
release_number: ${{ github.env.RELEASE_ID }}

我的期望:

  1. 在"如何在github工作流作业之间传递数据"中询问
  2. 只有当github.event.inputs.should_auto_deploy为true并且create_release作业成功时,才应运行部署作业

发生了什么:deployjub始终运行,即使github.event.inputs.should_auto_deploy为假(未在UI中选择(

我该如何修复这些?

GitHub操作中的布尔值不是真正的布尔值,你可以在这里了解这个问题。

您必须使用:{{ github.event.inputs.should_auto_deploy == 'true' }}

对于ENV变量——使用这样的ENV变量真的很糟糕——这里中描述了在作业之间交换数据的明确方式

最后,这个工作流程应该是这样的:

jobs:
create_release:
name: Create release
needs: run_tests
runs-on: ubuntu-latest
outputs: ${{ steps.set_release_number.release_id }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set release number
id: set_release_number
run: echo "::set-output name=release_id::RELEASE_ID"
deploy:
name: Deploy release
needs: create_release
if: ${{ github.event.inputs.should_auto_deploy == 'true' && needs.create_release.result == 'success' }}
uses: Aplanke/ApiBackend/.github/workflows/deployment.yml@main
with:
environment: ${{ github.event.inputs.environment }}
release_number: ${{ needs.create_release.outputs.release_id }}

最新更新