4NK_dev/scripts/tag.sh

91 lines
2.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Usage: ./tag.sh [TAG_NAME]
# Par défaut, TAG_NAME="dev-test"
TAG_NAME="${1:-dev-test}"
ROOT_DIR="/home/desk/code/4NK_dev"
print_info() { echo "[tag] $*"; }
print_warn() { echo "[tag][warn] $*"; }
delete_tag_if_exists() {
local tag="$1"
if git show-ref --tags --quiet --verify "refs/tags/${tag}"; then
git tag -d "${tag}" >/dev/null || true
git push origin ":refs/tags/${tag}" >/dev/null || true
fi
}
ensure_branch_checked_out() {
local branch="$1"
if git rev-parse --verify "origin/${branch}" >/dev/null 2>&1; then
git checkout "${branch}" >/dev/null 2>&1 || git checkout -b "${branch}" "origin/${branch}" >/dev/null 2>&1 || true
git pull --ff-only >/dev/null 2>&1 || true
else
print_info "branche ${branch} introuvable, utilisation de la branche courante"
fi
}
tag_current_repo() {
local tag="$1"
local ref_sha
ref_sha=$(git rev-parse --verify HEAD)
delete_tag_if_exists "${tag}"
git tag -a "${tag}" -m "CI ${tag} trigger" "${ref_sha}"
git push origin "refs/tags/${tag}"
}
get_submodule_names() {
git config -f .gitmodules --name-only --get-regexp 'submodule\..*\.path' | sed -E 's/\.path$//'
}
tag_root_repo() {
cd "${ROOT_DIR}"
print_info "repository racine: $(basename "${ROOT_DIR}") -> ${TAG_NAME}"
local dev_head
dev_head=$(git rev-parse --verify refs/heads/dev)
delete_tag_if_exists "${TAG_NAME}"
git tag -a "${TAG_NAME}" -m "CI ${TAG_NAME} trigger" "${dev_head}"
git push origin "refs/tags/${TAG_NAME}"
}
tag_submodules() {
print_info "lecture des submodules depuis .gitmodules"
local names
mapfile -t names < <(get_submodule_names)
for name in "${names[@]}"; do
local sm_path sm_url sm_branch
sm_path=$(git config -f .gitmodules --get "${name}.path" || true)
sm_url=$(git config -f .gitmodules --get "${name}.url" || true)
sm_branch=$(git config -f .gitmodules --get "${name}.branch" || echo dev)
if [ -z "${sm_path}" ] || [ -z "${sm_url}" ]; then
print_warn "entrée incomplète: ${name}, ignorée"
continue
fi
if [ ! -d "${sm_path}" ]; then
print_warn "dossier manquant: ${sm_path}, on saute"
continue
fi
print_info "${sm_path} (${sm_url}) -> ${TAG_NAME} sur branche ${sm_branch}"
(
cd "${sm_path}"
git fetch --all --prune >/dev/null 2>&1 || true
ensure_branch_checked_out "${sm_branch}"
tag_current_repo "${TAG_NAME}"
) || print_warn "échec sur ${sm_path}, on continue"
done
}
main() {
cd "${ROOT_DIR}"
tag_root_repo
tag_submodules
print_info "terminé: ${TAG_NAME} posé sur le repo racine et submodules accessibles"
}
main "$@"