Accessing dynamic variable name in github actions

I want to access value of dynamic variable name which in effect is c_ip1, c_ip2 How do I construct this in github actions workflow ?
Currently I am getting error if I access as below:

val=“needs.build.outputs.c_ip$ip”
echo ${!val}

on: [push]


jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      c_ip1: ${{ steps.set_c.outputs.c_ip1 }}
      c_ip2: ${{ steps.set_c.outputs.c_ip2 }}

    strategy:
      matrix:
       include:
         - index: 1
           deploy: "dev" 
         - index: 2
           deploy: "prod" 
    steps:

      - id: set_c
        run: |
          res=10
          if [ ${{ matrix.index }} = "dev" ]; then
            echo "::set-output name=c_ip1::$res"
          else
            res=5
            echo "::set-output name=c_ip2::$res"
              
          fi
        continue-on-error: true     

  upgrade-c2:
    name: another job
    needs: build
    runs-on: ubuntu-latest

    strategy:
      matrix:
       include:
         - index: 1
           deploy: "dev" 
         - index: 2
           deploy: "prod" 


    steps:
    - run: |
         ip=${{ matrix.index }}
         val="needs.build.outputs.c_ip$ip"
         if [ ${{ matrix.deploy }} = "dev" ]; then
           #echo ${{ needs.build.outputs.c_ip[$ip] }}
           echo ${!val}
         else
           #echo ${{ needs.build.outputs.c_ip[$ip] }}
           echo ${!val}
         fi
      continue-on-error: 

GHA contexts are only accessible in workflow expressions ${{ }}. Mixing them with shell commands like that will not work. I think you should do this:

echo ${{ needs.build.outputs[format('c_ip{0}', matrix.index)] }}

1 Like

Thanks it worked. Able to fetch the values.