0

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.

Doug Fir
  • 19,971
  • 47
  • 169
  • 299

1 Answers1

3

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}
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • just `github.event.inputs.db_schema` in case – Matteo Sep 20 '22 at 15:39
  • 1
    @Matteo `inputs` is accessible directly, access via `github.event` is there to maintain compatibility (see [here](https://github.blog/changelog/2022-06-10-github-actions-inputs-unified-across-manual-and-reusable-workflows/)). – Benjamin W. Sep 20 '22 at 15:40
  • Thanks for the info. I tried the first suggestion but get error: "The workflow is not valid. .github/workflows/update-sk4-caller.yaml (Line: 32, Col: 18): Unrecognized named-value: 'env'. Located at position 1 within expression: env.db_schema .github/workflows/...r.yaml (Line: 37, Col: 18): Unrecognized named-value: 'env'. Located at position 1 within expression: env.db_schema". For your second suggestion, the issue is I need to pass this to a `with`. Can I use an expression when passing a value within a `with` do you know? – Doug Fir Sep 20 '22 at 16:05
  • I added a more complete snippet from my yaml, I'm hoping my goal is more clear after that. – Doug Fir Sep 20 '22 at 16:15
  • 1
    Expressions are evaluated before anything else, so IMO you should be able to use `db_schema: ${{ inputs.db_schema || 'US.DATA_SCIENCE' }}` in the call to your reusable workflow, no `env` required. – Benjamin W. Sep 20 '22 at 18:19