Search code examples
github-actions

Use a step output with a variable name


I have this (real but simplified) workflow, where I would like in short to nest ${{ ${{...}} }}. In all cases, 'does not work' means syntax error when I attempt to run the workflow.

- steps:
  - name: login to AWS
    id: login-aws
    uses: <some custom action finding automagically the aws account id>
    # has an output: account-id

  - name: Login to ECR
    id: login-ecr
    uses: aws-actions/amazon-ecr-login@v2
    with:
      mask-password: 'false' # needed to be able to use the password in the next step
    # has an output which uses the aws account id in the variable name

  - name: sort out variables
    run: |
      # what I would like 
      # nested ${{ does not work
      echo ${{ steps.login-ecr.outputs.docker_username_${{ steps.login-aws.outputs.account-id }}_dkr_ecr_eu_central_1_amazonaws_com }}

      # another attempt, still does not work
      accountid=${{ steps.login-aws.outputs.account-id }}
      echo ${{ format(steps.login-ecr.outputs.docker_username_{0}_dkr_ecr_eu_central_1_amazonaws_com, $accountid) }}" password_{0}_dkr_ecr_eu_central_1_amazonaws_com, $accountid) }}"

How could I get this variable steps.login-ecr.outputs.docker_username_{0} where {0} is itself a variable?


Solution

  • This was partly a stupid error (missing quotes) and partly helped by @jonrsharpe comment:

    echo ${{ steps.login-ecr.outputs[format('docker_username_{0}_dkr_ecr_eu_central_1_amazonaws_com', steps.login-aws.outputs.account-id)] }}
    

    format() needs quotes, and using the index ([]) operator instead of the dot notation, format can be properly used.