skip to Main Content

i have a set of 20 variables for each of the 5 environments viz dev, qa, uat, prod, dr like below var_dev.yml:

hostname: "mydevhost"
port: "1885"
mount: "D:"
...
...

In ansible I could save the variable in variable files that have environment names like var_dev.yml, var_qa.yml etc and load one file based on user input choice like whichever environment they select.

- hosts: localhost
  vars_files:
    - "vars/var_{{ myenv }}.yml"
  tasks:
    - debug: var={{ port }}

ansible-playbook test.yml -e "myenv=dev"

How is it possible to achieve the same in Github Actions workflow?

Below approach I took does not look clean.

name: example-workflow
env:
  hostname_dev: "mydevhost"
  hostname_qa: "myqahost"
  port_dev: "1885"
  port_qa: "1881"
on: 
  push:
  workflow_dispatch:
    inputs:
      myenv:
        type: choice
        options:
          - dev
          - qa
          - perf
          - dr
          - prod

jobs:

  deploy:
    runs-on: ubuntu-latest
    steps:
      - run: echo env[env.hostname_${myen}]

2

Answers


  1. I would suggest you to use the action-dotenv-to-setenv GitHub Action, as example you could have one env files for each environment and use like:

      - name: Configure ${{ inputs.myenv }} environment
        uses: c-py/action-dotenv-to-setenv@v2
        with:
          env-file: ./env/env.${{ inputs.myenv }}
    

    with the following files

    ./env/env.dev

      hostname: "mydevhost"
      port: "1885"
    

    ./env/env.qa

      hostname: "myqahost"
      port: "1881"
    
    Login or Signup to reply.
  2. for GitHub Actions you have several levels for environment variables, you could have an organization, Repository, or environment level, this is the most secure one.
    The nice part about it is that it is handled securely on GitHub and you can call it using $REPOSITORY_VAR in the workflow.
    In your case, I would create a list of repository variables.
    https://docs.github.com/en/actions/learn-github-actions/variables "Please refer to this link of documentation for examples and more clarity."

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search