1

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}]
Ashar
  • 2,942
  • 10
  • 58
  • 122

2 Answers2

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"
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • 1
    I get this error `Error: Container action is only supported on Linux`. My runner is Windows. Can you provide a fix workaround. – Ashar May 09 '23 at 09:06
  • sorry @Ashar I was not aware of that limitation. Anyway you could try any action on the [marketplace](https://github.com/marketplace?category=&query=dot+env+sort%3Apopularity-desc&type=actions&verification=) that support `dotenv` – Matteo May 09 '23 at 09:45
0

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.

Please refer to this link of documentation for examples and more clarity: https://docs.github.com/en/actions/learn-github-actions/variables

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Ali Massoud
  • 135
  • 1
  • 10