GitHub Action - 无法在步骤中添加“if”条件

Pra*_*oni 6 continuous-integration github github-actions

我正在使用复合 GitHub actions,我想在复合操作的某些步骤中检查当前分支名称,并根据该条件做出决定。

例如

name: main

on:
  push:
  repository_dispatch:
    types:
      - manual-trigger

jobs:
  build:
    runs-on: windows-latest
    steps:
    - name: Checkout project
      uses: actions/checkout@v2

    - name: Fetch full project
      run: git fetch --prune --unshallow

    - name: Restore packages
      run: nuget restore -ConfigFile "../Build/Nuget.config"
      working-directory: Projects
      env:
        # ARTIFACTORY_PASSWORD is read by the nuget.config credentials section
        ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}

    - name: Composite Action - To build solution and execute SonarScanner
      uses: ./Build/build-and-sonarscanner-execution
Run Code Online (Sandbox Code Playgroud)

在复合操作中,我确实检查了声纳扫描仪应该只为开发分支执行,否则只有项目构建才会执行。

name: build-and-sonarscanner-execution
description: "Build the solution using msbuild and SonarScanner execution"

inputs:
  # Set of parameters

runs:
  using: "composite"
  steps: 
    - name: Restore packages
      run: nuget restore -ConfigFile "../Build/Nuget.config"
      working-directory: ${{ inputs.solution-directory }}
      env:
        # ARTIFACTORY_PASSWORD is read by the nuget.config credentials section
        ARTIFACTORY_PASSWORD: ${{ inputs.artifactory-password }}
      shell: pwsh

    - name: Install dotnet-sonarscanner package
      if: {{ github.ref == 'ref/head/develop' }}  This is the line where it is throwing syntax error as 'if' is not allowed under steps
      run: dotnet tool install --global dotnet-sonarscanner --version 4.10.0
      shell: pwsh
Run Code Online (Sandbox Code Playgroud)

这里这里是我在此处应用此 if 条件之前查找的参考资料。如果我应用 main.yml,那么效果完全没问题,但在复合操作 yaml 文件中则不起作用。

有人可以分享一下我在这里缺少什么吗?

Joa*_*him 5

上面的语法if: ${{ ... }}应该没问题,但似乎复合操作还不支持条件:https://github.com/actions/runner/blob/main/docs/adrs/0549-composite-run-steps.md

本杰明提出的解决方法对我有用:

run: |
  if [[ "${{inputs.myVar}}" != "" ]]; then
    aws ...
  fi
  exit 0

shell: bash
Run Code Online (Sandbox Code Playgroud)

  • 现在看来确实是支持的。但请注意,布尔条件必须是 if: ${{inputs.myVar == true }} 而不是 if: ${{inputs.myVar }}。 (3认同)
  • 现在支持:https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsif (2认同)

小智 -6

尝试以下语法:

if: ${{ github.ref == 'refs/heads/xxxx' }}
Run Code Online (Sandbox Code Playgroud)