Giter VIP home page Giter VIP logo

variable-mapper's Introduction

Github Action for mapping variables by a specific key

build-test

Variable-Mapper action maps variables by regular expressions.

  • The map argument is a configuration in json format.
    • The top-level key in JSON is a regular expression condition. They are evaluated in order from the top.
    • The value is the key-value pair of variables to be exported.
  • The key argument is the key to match the map.

Sample Workflows

Export variables corresponding to regular expression-matched keys

on: [push]
name: Export variables corresponding to regular expression-matched keys
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: kanga333/variable-mapper@master
      with:
        key: "${{github.base_ref}}"
        map: |
          {
            "master": {
              "environment": "production",
              "AWS_ACCESS_KEY_ID": "${{ secrets.PROD_AWS_ACCESS_KEY_ID }}",
              "AWS_SECRET_ACCESS_KEY": "${{ secrets.PROD_AWS_ACCESS_KEY_ID }}"
            },
            "staging": {
              "environment": "staging",
              "AWS_ACCESS_KEY_ID": "${{ secrets.STG_AWS_ACCESS_KEY_ID }}",
              "AWS_SECRET_ACCESS_KEY": "${{ secrets.STG_AWS_ACCESS_KEY_ID }}"
            },
            ".*": {
              "environment": "development",
              "AWS_ACCESS_KEY_ID": "${{ secrets.DEV_AWS_ACCESS_KEY_ID }}",
              "AWS_SECRET_ACCESS_KEY": "${{ secrets.DEV_AWS_ACCESS_KEY_ID }}"
            }
          }
    - name: Echo environment
      run: echo ${{ env.environment }}

The key is evaluated from the top and exports the first matched variables.

Export variables to output and environment and log

on: [push]
name: Export variables to output and environment and log
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: kanga333/variable-mapper@master
      id: export
      with:
        key: "${{github.base_ref}}"
        map: |
          {
            "master": {
              "environment": "production"
            },
            ".*": {
              "environment": "development"
            }
          }
        export_to: env,log,output
    - name: Echo environment and output
      run: |
        echo ${{ env.environment }}
        echo ${{ steps.export.outputs.environment }}

The variables can be exported to log, env and output. (Default is log,env)

Switching the behavior of getting the variable

The mode option can be used to change the behavior of getting variables. first_match, overwrite and fill are valid values.

first_match mode (default)

first_match evaluates the regular expression of a key in order from the top and gets the variable for the first key to be matched.

on: [push]
name: Exporting variables in the first match
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: kanga333/variable-mapper@master
      id: export
      with:
        key: "first"
        map: |
          {
            "first": {
              "env1": "value1",
              "env2": "value2"
            },
            ".*": {
              "env1": "value1_overwrite",
              "env3": "value3"
            }
          }
        export_to: env
        mode: first_match
    - name: Echo environment and output
      run: |
        echo ${{ env.env1 }}
        echo ${{ env.env2 }}
        echo ${{ env.env3 }}

In this workflow, only env1:value1 and env2:value2 are exported as env.

overwrite mode

overwrite evaluates the regular expression of the keys in order from the top, and then merges the variables associated with the matched keys in turn. If the same variable is defined, the later evaluated value is overwritten.

on: [push]
name: Exporting variables by overwriting
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: kanga333/variable-mapper@master
      id: export
      with:
        key: "first"
        map: |
          {
            "first": {
              "env1": "value1",
              "env2": "value2"
            },
            ".*": {
              "env1": "value1_overwrite",
              "env3": "value3"
            }
          }
        export_to: env
        mode: overwrite
    - name: Echo environment and output
      run: |
        echo ${{ env.env1 }}
        echo ${{ env.env2 }}
        echo ${{ env.env3 }}

In this workflow, env1:value1_overwrite, env2:value2 and env3:value3 export as env.

fill mode

fill evaluates the regular expression of the keys in order from the top, and then merges the variables associated with the matched keys in turn. If the same variable is defined, later evaluated values are ignored and the first evaluated value takes precedence.

on: [push]
name: Export parameters in filling
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: kanga333/variable-mapper@master
      id: export
      with:
        key: "first"
        map: |
          {
            "first": {
              "env1": "value1",
              "env2": "value2"
            },
            ".*": {
              "env1": "value1_overwrite",
              "env3": "value3"
            }
          }
        export_to: env
        mode: fill
    - name: Echo environment and output
      run: |
        echo ${{ env.env1 }}
        echo ${{ env.env2 }}
        echo ${{ env.env3 }}

In this workflow, env1:value1, env2:value2 and env3:value3 export as env.

variable-mapper's People

Contributors

dependabot[bot] avatar kanga333 avatar mrmeyers99 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

variable-mapper's Issues

Incorrect Examples

Hi there, love the GitHub action but there seems to be some mistakes with the examples in the readme.

When I tried uses: kanga333/variable-mapper@v1 I got the error:
An action could not be found at the URI 'https://api.github.com/repos/kanga333/variable-mapper/tarball/v1'.
The fix for this is to change it to replace it with uses: kanga333/[email protected].

The second problem is with secrets the readme has "AWS_ACCESS_KEY_ID": ${{ secrets.PROD_AWS_ACCESS_KEY_ID }},.
This fails for me with Unexpected token A in JSON at position 77 quoting the secret fixes this.

GitHub Action's deprecation warnings regarding Node.js 12

Possible you're already aware of this, but just letting you know that running this action now produces the following warning:

Node.js 12 actions are deprecated. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/. Please update the following actions to use Node.js 16: kanga333/variable-mapper

GitHub Action's deprecation warnings : set-output

Hello, thank you for this Github action is really useful.

I would like to notice that since this github action update : GitHub Actions: Deprecating save-state and set-output commands , the action seems to generate this warning :

Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

image

Have a good day

Support arrays

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.

Get multi private ssh keys from github secrets in to kanga333/variable-mapper map

Hi i have an error while trying get ssh priv key from githubsecrets, depending from branch dev prod.

My script in github
`
jobs:

build:
runs-on: ubuntu-latest
steps:

- name: Get branch name
  uses: nelonoel/[email protected]

- name: Set branch-specific configurations
  uses: kanga333/[email protected]
  with:
    key: ${{ env.BRANCH_NAME }}
    map: |
      {
        "dev": {
          "spell_hostname": "dev.test.com",
          "spell_user": "ubuntu",
          "aws_region": "eu-central-1",
          "aws_access_key": "${{ secrets.DEV_AWS_SECGROUP_ACCESS }}",
          "aws_secret_key": "${{ secrets.DEV_AWS_SECGROUP_SECRET }}",
          "ssh_key": ${{ secrets.ALEKSP_SSH_DEV_SERVER_KEY }},
          "aws_sg_id": "sg-03b21b4a686fdd481",
          "build": "linux",
          "key_path": "~/.ssh/",
          "key_name": "id_rsa-dev",
          "service_name": "dev"
        },
        "prod": {
          "spell_hostname": "prod.test.com",
          "spell_user": "ubuntu",
          "aws_region": "eu-central-1",
          "aws_access_key": "${{ secrets.PROD_AWS_SECGROUP_ACCESS }}",
          "aws_secret_key": "${{ secrets.PROD_AWS_SECGROUP_SECRET }}",
          "ssh_key": ${{ secrets.ALEKSP_SSH_PROD_SERVER_KEY }},
          "aws_sg_id": "sg-03b21b4a686fdd481",
          "build": "linux",
          "service_name": "prod"
          
        }
      }`

and i get error:
Error: Unexpected token
in JSON at position 272

Full git hub log:
` 0s
Run kanga333/[email protected]
with:
key: dev
map: {
"dev": {
"spell_hostname": "dev.test.com",
"spell_user": "ubuntu",
"aws_region": "eu-central-1",
"aws_access_key": "",
"aws_secret_key": "
",
"ssh_key": "",
"aws_sg_id": "sg-03b21b4a686fdd481",
"build": "linux",
"key_path": "~/.ssh/",
"key_name": "id_rsa-dev",
"service_name": "spell_dev"
},
"prod": {
"spell_hostname": "prod.test.com",
"spell_user": "ubuntu",
"aws_region": "eu-central-1",
"aws_access_key": "",
"aws_secret_key": "",
"ssh_key": "
",
"aws_sg_id": "sg-03b21b4a686fdd481",
"build": "linux",
"service_name": "prod"

}

}

export_to: log,env
mode: first_match

env:
BRANCH_NAME: dev
Error: Unexpected token
in JSON at position 272`

Without this ssh_key in map :
"ssh_key": ${{ secrets.ALEKSP_SSH_DEV_SERVER_KEY }},
and this in map:
"ssh_key": ${{ secrets.ALEKSP_SSH_PROD_SERVER_KEY }},

script works fine.

Error: Unexpected token *** in JSON

Hi everyone,
In my build-and-deploy.yml workflow, I have the following:

on:
  workflow_dispatch:

    inputs:

      environment:
        required: true
        default: 'test'
        type: choice
        description: Which environment ?
        options:
          - test
          - staging
          - prod
      
      build_assets:
        required: false
        type: boolean
        description: Build assets ?

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
    
    - name: Project Checkout
      uses: actions/checkout@v3
    
    - name: Node version Setup
      uses: actions/setup-node@v3
      with:
        node-version: '18.17.1'
        cache: 'npm'

    - name: Map environment variables
      uses: kanga333/variable-mapper@master
      with:
        key: "${{inputs.environment}}"
        map: |
          {
            "prod": {
              "environment": "production",
              "AWS_SECRET_ACCESS_KEY": "${{ secrets.SSH_PRIVATE_KEY }}"
            },
            "staging": {
              "environment": "staging",
              "AWS_SECRET_ACCESS_KEY": "${{ secrets.SSH_PRIVATE_KEY_STAGING }}"
            },
            ".*": {
              "environment": "test",
              "AWS_SECRET_ACCESS_KEY": "${{ secrets.SSH_PRIVATE_KEY_TEST }}"
            }
          }

    - name: Echo environment
      run: echo ${{ env.environment }}

My worflow fails with following error:

Run kanga333/variable-mapper@master
  with:
    key: test
    map: {
    "prod": {
      "environment": "production",
      "AWS_SECRET_ACCESS_KEY": "***"
    },
    "staging": {
      "environment": "staging",
      "AWS_SECRET_ACCESS_KEY": "***"
    },
    ".*": {
      "environment": "test",
      "AWS_SECRET_ACCESS_KEY": "***"
    }
  }
  
    export_to: log,env
    mode: first_match
Error: Unexpected token 
 in JSON at position 108

I've already checked that my map JSON is correct, I don't get what I'm missing here
Can somebody help me ?

Error: Unexpected token in JSON at position 111 | Multiline Secret is failing

Hi, We are using this mapper for our actions. This is how it looks like:

jobs:
  dispatch-deployment:
    name: Connecting to GCP Cluster
    runs-on: ubuntu-latest
    steps:
      - uses: kanga333/variable-mapper@master
        with:
          key: "${{ github.event.inputs.deployment_destination }}"
          map: |
            {
              "testing": {
                "environment": "testing",
                "project": "testing",
                "service_account_key": "${{ secrets.GCP_SA_KEY }}",
                "cluster_name": "testing-cluster-v3",
                "location": "europe-west1-b",
                "cluster_short_name": "tst-v3"
              }
            }

Now the error we are getting is:

    export_to: log,env
    mode: first_match
Error: Unexpected token 
 in JSON at position 111

And on further investigation, we found that position 111 is the "${{ secrets.GCP_SA_KEY }}" part.

So initially we tested it with a single line secret (like in the docs i.e. AWS secret or ID) and it was working fine but here in our scenario is a GCP service account key which is multiline.

Hide export logs

Hi. I'm using your mapper to map variables and I use ssh private keys. But when the ssh key is mapped, it's printed in logs of GitHub action. Is there any opportunity to switch to "silent" mode or something like that?

Don't stop on first match

Hi ๐Ÿ‘‹

I have a use-case where I'd like to configure everything in a single step by not stopping on first match. I.e, given:

{
  "*@production": {
    "DEPLOY_CONFIG_BRANCH": "master",
    "SSH_SECRET_KEY": "SSH_DEPLOY_KEY_PRODUCTION"
  },
  "api@production": {
    "DEPLOY_ENVIRONMENT_URL": "https://api.example.com"
  },
  "admin@production": {
    "DEPLOY_ENVIRONMENT_URL": "https://admin.example.com"
  },
  "*@staging": {
    "DEPLOY_CONFIG_BRANCH": "staging",
    "SSH_SECRET_KEY": "SSH_DEPLOY_KEY_STAGING"
  },
  "api@staging": {
    "DEPLOY_ENVIRONMENT_URL": "https://api.staging.example.com"
  },
  "admin@staging": {
    "DEPLOY_ENVIRONMENT_URL": "https://admin.staging.example.com"
  }
}

if the key is api@staging, I'd like it to match both *@staging and api@staging, so the three DEPLOY_CONFIG_BRANCH, SSH_SECRET_KEY, DEPLOY_ENVIRONMENT_URL would be set.

Order might matter in this case, as an env var declared in a first matching key might be overridden by the next one. We should just choose either it overrides or ignores an already set env var in such cases and document the behavior.

Error: Unexpected token *** in JSON

In my deploy-preview.yml workflow I have the following.

      - uses: kanga333/variable-mapper@master
        with:
          key: "${{github.base_ref}}"
          map: |
            {
              "master": {
                "VITE_ENVIRONMENT": "production",
              },
              "develop": {
                "VITE_ENVIRONMENT": "staging",
              },
              ".*": {
                "VITE_ENVIRONMENT": "development",
              }
            }

It recently started failing with the following error (extract from the logs):

2022-11-01T10:40:32.2086812Z ##[group]Run kanga333/variable-mapper@master
2022-11-01T10:40:32.2087407Z with:
2022-11-01T10:40:32.2087777Z   key: develop
2022-11-01T10:40:32.2088981Z   map: ***
  "master": ***
    "VITE_ENVIRONMENT": "production",
  ***,
  "develop": ***
    "VITE_ENVIRONMENT": "staging",
  ***,
  ".*": ***
    "VITE_ENVIRONMENT": "development",
  ***
***

2022-11-01T10:40:32.2090045Z   export_to: log,env
2022-11-01T10:40:32.2090430Z   mode: first_match
2022-11-01T10:40:32.2090790Z env:
2022-11-01T10:40:32.2091175Z   BRANCH: feature/new-graph-segment
2022-11-01T10:40:32.2091595Z ##[endgroup]
2022-11-01T10:40:32.2941053Z ##[error]Unexpected token *** in JSON at position 102

The workflow was previously working with this exact same setup so I'm not sure if the new version introduced some bug or I'm at fault somehow here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.