Mark
Mark

Reputation: 5513

How to read environment variables in env section of github action workflow

I'm trying to set a env variable based on another env variable in a github workflow. I've tried a couple of syntax options but none seem to work

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  workflow_dispatch:

env:
  BASE_VERSION: 1.0.0
  FULL_VERSION: ${BASE_VERSION}-${{ github.run_number }}-${{ github.ref_name }}
jobs:
  1. The example for BASE_VERSION above just keeps ${BASE_VERSION} as a string
  2. $BASE_VERSION also just keeps $BASE_VERSION as a string
  3. ${{ env.BASE_VERSION }}-blabla just fails with syntax error

Is this doable?

The output I want is "1.0.0-1-master" for example

Upvotes: 20

Views: 17457

Answers (3)

Flo
Flo

Reputation: 139

As @lyzlisa stated, it does not work for env variable at the same level, but it does work if you reuse a top level env variable in a lower level env variable definition:

env:
  SOME_GLOBAL_VAR: 1.0.0
jobs:
  build:
    name: My build
    env:
      SOME_BUILD_VAR: "${{ env.SOME_GLOBAL_VAR }}-build"
    steps:
      - name: My step
        env:
          SOME_STEP_VAR: "${{ env.SOME_GLOBAL_VAR }} ${{ env.SOME_BUILD_VAR}} step 1"
        run:
          ...

Upvotes: 1

Stefano L
Stefano L

Reputation: 1593

Do it like this:

      - name: Set docker image env var
        run: |
          echo "DOCKER_IMAGE=${ARTIFACTORY_URL}/${IMAGE_NAME}:${GITHUB_REF##*/}.${{github.run_number}}" >> $GITHUB_ENV

      - run: |
          echo ${{ env.DOCKER_IMAGE }}

Outputs

artifactory-host/some-project/some-repo/image-name:branch.number

Upvotes: 11

lyzlisa
lyzlisa

Reputation: 651

Is this doable?

It does not seem like a supported behaviour at the moment.

The docs on env mentions that

variables in the env map cannot be defined in terms of other variables in the map.

Upvotes: 16

Related Questions