Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a mechanism to fail the CD when required environment variables are missing | issue#257 #274

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/register-commands-production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ on:
push:
branches: main
jobs:
Environment-Variables-Check:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v2
- run: bash environment-variables-validator.sh
env:
DISCORD_APPLICATION_ID: ${{secrets.DISCORD_APPLICATION_ID}}
DISCORD_GUILD_ID: ${{secrets.DISCORD_GUILD_ID}}
DISCORD_TOKEN: ${{secrets.DISCORD_TOKEN}}
DISCORD_PUBLIC_KEY: ${{secrets.DISCORD_PUBLIC_KEY}}
CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}}
CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}}
BOT_PRIVATE_KEY: ${{secrets.BOT_PRIVATE_KEY}}
RDS_SERVERLESS_PUBLIC_KEY: ${{secrets.RDS_SERVERLESS_PUBLIC_KEY}}
CRON_JOBS_PUBLIC_KEY: ${{secrets.CRON_JOBS_PUBLIC_KEY}}
IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}}
Register-Commands:
needs: [Environment-Variables-Check]
runs-on: ubuntu-latest
environment: production
steps:
Expand Down
20 changes: 19 additions & 1 deletion .github/workflows/register-commands-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ on:
push:
branches: develop
jobs:
Environment-Variables-Check:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v2
- run: bash environment-variables-validator.sh
env:
DISCORD_APPLICATION_ID: ${{secrets.DISCORD_APPLICATION_ID}}
DISCORD_GUILD_ID: ${{secrets.DISCORD_GUILD_ID}}
DISCORD_TOKEN: ${{secrets.DISCORD_TOKEN}}
DISCORD_PUBLIC_KEY: ${{secrets.DISCORD_PUBLIC_KEY}}
CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}}
CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}}
BOT_PRIVATE_KEY: ${{secrets.BOT_PRIVATE_KEY}}
RDS_SERVERLESS_PUBLIC_KEY: ${{secrets.RDS_SERVERLESS_PUBLIC_KEY}}
CRON_JOBS_PUBLIC_KEY: ${{secrets.CRON_JOBS_PUBLIC_KEY}}
IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}}
Register-Commands:
needs: [Environment-Variables-Check]
runs-on: ubuntu-latest
environment: staging
steps:
Expand Down Expand Up @@ -46,4 +64,4 @@ jobs:
DISCORD_GUILD_ID: ${{secrets.DISCORD_GUILD_ID}}
RDS_SERVERLESS_PUBLIC_KEY: ${{secrets.RDS_SERVERLESS_PUBLIC_KEY}}
CRON_JOBS_PUBLIC_KEY: ${{secrets.CRON_JOBS_PUBLIC_KEY}}
IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}}
IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}}
17 changes: 17 additions & 0 deletions environment-variables-validator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

ENV_VARS_FILE="environment-variables.txt"
# Check if the file exists
if [[ ! -f "$ENV_VARS_FILE" ]]; then
echo "File $ENV_VARS_FILE does not exist."
exit 1
fi

# Read the file and iterate through each variable name
while IFS= read -r var_name; do
# Check if the variable is set in the environment
if [[ -z "${!var_name}" ]]; then
echo "Environment variable $var_name is not set."
exit 1 # Exit with error if any variable is not set
fi
done < "$ENV_VARS_FILE"
10 changes: 10 additions & 0 deletions environment-variables.txt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need this file, .env file is not enough

Copy link
Contributor Author

@Saitharun279 Saitharun279 Oct 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially i tried by hardcoding the environment variables in the bash script along with "if" conditional blocks for each env var. To use while loop I stored them in a file, bash script iterates through each env var and checks if they are set.

.env file is used for local setup as per guidelines in contributing.MD file link.

So I used a different file.

We can add a .env file as well, but that will be confusing during local setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed that file , instead added one env var to store all the actual env var names

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DISCORD_APPLICATION_ID
DISCORD_GUILD_ID
DISCORD_TOKEN
DISCORD_PUBLIC_KEY
CLOUDFLARE_API_TOKEN
CLOUDFLARE_ACCOUNT_ID
BOT_PRIVATE_KEY
RDS_SERVERLESS_PUBLIC_KEY
CRON_JOBS_PUBLIC_KEY
IDENTITY_SERVICE_PUBLIC_KEY
Loading