Skip to content

Instantly share code, notes, and snippets.

@aronreisx
Created December 31, 2024 00:08
Show Gist options
  • Save aronreisx/8f1313cdaa3c7648d090ab84531f6aee to your computer and use it in GitHub Desktop.
Save aronreisx/8f1313cdaa3c7648d090ab84531f6aee to your computer and use it in GitHub Desktop.
Set GitHub Secrets based on ENV file
#!/bin/bash
# Ensure the gh CLI is installed and authenticated
if ! command -v gh &> /dev/null; then
echo "Error: GitHub CLI (gh) is not installed. Please install it: https://cli.github.com/"
exit 1
fi
if ! gh auth status &> /dev/null; then
echo "Error: GitHub CLI (gh) is not authenticated. Run 'gh auth login' to authenticate."
exit 1
fi
# Ensure the .env file is passed as an argument
if [ -z "$1" ]; then
echo "Usage: $0 /path/to/.env"
exit 1
fi
ENV_FILE="$1"
# Check if the file exists
if [ ! -f "$ENV_FILE" ]; then
echo "Error: File $ENV_FILE not found!"
exit 1
fi
# Ensure the GitHub repository is passed as an argument
if [ -z "$2" ]; then
echo "Usage: $0 /path/to/.env owner/repository"
exit 1
fi
REPO="$2"
# Read the .env file and set each secret
while IFS='=' read -r key value; do
# Ignore lines that are comments or empty
if [[ "$key" =~ ^#.*$ || -z "$key" ]]; then
continue
fi
# Trim whitespace
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
# Set the secret using gh CLI
echo "Setting secret: $key"
gh secret set "$key" --body "$value" --repo "$REPO"
done < "$ENV_FILE"
echo "All secrets from $ENV_FILE have been set for repository $REPO."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment