Skip to content

Instantly share code, notes, and snippets.

@aldoyh
Last active September 9, 2024 15:35
Show Gist options
  • Save aldoyh/aaa82ac889dd4a6c93c869bf0eff3f68 to your computer and use it in GitHub Desktop.
Save aldoyh/aaa82ac889dd4a6c93c869bf0eff3f68 to your computer and use it in GitHub Desktop.
This script reads a .env file and sets the environment variables as GitHub secrets using the `gh` Command
#!/bin/bash
# This script reads a .env file and sets the environment variables as GitHub secrets using the gh CLI.
# totally AI generated using local Ollama. And Totally proud of it!
# AI Generated by: Hasan AlDoy @aldoyh @ollama_ai 2024
# No Copyrights Nor Lefts Reserved.
# This script is licensed under the MIT License.
# You are free to modify and distribute this script as long as the original author is credited.
# This script is provided AS IS without any warranty of any kind.
# Save the Pengiun, Save the World! 🐧
# Check if the gh CLI is installed.
if ! command -v gh &> /dev/null; then
echo "gh CLI is required to run this script."
echo "Please install it from https://cli.github.com/"
exit 1
fi
#!/bin/bash
# Function to count the number of variables in a .env file
count_variables() {
grep -v '^#' "$1" | grep -v '^$' | wc -l
}
# Function to select an environment file from a dropdown menu
select_env_file() {
PS3="Please select an environment file: "
env_files=(.env*)
select file in "${env_files[@]}"; do
if [ -n "$file" ]; then
echo "$file"
return
else
echo "Invalid selection. Please try again."
fi
done
}
# function that lists all secret variables using the gh cli command
list_secret_variables() {
gh secret list | awk '{print $1}'
}
# set a var to the list of secret variables
secret_variables=$(list_secret_variables)
# display the total number of secret variables
echo "There are $(echo "$secret_variables" | wc -l) secret variables in GitHub."
# Check if an argument is provided
if [ -z "$1" ]; then
# No argument provided, display dropdown menu
env_file=$(select_env_file)
else
# Use the provided argument as the .env file
env_file="$1"
fi
# Check if the file exists
if [ ! -f "$env_file" ]; then
echo "File $env_file does not exist."
exit 1
fi
# Count the number of variables in the selected .env file
variable_count=$(count_variables "$env_file")
echo "The selected file $env_file contains $variable_count variables."
# Read the .env file and set each variable using the gh CLI
while IFS= read -r line; do
# Skip empty lines and comments
if [[ -z "$line" || "$line" == \#* ]]; then
continue
fi
# checks if it's already in the secret variables
if echo "$secret_variables" | grep -q "$line"; then
echo "The secret variable $line already exists."
# then update that secret variable
gh secret set "$line" -b "$value"
continue
fi
# Extract the key and value
IFS='=' read -r key value <<< "$line"
# Remove any surrounding quotes from the value
value=$(echo "$value" | sed -e 's/^"//' -e 's/"$//')
# Set the environment variable using gh CLI
gh secret set "$key" -b"$value"
done < "$env_file"
echo "Environment variables from $env_file have been set using gh CLI."
# Check if an argument is provided
if [ -z "$1" ]; then
# No argument provided, display dropdown menu
env_file=$(select_env_file)
else
# Use the provided argument as the .env file
env_file="$1"
fi
# Check if the file exists
if [ ! -f "./$env_file" ]; then
echo "File $env_file does not exist."
exit 1
fi
# Count the number of variables in the selected .env file
variable_count=$(count_variables "./$env_file")
echo "The selected file $env_file contains $variable_count variables."
# Read the .env file and set each variable using the gh CLI
while IFS= read -r line; do
# Skip empty lines and comments
if [[ -z "$line" || "$line" == \#* ]]; then
continue
fi
# Extract the key and value
IFS='=' read -r key value <<< "$line"
# Remove any surrounding quotes from the value
value=$(echo "$value" | sed -e 's/^"//' -e 's/"$//')
# Set the environment variable using gh CLI
gh secret set "$key" -b "$value"
done < "$env_file"
echo "Environment variables from $env_file have been set using gh CLI."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment