<h2>General Notes</h2>

<p>I don't really understand this one:</p>

<blockquote>
  <p>However, this seems clunky, as I have to create the logic to read this
  file in  every script that is made.</p>
</blockquote>

<p>You can source all variables from a bash file in one line (source file-name.sh).  If they are "exported", you can pick them up in applications in any language you like (Java, python, or whatever), and most have built in syntax for doing it (like spring boot's SPEL syntax which lets you define your config to directly populate from environment variables automatically on start-up).</p>

<p>You can also make this export part of your ~/.bashrc file or whatever so it's auto sourced in your shell contexts, etc.</p>

<h2>Repositories + CI/CD</h2>

<p>This may depend on your repository and CI implementation more than the installed code.  For example, GitLab CI and GitHub Actions both support environment variables, as does Jenkins, etc.</p>

<p>In GitLab you can even go to your group level (above a bunch of related projects), and set per-environment environment variables there which are pushed into your deployment so you can use them in your installed code.  Then, you don't even need to store them in any extra files.</p>

<p>References:</p>

<ul>
<li><a href="https://docs.gitlab.com/ee/ci/variables/" rel="nofollow noreferrer">https://docs.gitlab.com/ee/ci/variables/</a></li>
<li><a href="https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables" rel="nofollow noreferrer">https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables</a></li>
</ul>

<h2>Docker</h2>

<p>Whatever your code is, you can probably bundle it into docker and pass the variables to the container at run time.</p>

CC BY-SA 4.0
2020-03-31T01:59:51.440Z
2020-03-31T20:11:48.183Z
2020-03-31T20:11:48.183Z
John Humphreys
<p>I am setting up a new infrastructure as code repository for a small to medium size project. The problem I am currently trying to solve is how to store my environment variables such that they can be read and used by all services and scripts within the repo. Some of the tools being used are Ansible, Powershell, Bash...</p>

<p>My current solution is a simple text file, manually created per environment. Each line has a variable that the services and scripts can look up. However, this seems clunky, as I have to create the logic to read this file in every script that is made.</p>

<p>So my question is, what approach should I be taking to provide all my services with environment variables? Am I on the right track, or should I be using something entirely different for this?</p>

<p>I'm not sure what I can use to provide this to all the different tools.
For details sake, I am not talking about secrets management. I mean variables such as: DNS address of the environment's DC, log server address, etc... 
There are significant constraints on addition of new software/tools due to the sensitivity of the environment. For example, I was considering using JSON format for the environment file, but Bash does not parse that natively. Meaning I would have to install a third party tool like 'jq' to use.</p>

<p>Any feedback welcome. Thank you!</p>

<git><infrastructure-as-code><environments><environment-variables><repository>
What approach should I take to store environment variables for a repo?
Savvly
# What approach should I take to store environment variables for a repo?

(Asked by: username_0 on 2020-03-30)

I am setting up a new infrastructure as code repository for a small to medium size project. The problem I am currently trying to solve is how to store my environment variables such that they can be read and used by all services and scripts within the repo. Some of the tools being used are Ansible, Powershell, Bash...

My current solution is a simple text file, manually created per environment. Each line has a variable that the services and scripts can look up. However, this seems clunky, as I have to create the logic to read this file in every script that is made.

So my question is, what approach should I be taking to provide all my services with environment variables? Am I on the right track, or should I be using something entirely different for this?

I'm not sure what I can use to provide this to all the different tools.
For details sake, I am not talking about secrets management. I mean variables such as: DNS address of the environment's DC, log server address, etc...
There are significant constraints on addition of new software/tools due to the sensitivity of the environment. For example, I was considering using JSON format for the environment file, but Bash does not parse that natively. Meaning I would have to install a third party tool like 'jq' to use.

Any feedback welcome. Thank you!

**Tags:** git, infrastructure-as-code, environments, environment-variables, repository


---

## 1 answers

### Answer by username_1 on 2020-03-31. Score: 4 | Accepted answer

## General Notes

I don't really understand this one:

> However, this seems clunky, as I have to create the logic to read this
> file in every script that is made.

You can source all variables from a bash file in one line (source file-name.sh). If they are "exported", you can pick them up in applications in any language you like (Java, python, or whatever), and most have built in syntax for doing it (like spring boot's SPEL syntax which lets you define your config to directly populate from environment variables automatically on start-up).

You can also make this export part of your ~/.bashrc file or whatever so it's auto sourced in your shell contexts, etc.

## Repositories + CI/CD

This may depend on your repository and CI implementation more than the installed code. For example, GitLab CI and GitHub Actions both support environment variables, as does Jenkins, etc.

In GitLab you can even go to your group level (above a bunch of related projects), and set per-environment environment variables there which are pushed into your deployment so you can use them in your installed code. Then, you don't even need to store them in any extra files.

References:

* <https://docs.gitlab.com/ee/ci/variables/>
* <https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables>

## Docker

Whatever your code is, you can probably bundle it into docker and pass the variables to the container at run time.


---

