使用输入参数调度触发 Github Action 工作流程。

14

我希望以两种方式运行我的 Github 工作流:

  1. 由用户手动运行
  2. 定时任务

现在,当我添加了输入参数后,定时任务可以正常运行,但是没有使用默认值。

以下是我的 YAML 文件:

name: WebDriverIO Automation
on:
  workflow_dispatch:
    inputs:
        typeOfTesting:
          type: choice
          description: Select Type of Test
          default: 'stage-test-local-All'
          required: true
          options: 
          - stage-test-local-All
          - stage-test
          - stage-test-local-Sanity
          - prod-test
    branches:
      - workingBranch
      - JSNew
  schedule:
    - cron: "*/5 * * * *"

1
输入与cron触发器无关。您需要在工作流程中处理此问题,例如“如果未设置Testing类型,则设置默认值”。 - rethab
6个回答

16

inputs 是仅在 workflow_dispatch 事件触发时(以及由调度工作流程调用的任何工作流程)可用的。

您可以使用 || 表达式运算符为输入参数设置默认值。例如:

on:
  schedule:
    - cron: '15 0,18 * * 0-5'
  workflow_dispatch:
    inputs:
      logLevel:
        required: true
        type: string

jobs:
  test:
    uses: ./.github/workflows/run-job-with-params.yml
    secrets: inherit
    with:
      springProfile: 'production'
      logLevel: ${{ inputs.logLevel || 'DEBUG' }}

在上面的示例中,当工作流由cron触发时,logLevel参数设置为DEBUG。当作业由调度事件触发时,它会被设置为输入值。

1
这通常不起作用(对于布尔输入参数)。 - Connor Clark
有没有一种方法可以通过env上下文传递DEBUG,以避免硬编码值? - pkaramol
就像@ConnorClark所描述的那样,这对布尔参数不起作用。经过多次测试,我们没有找到一个好的解决方案 - 因此我们最终只使用了choice类型,这似乎是有效的(我们针对特定的选择或空字符串进行测试以判断"schedule-value")。 - Anigif
使用布尔输入时,您可以使用${{ inputs.myBoolean || github.event_name == 'schedule' }}这样的语法。 - undefined

1
这是另一个版本,它适用于布尔输入,并且还支持将默认值设置为true。
name: Conditionally run a job

on:
  workflow_dispatch:
    inputs:
      should_run:
        description: "Whether the job should run"
        type: boolean
        default: true
schedule:
  - cron: '0 * * * *'

jobs:
  my-job:
    name: This job runs on schedule or if inputs.should_run is true
    runs-on: ubuntu-22.04
    if: ${{ !contains(inputs.should_run, 'false') }}
    steps:
    run: echo ok

这个工作按预期运行:作业总是按计划运行,当手动触发时,只有在检查到inputs.should_run为真时才运行。这是因为:
  • 如果工作流按计划运行,则inputs.should_runnull
  • contains函数将其参数转换为字符串:null被转换为''

0
以下代码可行。
name: WebDriverIO Automation
on:
  workflow_dispatch:
    inputs:
        typeOfTesting:
          type: choice
          description: Select Type of Test
          default: 'stage-test-local-All'
          required: true
          options: 
          - stage-test-local-All
          - stage-test
          - stage-test-local-Sanity
          - prod-test
    branches:
      - workingBranch
  schedule:
    - cron: "*/5 * * * *"

...
..
..
   - name: Test
        run: |
          if [ ${{ github.event.inputs.typeOfTesting }} != "" ]; then
            npm run ${{ github.event.inputs.typeOfTesting }} 
          else 
              npm run stage-test-local-All 
          fi

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community
你可以使用 npm run ${{ github.event.inputs.typeOfTesting || 'stage-test-local-All' }} 更简洁地完成此操作。 - Joman68

0

另一个选择是设置一个单独的工作流,使用curl POST来通过调度触发所需的工作流。这样,您可以设置一个包含所需变量/上下文的有效负载。虽然为一个任务设置两个工作流不是理想的选择,但它可以让您访问完整的输入集。


0

有几条评论提到其他答案不能用于布尔输入,所以这里有一个可以使用的答案。

name: Read a boolean input in a scheduled job

on:
  schedule:
    - cron: '0 * * * *'
  workflow_dispatch:
    inputs:
      human:
        description: Did a human start this workflow?
        type: boolean
        default: true

jobs:
  report-boolean:
    name: Report boolean state
    runs-on: ubuntu-latest
    env:
      BOOLEAN_STATE: ${{ !!(inputs.human) }}
    steps:
      - name: Output boolean
        run: echo "'${BOOLEAN_STATE}'"

结果:

  • 我派遣工作流并勾选输入框 -> 'true'
  • 我派遣工作流并取消勾选框 -> 'false'
  • 计划触发 -> 'false'

如果您希望变量在预定的作业中为真,请使用一个感叹号代替两个(${{ !inputs.whatever }})。


0
你可以设置一个带有默认值的环境变量:
env:
  typeOfTesting: ${{ github.event_name == 'schedule' && 'stage-test-local-All' || github.event.inputs.typeOfTesting }}

然后使用env.typeOfTesting而不是inputs.typeOfTesting

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接