如何有条件地 运行 github 工作流作业并在作业之间传递环境变量
How to run a github workflow job conditionally and pass env vars between jobs
我的 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 }}
我的期望:
- 在 How to pass data between github workflow jobs
中询问
- 只有当
github.event.inputs.should_auto_deploy
为真并且 create_release
作业成功时,部署作业才会 运行
发生了什么:
deploy
jub 总是 运行s,即使 github.event.inputs.should_auto_deploy
为假(未在 UI 中选择)
我该如何解决这些问题?
GitHub 操作中的布尔值不是真正的布尔值,您可以阅读有关问题 here。
你必须使用:
{{ github.event.inputs.should_auto_deploy == 'true' }}
对于 ENV 变量——像那样使用 ENV 变量是非常糟糕的方法——here
中描述了在作业之间交换数据的明确方法
最后这个工作流程应该是这样的:
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 }}
我的 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 }}
我的期望:
- 在 How to pass data between github workflow jobs 中询问
- 只有当
github.event.inputs.should_auto_deploy
为真并且create_release
作业成功时,部署作业才会 运行
发生了什么:
deploy
jub 总是 运行s,即使 github.event.inputs.should_auto_deploy
为假(未在 UI 中选择)
我该如何解决这些问题?
GitHub 操作中的布尔值不是真正的布尔值,您可以阅读有关问题 here。
你必须使用:
{{ github.event.inputs.should_auto_deploy == 'true' }}
对于 ENV 变量——像那样使用 ENV 变量是非常糟糕的方法——here
中描述了在作业之间交换数据的明确方法最后这个工作流程应该是这样的:
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 }}