How to create a variable of variables (concatination)

Hi,

I’m trying to create a version environment variable (I know there are better ways, but this is just a quick fix).

name: Docker Image CI

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

env:
  major: 0
  minor: 0
  build: ${{ github.run_number }}
  #version: '${{ env.major }}.${{ env.minor }}'
  
jobs:
  # This workflow contains a single job called "build"
  
  build:
    env: 
      VERSION: '${{ env.major }}'

From the snippet of code above env.version (commented out) doesn’t work and neither does jobs.build.env.VERSION work. Both result in the error:

The workflow is not valid. .github/workflows/pdk-ci.yml (Line: 31, Col: 16): Unrecognized named-value: 'env'. Located at position 1 within expression: env.major

It seems to be the case that one cannot define a variable that contains other variables. Is this the case, or am I doing something wrong? However, env.build doesn’t complain but is this because it contains a default environment variable?

T. I. A.

Until someone can share a better solution, this clunky way works:

- name: 1.2 Set variables
        id: vars
        run: |
          echo Setting version variable.
          echo ::set-output name=version::$echo '${{env.major}}.${{env.minor}}.${{github.run_number}}'

And to consume:

- name: 1.3 Get Version variable
        id: url
        run: |
          echo Getting version '${{steps.vars.outputs.version}}'
        

Although, in this way, I have to repeat the step for each job.

T.I.A

If you define that version output as a job output (see jobs.<jobs_id>.outputs) other jobs should be able to reuse it, as long as they depend (needs) on the job that creates the version variable. Still not ideal, but maybe a little simpler.

Hi @woter1832,

Yes, top level env and job level env variables cannot be concatenated.
You can use ‘set-env’ command instead, it will set&update env context for latter steps in the job.

      - name: set env
        run: |
          echo "::set-env name=version::$(echo '${{env.major}}.${{env.minor}}.${{github.run_number}}')"   
      - name: check env
        run: |
          echo $version    
          echo ${{ env.version }}

image

If you have mutliple jobs, please follow airtower-luna’s comment, use jobs level ouput to deliver env value to the latter job with needs keyword.

Thanks