variable-mapper icon indicating copy to clipboard operation
variable-mapper copied to clipboard

Support arrays

Open mrmeyers99 opened this issue 3 years ago • 2 comments

It would be nice if this supported arrays so that we could plugin the array as a matrix variable. Not sure if this is supported by Github Actions though. I'm thinking something like this:

  determine-regions:
    outputs:
      regions: ${{ steps.export.outputs.regions }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - id: var-map
        run: |
          echo ::set-output name=content::$(cat ./.github/env_vars.json)

      - uses: kanga333/variable-mapper@master
        id: export
        with:
          key: "${{ github.event.inputs.environment }}"
          map: ${{ steps.var-map.outputs.content }}
          export_to: log,output
  deploy:
    environment: ${{ github.event.inputs.environment }}
    needs: [ determine-regions ]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        region: ${{ needs.determine-regions.outputs.regions }}
        stack: [a, b, c]
      fail-fast: true
      max-parallel: 1

where the env_vars.json looks like this:

{
 "production": {
   "regions": ["us-east-1", "us-west-2"]
 },
 "uat": {
   "regions": ["us-east-1", "us-west-2"]
 }
}

I guess the work around is to encode the JSON array in the string and then use the toJson function github provides, but it would be nice to not have to do that.

mrmeyers99 avatar Sep 24 '21 14:09 mrmeyers99

@mrmeyers99 Thank you for your report. Unfortunately, the output of Github Actions supports only string type. So, we can't export the output as an array. However, If we support export value as a JSON string, is it helpful for you?

It will be able to use like below.

determine-regions:
  outputs:
    regions: ${{ steps.export.outputs.regions }}
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v2

    - id: var-map
      run: |
        echo ::set-output name=content::$(cat ./.github/env_vars.json)

    - uses: kanga333/variable-mapper@master
      id: export
      with:
        key: "${{ github.event.inputs.environment }}"
        map: |
          {
            "production": {
              "regions": ["us-east-1", "us-west-2"]
            },
            "uat": {
              "regions": ["us-east-1", "us-west-2"]
            }
          }
        export_to: log,output
        # Flag to serialize map value as a JSON string
        serialize_json: true
deploy:
  environment: ${{ github.event.inputs.environment }}
  needs: [ determine-regions ]
  runs-on: ubuntu-latest
  strategy:
    matrix:
      # using with fromJson, because outputs value is JSON string.
      region: ${{ fromJson(needs.determine-regions.outputs.regions) }}
      stack: [a, b, c]
    fail-fast: true
    max-parallel: 1

In this example, variable-mapper outputs the serialized JSON string ["us-east-1", "us-west-2"]. And user can use it with formJson. I will try to work on this problem.

kanga333 avatar Oct 07 '21 00:10 kanga333

Yeah that would work, thanks!

mrmeyers99 avatar Nov 10 '21 16:11 mrmeyers99