【问题标题】:If Else in Github action如果在 Github 中的操作
【发布时间】:2021-05-28 03:09:42
【问题描述】:

我正在关注这个。

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 }}

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

就像在 env 中确定提交 SHA。

env:
  COMMIT_HASH: ${{ github.event.inputs.git-ref != '' && github.event.inputs.git-ref || github.sha }}

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

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    您可以考虑使用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 }}
    

    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 }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 2020-10-08
      • 2022-09-25
      • 2023-03-04
      • 2020-11-18
      • 1970-01-01
      • 2022-12-04
      相关资源
      最近更新 更多