Set an env variable using bash command that checks for existence of input variable

Viewed 23

I've been reading about expressions.

I need to create an env var that all jobs in a workflow can reference that, in English, checks for the existence of ${{ inputs.db_schema }} and if it exists, use it, otherwise set it to 'prod'.

Tried (borrows from JWLs solution on this SO post):

env:
  db_schema: ${{ ${{inputs.db_schema}} :-'prod }}

This returned an error when I tried to run:

The workflow is not valid. .github/workflows/main.yaml (Line: 17, Col: 14): Unexpected symbol: '${{inputs'. Located at position 1 within expression: ${{inputs.db_schema .github/workflows/update-sk4-caller.yaml (Line: 18, Col: 16): Unexpected symbol: '${{inputs'. Located at position 1 within expression: ${{inputs.update_date

How can I create an env variable that can be used by all jobs in a workflow where the value is either what exists in ${{inputs.db_schema}} else if that input doesn't exist then 'prod'?

[EDIT]

Adding a more complete example of what I"m trying to do. Here's a piece of my workflow:

name: MyWorkflow
on:
  workflow_dispatch:
    inputs:
      db_schema:
        required: true
        type: string
        default: US.DATA_SCIENCE
  schedule:
    - cron:  '10 3 * * *' # daily at ten past 3am

env:
  db_schema: ${{inputs.db_schema || 'US.DATA_SCIENCE'}}

jobs:
  check-env-vars:
    runs-on: ubuntu-latest
    steps:
      - name: check env vars
        run: |
          echo ${{ env.db_schema }}
  update-clicks:
    uses: ./.github/workflows/update-clicks.yaml
    secrets: inherit
    with:
      db_schema: ${{ env.db_schema }}

I need to pass a value to db_schema within the with statement.

1 Answers

You're mixing access to GitHub Actions context and shell parameter expansion. You might be able to do something like

  db_schema: ${{ inputs.db_schema || 'prod' }}

or if you access db_schema in a shell script, you could use

  db_schema: ${{ inputs.db_schema }}

and then access via

${db_schema:-prod}
Related