Skip to content

Instantly share code, notes, and snippets.

@lewisgibson
Last active August 27, 2023 12:38
Show Gist options
  • Save lewisgibson/af56ab1307184990eb3a02491ffa8755 to your computer and use it in GitHub Desktop.
Save lewisgibson/af56ab1307184990eb3a02491ffa8755 to your computer and use it in GitHub Desktop.
πŸ”’ Automate GitHub Organization Repository Secrets Deployment πŸ€–
#!/bin/bash
# ----------------------------------------------------------------------
# This script sets a secret for all repositories in a GitHub organization
# using the GitHub CLI (gh).
#
# Usage:
# 1. Make sure you have the GitHub CLI (gh) installed. If not, install it from: https://cli.github.com/
# 2. Add this function to your shell configuration file (e.g., .profile, .bashrc, .zshrc).
# 3. Use the function as follows:
# set_org_secrets <ORG_NAME> <SECRET_NAME> <SECRET_VALUE>
#
# Arguments:
# - ORG_NAME: The name of the organization on GitHub.
# - SECRET_NAME: The name of the secret you want to set.
# - SECRET_VALUE: The value of the secret you want to set.
#
# Note:
# - Be cautious while using scripts that modify repositories.
# - Test in a controlled environment before applying to production.
#
# Author: Lewis Gibson
# ----------------------------------------------------------------------
set_org_secrets() {
if [ $# -ne 3 ]; then
echo "Usage: set_org_secrets <ORG_NAME> <SECRET_NAME> <SECRET_VALUE>"
return 1
fi
ORG_NAME="$1"
SECRET_NAME="$2"
SECRET_VALUE="$3"
if [ -z "$ORG_NAME" ] || [ -z "$SECRET_NAME" ] || [ -z "$SECRET_VALUE" ]; then
echo "Error: All arguments must be non-empty"
return 1
fi
if ! command -v gh >/dev/null 2>&1; then
echo "Error: GitHub CLI (gh) is not installed. Please install it: https://cli.github.com/"
return 1
fi
if ! gh auth status | grep -q "Logged in to github.com"; then
echo "You're not logged in to GitHub. Please run 'gh auth login' first."
return 1
fi
REPO_LIST=$(gh repo list $ORG_NAME --source --json=name --jq '.[].name')
TOTAL_REPOS=$(echo "$REPO_LIST" | wc -l)
COUNTER=0
SUCCESS_MARK="βœ“"
FAILURE_MARK="❌"
if [ -t 1 ]; then
SUCCESS_MARK='\033[0;32mβœ“\033[0m'
FAILURE_MARK='\033[0;31m❌\033[0m'
fi
set_secret_in_repo() {
local REPO="$1"
local COUNTER="$2"
if $(gh secret set -R "$ORG_NAME/$REPO" "$SECRET_NAME" <<<"$SECRET_VALUE" 2>&1); then
echo "$SUCCESS_MARK Successfully set secret in $ORG_NAME/$REPO [$COUNTER/$TOTAL_REPOS]"
else
echo "$FAILURE_MARK Failed setting secret in $ORG_NAME/$REPO [$COUNTER/$TOTAL_REPOS]"
fi
}
MAX_PARALLEL=4
CURRENT_PARALLEL=0
while IFS= read -r REPO; do
COUNTER=$((COUNTER + 1))
set_secret_in_repo "$REPO" "$COUNTER" &
CURRENT_PARALLEL=$((CURRENT_PARALLEL + 1))
if [ "$CURRENT_PARALLEL" -ge "$MAX_PARALLEL" ]; then
while [ $(jobs | wc -l) -ge "$MAX_PARALLEL" ]; do
sleep 0.1
done
CURRENT_PARALLEL=$((CURRENT_PARALLEL - 1))
fi
done <<<"$REPO_LIST"
wait
echo "Secrets set for $TOTAL_REPOS repositories."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment