【问题标题】:github action combine workflow_dispatch and push in the same workflowgithub action 结合 workflow_dispatch 和 push 在同一个工作流中
【发布时间】:2022-01-23 17:45:38
【问题描述】:

我试图弄清楚如何在同一工作流程中结合手动触发器和其他触发器(例如推送)

这是我的手动操作


on:
  workflow_dispatch:
    inputs:
      environment:
        type: environment
        default: DEV
        required: true

env:
  ENVIRONMENT: ${{ github.event.inputs.environment }}
.
.
.

我想要类似的东西

on:
  push:
    branches:
    - main
    - dev
  workflow_dispatch:
    inputs:
      environment:
        type: environment
        default: DEV
        required: true

env:
  ENVIRONMENT: ${{ github.event.inputs.environment }} or {{ DEV if dev }} or {{ PROD if main }}
.
.
.

【问题讨论】:

  • 您无法使用 ENV 字段中的表达式来实现此目的。您必须在作业步骤中根据分支名称评估和设置环境变量,然后将其添加到 GITHUB_ENV 或与输出共享。

标签: github-actions


【解决方案1】:

这是一种方法:

name: Print environment variable

on:
  push:
    branches:
      - master
      - development
  workflow_dispatch:
    inputs:
      environment:
        type: string
        default: DEV
        required: true

jobs:    
  prod:
    if: ${{ github.event_name == 'push' && github.ref_name == 'master' || github.event.inputs.environment == 'PROD' }}
    env:
      environment: PROD
    runs-on: ubuntu-latest
    steps:
      - name: Print value
        run: echo ${{ env.environment }}
        
  dev:
    if: ${{ github.event_name == 'push' && github.ref_name == 'development' || github.event.inputs.environment == 'DEV' }}
    env:
      environment: DEV
    runs-on: ubuntu-latest
    steps:
      - name: Print value
        run: echo ${{ env.environment }}

当然,如果您对两种环境都有相同的步骤并且不想重复它们,那么可以考虑使用reusable workflows

更新:

只是为了澄清为什么我使用类型string 作为环境。是的,我注意到您使用environment 作为输入类型,但文档在这方面不太清楚。 Here 表示输入只能是booleannumberstring 类型,但在示例中here 不仅显示environment 类型,还显示choice 类型。

【讨论】:

  • 请注意我的环境类型是环境而不是字符串
猜你喜欢
  • 2023-01-30
  • 2021-10-26
  • 2021-12-28
  • 2021-10-20
  • 2020-09-11
  • 2023-02-10
  • 2022-10-02
  • 1970-01-01
  • 2020-09-03
相关资源
最近更新 更多