Skip to content

Commit 271c355

Browse files
authored
Add support for variable env vars (#84)
* Adding option to include GHVars * Adding validation for repo file, fixing readme * Update README.md * Update README.md * Adjusted for snippet consistency
1 parent 00c5f7c commit 271c355

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ You'll need [Access Keys](https://docs.aws.amazon.com/powershell/latest/userguid
2424

2525
## Environment variables
2626

27-
For envirnoment variables in your app, you can provide a `repo_env` file in your repo, a `.env` file in GitHub Secrets named `DOT_ENV`, or an AWS Secret. Then hook it up in your `docker-compose.yaml` file like:
27+
For envirnoment variables in your app, you can provide:
28+
- `repo_env` - A file in your repo that contains env vars
29+
- `ghv_env` - An entry in [Github actions variables](https://docs.github.com/en/actions/learn-github-actions/variables)
30+
- `dot_env` - An entry in [Github secrets](https://docs.github.com/es/actions/security-guides/encrypted-secrets)
31+
- `aws_secret_env` - The path to a JSON format secret in AWS
32+
33+
Then hook it up in your `docker-compose.yaml` file like:
2834

2935
```
3036
version: '3.9'
@@ -93,6 +99,7 @@ jobs:
9399
sub_domain: app
94100
tf_state_bucket: my-terraform-state-bucket
95101
dot_env: ${{ secrets.DOT_ENV }}
102+
ghv_env: ${{ vars.VARS }}
96103
app_port: 3000
97104
additional_tags: "{\"key1\": \"value1\",\"key2\": \"value2\"}"
98105

@@ -121,9 +128,10 @@ The following inputs can be used as `step.with` keys
121128
| `no_cert` | Boolean | Set this to true if no certificate is present for the domain. **See note **. Defaults to `false` |
122129
| `tf_state_bucket` | String | AWS S3 bucket to use for Terraform state. |
123130
| `tf_state_bucket_destroy` | Boolean | Force purge and deletion of S3 bucket defined. Any file contained there will be destroyed. (Default is `false`). `stack_destroy` must also be `true`|
124-
| `repo_env` | String | `.env` file containing environment variables to be used with the app. Name defaults to `repo_env`. Check **SEnvironment variables** note |
125-
| `dot_env` | String | `.env` file to be used with the app. This is the name of the [Github secret](https://docs.github.com/es/actions/security-guides/encrypted-secrets). Check **SEnvironment variables** note |
126-
| `aws_secret_env` | String | Secret name to pull environment variables from AWS Secret Manager. Check **SEnvironment variables** note |
131+
| `repo_env` | String | `.env` file containing environment variables to be used with the app. Name defaults to `repo_env`. Check **Environment variables** note |
132+
| `dot_env` | String | `.env` file to be used with the app. This is the name of the [Github secret](https://docs.github.com/es/actions/security-guides/encrypted-secrets). Check **Environment variables** note |
133+
| `ghv_env` | String | `.env` file to be used with the app. This is the name of the [Github variables](https://docs.github.com/en/actions/learn-github-actions/variables). Check **Environment variables** note |
134+
| `aws_secret_env` | String | Secret name to pull environment variables from AWS Secret Manager. Check **Environment variables** note |
127135
| `app_port` | String | port to expose for the app |
128136
| `lb_port` | String | Load balancer listening port. Defaults to 80 if NO FQDN provided, 443 if FQDN provided |
129137
| `lb_healthcheck` | String | Load balancer health check string. Defaults to HTTP:app_port |

action.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ inputs:
3737
required: false
3838
default: 'repo_env'
3939
dot_env:
40-
description: '`.env` file to be used with the app'
40+
description: '`.env` file to be used with the app from Github secrets'
41+
required: false
42+
ghv_env:
43+
description: '`.env` file to be used with the app from Github variables'
4144
required: false
4245
aws_secret_env:
4346
description: 'Secret name to pull env variables from AWS Secret Manager'
@@ -115,7 +118,8 @@ runs:
115118
TF_STATE_BUCKET: ${{ inputs.tf_state_bucket }}
116119
TF_STATE_BUCKET_DESTROY: ${{ inputs.tf_state_bucket_destroy }}
117120
REPO_ENV: ${{ inputs.repo_env }}
118-
DOT_ENV: ${{ inputs.dot_env }}
121+
GHS_ENV: ${{ inputs.dot_env }}
122+
GHV_ENV: ${{ inputs.ghv_env }}
119123
AWS_SECRET_ENV: ${{ inputs.aws_secret_env }}
120124
APP_PORT: ${{ inputs.app_port }}
121125
LB_PORT: ${{ inputs.lb_port }}

operations/_scripts/generate/generate_app_repo.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ fi
1717

1818
cp -rf "$TARGET_PATH"/* "${GITHUB_ACTION_PATH}/operations/deployment/ansible/app/${GITHUB_REPO_NAME}/"
1919

20-
echo "Copying checked in env file from repo to Ansible deployment path"
21-
22-
cp "$TARGET_PATH/$REPO_ENV" "${GITHUB_ACTION_PATH}/operations/deployment/ansible/repo.env"
20+
if [ -s "$TARGET_PATH/$REPO_ENV" ]; then
21+
echo "Copying checked in env file from repo to Ansible deployment path"
22+
cp "$TARGET_PATH/$REPO_ENV" "${GITHUB_ACTION_PATH}/operations/deployment/ansible/repo.env"
23+
else
24+
echo "Checked in env file from repo is empty or couldn't be found"
25+
fi

operations/_scripts/generate/generate_dot_env.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ set -e
55

66
echo "In generate_dot_env.sh"
77

8-
echo "$DOT_ENV" >> "${GITHUB_ACTION_PATH}/operations/deployment/ansible/ghs.env"
8+
echo "$GHV_ENV" > "${GITHUB_ACTION_PATH}/operations/deployment/ansible/ghv.env"
9+
echo "$GHS_ENV" > "${GITHUB_ACTION_PATH}/operations/deployment/ansible/ghs.env"

operations/deployment/ansible/bitops.before-deploy.d/merge-tf-env.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set -e
66
echo "BitOps Ansible before script: Merge Terraform Enviornment Variables..."
77

88
# Merging order
9-
order=tf,repo,ghs,aws
9+
order=tf,repo,ghv,ghs,aws
1010

1111
# Ansible dotenv file -> The final destination of all
1212
DOTENV_FILE="${BITOPS_ENVROOT}/ansible/app.env"
@@ -17,7 +17,10 @@ TF_DOTENV_FILE="${BITOPS_ENVROOT}/terraform/tf.env"
1717
# Repo env file
1818
REPO_ENV_FILE="${BITOPS_ENVROOT}/ansible/repo.env"
1919

20-
# GH Secrets env file
20+
# GH Variables env file
21+
GHV_ENV_FILE="${BITOPS_ENVROOT}/ansible/ghv.env"
22+
23+
# GH Secrets env file
2124
GHS_ENV_FILE="${BITOPS_ENVROOT}/ansible/ghs.env"
2225

2326
# TF AWS dotenv file
@@ -52,12 +55,16 @@ function process {
5255
# Code to be executed for option2
5356
merge $REPO_ENV_FILE "checked-in"
5457
;;
55-
ghs)
58+
ghv)
5659
# Code to be executed for option3
60+
merge $GHV_ENV_FILE "GH-Vars"
61+
;;
62+
ghs)
63+
# Code to be executed for option4
5764
merge $GHS_ENV_FILE "GH-Secret"
5865
;;
5966
tf)
60-
# Code to be executed for option3
67+
# Code to be executed for option5
6168
merge $TF_DOTENV_FILE "Terraform"
6269
;;
6370
*)

0 commit comments

Comments
 (0)