ross flood
ross flood

Reputation: 41

github actions workflow call if statement with custom env vars

If I have set a custom env var like so:

env:
  web_app_1: none

and then I want to run a workflow based off of an if expression:

  build_artifact:
      uses: ./.github/workflows/X-Call-build-artifact.yml
      if: web_app_1 != 'none'
      secrets: inherit
      with:
        artifact-name:  $web_app_1 #put in the functions or web app names you want to publish
        path: $web_app_1_artifact_path # and path to it

it return the error: The workflow is not valid. .github/workflows/Dev-Pipeline.yml (Line: 37, Col: 11): Unrecognized named-value: 'web_app_1'. Located at position 1 within expression: web_app_1 != 'none'

I tried using ${{ web_app_1 }}, ${{ enn.web_app_1 }}, and $web_app_1

all giving similar errors...

if there not a way to call a workflow with an if conditional?

Upvotes: 3

Views: 3768

Answers (2)

Avishek Dhar
Avishek Dhar

Reputation: 51

In my framework the approach we have taken for below error when calling a custom variable in if loop.enter image description here

How we solve above issue shown in image by creating a Job saying prepare matrix:

env: CX_SCHEDULE_PLATFORMS: ${{ secrets.CX_platform }}

jobs:
prepare-matrix:
    runs-on: ubuntu-latest
    outputs:
      platforms: ${{ steps.platform-matrix.outputs.matrix }}
    steps:
      # Step 1: Retrieve and parse PLATFORMS
      - name: Platform Matrix
        id: platform-matrix
        env:
          PLATFORMS: ${{ env.CX_SCHEDULE_PLATFORMS }}
        run: |
          echo "matrix=$PLATFORMS" >> $GITHUB_OUTPUT

job2: #execute the scheduled job on saturday in 4 slots
needs: prepare-matrix
if: ${{ needs.prepare-matrix.outputs.platforms == 'true' && github.event.schedule == '0 19,1,7,1 * * 6' }}
# if: contains(env.CX_SCHEDULE_PLATFORMS, 'true') this doesnot work for error shared as image
runs-on: ubuntu-latest
permissions:
  id-token: write
  contents: read
continue-on-error: true
strategy:

Hope this solves the issue of calling custom env variables directly in conditional loop

Upvotes: 0

ross flood
ross flood

Reputation: 41

This is a work around I figured out. Should be a more elegant solution but it works...

env:
  web_app_1: none
  web_app_1_artifact_path: 'Code/location/'
  function_1: none
  function_1_artifact_path: 'Code/location/' 


jobs:

  check_resources:
    runs-on: ubuntu-latest
    outputs:
      has_web_app: ${{ steps.web.outputs.declared }}
      has_function: ${{ steps.function.outputs.declared }}
    steps:
      - name: Check web app
        id: web
        if: ${{ env.web_app_1 != 'none' }}
        run: echo "::set-output name=declared::true"
      - name: Check functions
        id: function
        if: ${{ env.function_1 != 'none' }}
        run: echo "::set-output name=declared::true"

  build_artifact:
      uses: ./.github/workflows/X-Call-build-artifact.yml
      needs: [check_resources]
      if: ${{ (needs.check_resources.outputs.has_web_app == 'true') && (needs.check_resources.outputs.has_function == 'true')}}
      secrets: inherit
      with:
        artifact-name:  $web_app_1 #put in the functions or web app names you want to publish
        path: $web_app_1_artifact_path # and path to it

essentially adds a job to make the if statement on the set env var then sets the output values, then the workflow call job uses the output on the if.

Upvotes: 1

Related Questions