Created
August 21, 2026 06:12
-
-
Save mr-anxo/a682f7ba7f42718603b3972a7ca4fef5 to your computer and use it in GitHub Desktop.
ArgoCD role members script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Vérification des arguments | |
| if [ "$#" -ne 5 ]; then | |
| echo "Usage: $0 <GITLAB_URL> <GITLAB_TOKEN> <GROUP_NAME> <PROJECT_NAME> <ARGO_PROJECT_NAME>" | |
| exit 1 | |
| fi | |
| GITLAB_URL="$1" | |
| GITLAB_TOKEN="$2" | |
| GROUP_NAME="$3" | |
| PROJECT_NAME="$4" | |
| ARGO_PROJECT_NAME="$5" | |
| # Fonction pour faire des requêtes à l'API GitLab | |
| gitlab_api_call() { | |
| curl --silent --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLAB_URL/api/v4/$1" | |
| } | |
| # Fonction pour remplacer le domaine de l'email | |
| replace_email_domain() { | |
| echo "$1" | sed 's/@smile\.ci/@synelia.tech/' | |
| } | |
| echo "$GROUP_NAME" | awk -F'/' '{ for (i=1; i<=NF; i++) print $i }' | while IFS= read -r GROUP_NAME_X; do | |
| # Récupérer l'ID du groupe | |
| GROUP_ID=$(gitlab_api_call "groups?search=$GROUP_NAME_X" | jq '.[] | select(.name == "'$GROUP_NAME_X'")' | jq -r '.id') | |
| if [ -z "$GROUP_ID" ] || [ "$GROUP_ID" = "null" ]; then | |
| #echo "Groupe non trouvé." | |
| #echo "Recherche de l'ID du projet..." | |
| echo "Deep search..." | |
| PROJECT_ID=$(gitlab_api_call "projects?search=$PROJECT_NAME" | jq -r '.[0].id') | |
| if [ -z "$PROJECT_ID" ] || [ "$PROJECT_ID" = "null" ]; then | |
| echo "Projet non trouvé." | |
| exit 1 | |
| fi | |
| fi | |
| # Récupérer les membres du groupe | |
| MEMBERS=$(gitlab_api_call "groups/$GROUP_ID/members") | |
| # Un peu de gagie ooooh | |
| if echo "$MEMBERS" | grep -q '{"message":"404 Group Not Found"}'; then | |
| MEMBERS=null | |
| fi | |
| if echo "$MEMBERS" | jq -e '. | length > 0' > /dev/null 2>&1; then | |
| echo | |
| else | |
| MEMBERS=$(gitlab_api_call "projects/$PROJECT_ID/members") | |
| fi | |
| # Traiter chaque membre | |
| echo "$MEMBERS" | jq -r '.[].username' | while read -r username; do | |
| user_info=$(gitlab_api_call "users?username=$username") | |
| email=$(echo "$user_info" | jq -r '.[0].email') | |
| modified_email=$(replace_email_domain "$email") | |
| # Exécuter la commande ArgoCD | |
| argocd proj role add-group "$ARGO_PROJECT_NAME" owner "$modified_email" | |
| #echo "Ajout de $modified_email au projet ArgoCD $ARGO_PROJECT_NAME" | |
| done | |
| done | |
| echo "Opération terminée." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment