6

I'm using composite GitHub actions, where I want to check the current branch name in composite action's some steps and make the decision on that condition.

e.g.

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

Where in Composite action - I do have a check that Sonar-Scanner should execute only for develop branch, else only project build will gets execute.

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

Here and here are the references I have looked for before applying this if condition here. And that works totally fine if I apply that main.yml, but in composite action yaml file that's not working.

Can someone please share that what am I missing in here?

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 3
    `if` is not supported in composite run steps actions, only `run`, `shell`, `name`, `id`, `env` and `working-directory`, see [documentation](https://docs.github.com/en/free-pro-team@latest/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions), and [this GitHub issue](https://github.com/actions/runner/issues/646) where "we don't support setting conditionals" is mentioned. – Benjamin W. Dec 28 '20 at 06:29
  • Thank you @BenjaminW. In case if it is a right channel, can you please let me know if there is any other approach I can use to achieve what I have explained? – I Love Stackoverflow Dec 28 '20 at 06:47
  • 2
    You can make it a condition in the `run` step; I don't know PowerShell, but in Bash it would be something like `run: "[[ $GITHUB_REF == 'ref/head/develop' ]] && "` – Benjamin W. Dec 28 '20 at 20:34

3 Answers3

5

The above syntax if: ${{ ... }} should be fine but it seems that the composite actions do not support conditions yet: https://github.com/actions/runner/blob/main/docs/adrs/0549-composite-run-steps.md

The workaround that Benjamin proposed is working for me:

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

shell: bash
Joachim
  • 308
  • 1
  • 3
  • 10
  • 2
    This is now supported : https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsif – sbernard Jun 16 '22 at 08:51
  • 2
    It does now seem to be supported. Although note that boolean conditionals need to be if: ${{ inputs.myVar == true }} and not if: ${{ inputs.myVar }}. – Maggie Nov 07 '22 at 10:59
  • I think it would be useful to update this answer with the syntax reported by @Maggie, since the github documentation about this topic is quite confusing. – Alessandro Muntoni Jun 23 '23 at 12:35
  • @Maggie IME I needed `inputs.myVar == 'true'`, with the single quotes – Ben Millwood Jul 24 '23 at 12:07
0

if used to be unsupported in composite actions, but is now supported (sounds like since Nov 2021). See GitHub's documentation on what does or does not work there.

One thing that tripped me up using these is that inputs are all strings, and in particular if: inputs.enable-deploy will run the step if inputs.enable-deploy is non-empty, including if it's the non-empty string false. Use if: inputs.enable-deploy == 'true' instead.

Ben Millwood
  • 6,754
  • 24
  • 45
  • I debated whether to make this community wiki since it's mostly things that I learned from other comments, but eh, I did some of my own legwork as well. Happy to convert if people think that's right. – Ben Millwood Jul 24 '23 at 11:45
-6

Try the below syntax:

if: ${{ github.ref == 'refs/heads/xxxx' }}
buzatto
  • 9,704
  • 5
  • 24
  • 33
Singaravelan
  • 57
  • 1
  • 2