GitHub Action - Unable to add "if" condition in steps

Viewed 5870

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?

2 Answers

Try the below syntax:

if: ${{ github.ref == 'refs/heads/xxxx' }}
Related