docker-promote/promote.sh
2025-03-10 15:52:41 +01:00

145 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
# Vérification du nombre d'arguments
if [ "$#" -lt 1 ]; then
echo "Usage: promote.sh <source_tag> [<source_tag> ...] [--dry-run] [--target-stage=<stage>] [--no-confirmation]"
echo
echo "Description:"
echo "Ce script permet de promouvoir des tags d'images Docker du projet MSE d'une étape à une autre (ex: 'k8sunstable' vers 'k8stesting')."
echo "Il prend un ou plusieurs tags d'images Docker comme argument et les promeut en fonction de leur étape actuelle."
echo
echo "Arguments :"
echo " <source_tag> : Tag source de l'image Docker à promouvoir. Format attendu :"
echo " <registry>/<repository>:<version>-<stage>.<build>.<commit>"
echo " Exemple : reg.cadoles.com/cnous-mse/portal:2025.2.6-XXX.1402.ed7732337"
echo
echo "Options :"
echo " --dry-run : Mode 'dry-run'. Simule la promotion sans exécuter de commandes Docker."
echo " Utile pour vérifier ce qui se passerait sans faire de changements."
echo " --target-stage=<stage> : Spécifie un stage cible particulier pour la promotion."
echo " Par défaut, la promotion suit la séquence d'étapes (ex: 'k8sunstable' -> 'k8stesting' -> ...)"
echo " Vous pouvez spécifier directement le stage (ex: --target-stage k8spreprod)."
echo " --no-confirmation : Ignore la demande de confirmation avant de pousser le tag Docker."
echo " Utilisé pour automatiser la promotion sans intervention manuelle."
echo
echo "Exemples d'utilisation :"
echo " promote.sh reg.cadoles.com/cnous-mse/portal:2025.2.6-XXX.1402.ed7732337"
echo " promote.sh reg.cadoles.com/cnous-mse/portal:2025.2.6-XXX.1402.ed7732337 --dry-run"
echo " promote.sh reg.cadoles.com/cnous-mse/portal:2025.2.6-XXX.1402.ed7732337 --target-stage=k8spreprod"
echo " promote.sh reg.cadoles.com/cnous-mse/portal:2025.2.6-XXX.1402.ed7732337 --no-confirmation"
exit 1
fi
# Extraction des options
DRY_RUN=false
TARGET_STAGE=""
NO_CONFIRMATION=false
IMAGES=()
for arg in "$@"; do
if [ "$arg" == "--dry-run" ]; then
DRY_RUN=true
elif [[ "$arg" == --target-stage=* ]]; then
TARGET_STAGE="${arg#--target-stage=}"
elif [ "$arg" == "--no-confirm" ]; then
NO_CONFIRMATION=true
else
IMAGES+=("$arg")
fi
done
# Variables pour stocker les promotions par stage
PROMOTIONS=(
"## Livré Unstable"
"## Livré Qualif"
"## Livré Preprod"
"## Livré Prod"
)
UNSTABLE=""
QUALIF=""
PREPROD=""
PROD=""
# Fonction pour promouvoir une image Docker
promote_image() {
local SOURCE_TAG="$1"
# Vérifie si le tag source suit le format attendu
if [[ ! "$SOURCE_TAG" =~ ^(.+):([0-9]{4}\.[0-9]+\.[0-9]+)-([a-zA-Z0-9]+)\.([0-9]+)\.([a-z0-9]+)$ ]]; then
echo "Erreur : Le tag source ne suit pas le format attendu : $SOURCE_TAG"
return 1
fi
# Extraction des parties du tag source
local REGISTRY_REPOSITORY="${BASH_REMATCH[1]}"
local VERSION="${BASH_REMATCH[2]}"
local CURRENT_STAGE="${BASH_REMATCH[3]}"
local BUILD_NUMBER="${BASH_REMATCH[4]}"
local COMMIT_HASH="${BASH_REMATCH[5]}"
# Définir le nouveau stage
if [ -n "$TARGET_STAGE" ]; then
NEW_STAGE="$TARGET_STAGE"
else
case "$CURRENT_STAGE" in
"k8sunstable") NEW_STAGE="k8stesting" ;;
"k8stesting") NEW_STAGE="k8spreprod" ;;
"k8spreprod") NEW_STAGE="k8sprod" ;;
"k8sprod")
echo "Erreur : Promotion non définie pour '$CURRENT_STAGE'."
return 1
;;
*) NEW_STAGE="k8sunstable" ;;
esac
fi
# Construire le nouveau tag
local NEW_TAG="${REGISTRY_REPOSITORY}:${VERSION}-${NEW_STAGE}.${BUILD_NUMBER}.${COMMIT_HASH}"
echo "Promotion de tag Docker :"
echo "Source : $SOURCE_TAG "
echo "Cible : $NEW_TAG"
if [ "$DRY_RUN" == true ]; then
echo "Mode dry-run activé. Les commandes Docker ne seront pas exécutées."
return 0
fi
if [ "$NO_CONFIRMATION" != true ]; then
read -p "Souhaitez-vous pousser le nouveau tag ? (o/N) : " CONFIRMATION
if [[ ! "$CONFIRMATION" =~ ^([oO]|oui)$ ]]; then
echo "Annulé par l'utilisateur."
return 0
fi
fi
docker tag "$SOURCE_TAG" "$NEW_TAG"
docker push "$NEW_TAG"
# Regrouper dans la liste appropriée
case "$NEW_STAGE" in
"k8sunstable") UNSTABLE+="- $NEW_TAG\n" ;;
"k8stesting") QUALIF+="- $NEW_TAG\n" ;;
"k8spreprod") PREPROD+="- $NEW_TAG\n" ;;
"k8sprod") PROD+="- $NEW_TAG\n" ;;
esac
}
# Traiter chaque image
for IMAGE in "${IMAGES[@]}"; do
promote_image "$IMAGE"
done
# Afficher le Markdown final
if [ -n "$UNSTABLE" ]; then
echo -e "\n## Livré Unstable\n\n\`\`\`\n$UNSTABLE\`\`\`"
fi
if [ -n "$QUALIF" ]; then
echo -e "\n## Livré Qualif\n\n\`\`\`\n$QUALIF\`\`\`"
fi
if [ -n "$PREPROD" ]; then
echo -e "\n## Livré Preprod\n\n\`\`\`\n$PREPROD\`\`\`"
fi
if [ -n "$PROD" ]; then
echo -e "\n## Livré Prod\n\n\`\`\`\n$PROD\`\`\`"
fi