Github 操作中的 If Else

San*_*hoo 5 github-actions

我正在关注这个。

https://github.community/t/github-actions-manual-trigger-approvals/16233/83

  - name: Clone Repository (Latest)
    uses: actions/checkout@v2
    if: github.event.inputs.git-ref == ''
  - name: Clone Repository (Custom Ref)
    uses: actions/checkout@v2
    if: github.event.inputs.git-ref != ''
    with:
      ref: ${{ github.event.inputs.git-ref }}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但多步骤会使工作流程更大。我正在尝试更紧凑的东西。

就像确定 env 中的提交 SHA 一样。

env:
  COMMIT_HASH: ${{ github.event.inputs.git-ref != '' && github.event.inputs.git-ref || github.sha }}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但对我来说看起来像是一个丑陋的黑客。有什么建议。我试图避免额外的步骤,仅此而已。

Gui*_*urd 3

您可以考虑使用haya14busa/action-cond操作。

if-else当需要操作来设置其他步骤的动态配置时(不需要重复整个步骤来在几个参数中设置不同的值),它非常有用。

例子:

- name: Determine Checkout Depth
  uses: haya14busa/action-cond@v1
  id: fetchDepth
  with:
    cond: ${{ condition }}
    if_true: '0'  # string value
    if_false: '1' # string value
- name: Checkout
  uses: actions/checkout@v2
  with:
    fetch-depth: ${{ steps.fetchDepth.outputs.value }}
Run Code Online (Sandbox Code Playgroud)

或者

steps:
- uses: haya14busa/action-cond@v1
  id: condval
  with:
    cond: ${{ github.event_name == 'pull_request' }}
    if_true: "value for pull request event"
    if_false: "value for non pull request event"
- name: Use conditional value
  run: echo "${{ steps.condval.outputs.value }}"
Run Code Online (Sandbox Code Playgroud)