Environment variable in GitHub Actions not interpolated correctly

Viewed 230

In the beginning of a (re-usable) workflow

env:
  STAGING_GCR_PROJECT: my-project-id

and when using it

  slack_staging_success:
    needs: build_staging
    runs-on: ubuntu-latest
    if: ${{ always() && contains(join(needs.*.result, ','), 'success') }}
    env:
      STAGING_IMAGE: "gcr.io/$STAGING_GCR_PROJECT/${{ inputs.image_name }}:${{ inputs.image_tag }}"

    
    steps:
      
    - name: slack success for staging
      uses: rtCamp/action-slack-notify@v2
      env:
        SLACK_ICON: $SLACK_ICON_SUCCESS
        SLACK_COLOR: green
        SLACK_MESSAGE: "STAGING image ${{ env.STAGING_IMAGE }} was built / pushed with SUCCESS"

In Slack the message interpolates as

STAGING image gcr.io/$STAGING_GCR_PROJECT/echo-server:1.0.2 failed to be built

Why isn't $STAGING_GCR_PROJECT interpolated correctly?

1 Answers

In order to use workflow environment variables, use the prefix env. and enclose it in ${{ }} just like you did with the inputs:

env:
  STAGING_IMAGE: "gcr.io/${{ env.STAGING_GCR_PROJECT }}/${{ inputs.image_name }}:${{ inputs.image_tag }}"
Related