Skip to content

Instantly share code, notes, and snippets.

@celeroncoder
Last active May 19, 2025 07:38
Show Gist options
  • Save celeroncoder/8643e8d896cc5876f92fa6b7e58b1ad1 to your computer and use it in GitHub Desktop.
Save celeroncoder/8643e8d896cc5876f92fa6b7e58b1ad1 to your computer and use it in GitHub Desktop.
setting env secrets to github repos
#!/bin/bash
# Default values
ORIGIN="origin"
ENV_FILE=""
GH_ENV=""
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--origin) ORIGIN="$2"; shift ;;
*) echo "Unknown parameter: $1"; exit 1 ;;
esac
shift
done
# Get repo name from remote
REPO_URL=$(git remote get-url "$ORIGIN" 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Error: Remote '$ORIGIN' not found. Please specify a valid remote with --origin or check your git repository."
exit 1
fi
# Extract owner/repo from the URL
if [[ "$REPO_URL" =~ github\.com[:/](.+)\.git$ ]]; then
REPO="${BASH_REMATCH[1]}"
elif [[ "$REPO_URL" =~ github\.com[:/](.+)$ ]]; then
REPO="${BASH_REMATCH[1]}"
else
echo "Error: Could not parse GitHub repository from remote URL: $REPO_URL"
echo "Please ensure you're in a GitHub repository with a valid remote."
exit 1
fi
echo "Using repository: $REPO"
# Ask for environment file
echo "Please enter the environment file to use (.env, .env.staging, .env.production, etc.):"
read ENV_FILE
if [ ! -f "$ENV_FILE" ]; then
echo "Error: File '$ENV_FILE' does not exist."
exit 1
fi
# Ask for GitHub environment (optional)
echo "Please enter the GitHub environment to set secrets for (leave empty for repository-level secrets):"
read GH_ENV
ENV_FLAG=""
if [[ -n "$GH_ENV" ]]; then
ENV_FLAG="--env $GH_ENV"
echo "Setting secrets in environment: $GH_ENV"
else
echo "Setting repository-level secrets"
fi
echo "Using environment file: $ENV_FILE"
echo "Setting secrets from $ENV_FILE to $REPO..."
# Create an array to store all tasks
declare -a TASKS
# Read the entire file first, process each non-comment line
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip empty lines and comments
if [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]]; then
continue
fi
# Extract key and value
if [[ "$line" =~ ^([^=]+)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
# Add to the tasks array
TASKS+=("$key:$value")
fi
done < "$ENV_FILE"
# Set a reasonable number of parallel processes (adjust based on your system)
MAX_PARALLEL=5
RUNNING=0
# Process all tasks in parallel with controlled concurrency
for task in "${TASKS[@]}"; do
# Extract key and value
key="${task%%:*}"
value="${task#*:}"
# Start a background process
(
echo "Setting $key..."
if [[ -n "$ENV_FLAG" ]]; then
gh secret set "$key" --repo "$REPO" $ENV_FLAG --body "$value"
else
gh secret set "$key" --repo "$REPO" --body "$value"
fi
echo "✓ Set $key"
) &
# Control concurrency
(( RUNNING++ ))
if (( RUNNING >= MAX_PARALLEL )); then
wait -n
(( RUNNING-- ))
fi
done
# Wait for remaining background processes to finish
wait
echo "All secrets added successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment