Skip to content

Instantly share code, notes, and snippets.

@LdDl
Created March 26, 2025 11:48
Show Gist options
  • Save LdDl/8d2dbb73bbcd95c1de3903fcc92406d8 to your computer and use it in GitHub Desktop.
Save LdDl/8d2dbb73bbcd95c1de3903fcc92406d8 to your computer and use it in GitHub Desktop.
Bash script to scan Gitlab repositories to check if there are any branches, containers, packages or variables
#!/bin/bash
GITLAB_URL="https://gitlab.my.site.com"
GROUP_PATH="my-group" # Your group path
TOKEN="" # Token with read_api + read_registry + read_repository
PER_PAGE=100 # Max repos per page
OUTPUT_FILE="repo_analysis.csv"
echo "Generating repository analysis report..."
echo "Results will be saved to: $OUTPUT_FILE"
# Write CSV header
echo "repository_name;empty;num_registries;num_packages;num_variables" > "$OUTPUT_FILE"
# Fetch all projects in the group (including subgroups)
PAGE=1
while true; do
echo "Processing page $PAGE..."
# Get projects in the group
PROJECTS_JSON=$(curl -s -H "PRIVATE-TOKEN: $TOKEN" \
"$GITLAB_URL/api/v4/groups/$GROUP_PATH/projects?include_subgroups=true&per_page=$PER_PAGE&page=$PAGE")
# Break if no more projects
if [ $(echo "$PROJECTS_JSON" | jq '. | length') -eq 0 ]; then
break
fi
# Process each project
echo "$PROJECTS_JSON" | jq -c '.[]' | while read -r PROJECT; do
PROJECT_ID=$(echo "$PROJECT" | jq -r '.id')
PROJECT_PATH=$(echo "$PROJECT" | jq -r '.path_with_namespace')
REGISTRY_ENABLED=$(echo "$PROJECT" | jq -r '.container_registry_enabled')
# 1. Check if repository is empty (no branches = no commits)
BRANCHES_JSON=$(curl -s -H "PRIVATE-TOKEN: $TOKEN" \
"$GITLAB_URL/api/v4/projects/$PROJECT_ID/repository/branches")
EMPTY_STATUS="no"
if [ $(echo "$BRANCHES_JSON" | jq '. | length') -eq 0 ]; then
EMPTY_STATUS="yes"
fi
# 2. Count container registries
REGISTRY_COUNT=0
if [ "$REGISTRY_ENABLED" = "true" ]; then
REG_PAGE=1
while true; do
REGISTRY_JSON=$(curl -s -H "PRIVATE-TOKEN: $TOKEN" \
"$GITLAB_URL/api/v4/projects/$PROJECT_ID/registry/repositories?per_page=100&page=$REG_PAGE")
CURRENT_COUNT=$(echo "$REGISTRY_JSON" | jq '. | length')
[ "$CURRENT_COUNT" -eq 0 ] && break
REGISTRY_COUNT=$((REGISTRY_COUNT + CURRENT_COUNT))
REG_PAGE=$((REG_PAGE + 1))
done
fi
# 3. Count package registries
PACKAGE_COUNT=0
PKG_PAGE=1
while true; do
PACKAGES_JSON=$(curl -s -H "PRIVATE-TOKEN: $TOKEN" \
"$GITLAB_URL/api/v4/projects/$PROJECT_ID/packages?per_page=100&page=$PKG_PAGE")
CURRENT_COUNT=$(echo "$PACKAGES_JSON" | jq '. | length')
[ "$CURRENT_COUNT" -eq 0 ] && break
PACKAGE_COUNT=$((PACKAGE_COUNT + CURRENT_COUNT))
PKG_PAGE=$((PKG_PAGE + 1))
done
# 4. Count CI/CD variables
VARIABLE_COUNT=0
VAR_PAGE=1
while true; do
VARIABLES_JSON=$(curl -s -H "PRIVATE-TOKEN: $TOKEN" \
"$GITLAB_URL/api/v4/projects/$PROJECT_ID/variables?per_page=100&page=$VAR_PAGE")
CURRENT_COUNT=$(echo "$VARIABLES_JSON" | jq '. | length')
[ "$CURRENT_COUNT" -eq 0 ] && break
VARIABLE_COUNT=$((VARIABLE_COUNT + CURRENT_COUNT))
VAR_PAGE=$((VAR_PAGE + 1))
done
# Write to CSV
echo "$PROJECT_PATH;$EMPTY_STATUS;$REGISTRY_COUNT;$PACKAGE_COUNT;$VARIABLE_COUNT" >> "$OUTPUT_FILE"
done
PAGE=$((PAGE + 1))
done
echo "Report generated:"
column -t -s';' "$OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment