refactor(ci): structure tag.sh into functions and improve readability
This commit is contained in:
parent
3cd729bb71
commit
1a846b63f1
117
scripts/tag.sh
117
scripts/tag.sh
@ -6,59 +6,86 @@ IFS=$'\n\t'
|
|||||||
# Par défaut, TAG_NAME="dev-test"
|
# Par défaut, TAG_NAME="dev-test"
|
||||||
|
|
||||||
TAG_NAME="${1:-dev-test}"
|
TAG_NAME="${1:-dev-test}"
|
||||||
|
|
||||||
ROOT_DIR="/home/desk/code/4NK_dev"
|
ROOT_DIR="/home/desk/code/4NK_dev"
|
||||||
cd "$ROOT_DIR"
|
|
||||||
|
|
||||||
echo "[tag] repository racine: $(basename "$ROOT_DIR") -> ${TAG_NAME}"
|
print_info() { echo "[tag] $*"; }
|
||||||
DEV_HEAD=$(git rev-parse --verify refs/heads/dev)
|
print_warn() { echo "[tag][warn] $*"; }
|
||||||
if git show-ref --tags --quiet --verify "refs/tags/${TAG_NAME}"; then
|
|
||||||
git tag -d "${TAG_NAME}" >/dev/null
|
|
||||||
git push origin ":refs/tags/${TAG_NAME}" >/dev/null || true
|
|
||||||
fi
|
|
||||||
git tag -a "${TAG_NAME}" -m "CI ${TAG_NAME} trigger" "${DEV_HEAD}"
|
|
||||||
git push origin "refs/tags/${TAG_NAME}"
|
|
||||||
|
|
||||||
echo "[tag] lecture des submodules depuis .gitmodules"
|
delete_tag_if_exists() {
|
||||||
mapfile -t SUBS < <(git config -f .gitmodules --name-only --get-regexp 'submodule\..*\.path' | sed -E 's/\.path$//')
|
local tag="$1"
|
||||||
|
if git show-ref --tags --quiet --verify "refs/tags/${tag}"; then
|
||||||
for NAME in "${SUBS[@]}"; do
|
git tag -d "${tag}" >/dev/null || true
|
||||||
PATHsm=$(git config -f .gitmodules --get "${NAME}.path" || true)
|
git push origin ":refs/tags/${tag}" >/dev/null || true
|
||||||
URLsm=$(git config -f .gitmodules --get "${NAME}.url" || true)
|
|
||||||
BRANCHsm=$(git config -f .gitmodules --get "${NAME}.branch" || echo dev)
|
|
||||||
|
|
||||||
if [ -z "$PATHsm" ] || [ -z "$URLsm" ]; then
|
|
||||||
echo "[tag][warn] entrée incomplète: ${NAME}, ignorée"
|
|
||||||
continue
|
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [ ! -d "$PATHsm" ]; then
|
ensure_branch_checked_out() {
|
||||||
echo "[tag][warn] dossier manquant: $PATHsm, on saute"
|
local branch="$1"
|
||||||
continue
|
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
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
echo "[tag] ${PATHsm} (${URLsm}) -> ${TAG_NAME} sur branche ${BRANCHsm}"
|
tag_current_repo() {
|
||||||
(
|
local tag="$1"
|
||||||
cd "$PATHsm"
|
local ref_sha
|
||||||
git fetch --all --prune || true
|
ref_sha=$(git rev-parse --verify HEAD)
|
||||||
# Tente checkout de la branche cible si disponible
|
delete_tag_if_exists "${tag}"
|
||||||
if git rev-parse --verify "origin/${BRANCHsm}" >/dev/null 2>&1; then
|
git tag -a "${tag}" -m "CI ${tag} trigger" "${ref_sha}"
|
||||||
git checkout "${BRANCHsm}" || git checkout -b "${BRANCHsm}" "origin/${BRANCHsm}" || true
|
git push origin "refs/tags/${tag}"
|
||||||
git pull --ff-only || true
|
}
|
||||||
else
|
|
||||||
echo "[tag][info] branche ${BRANCHsm} introuvable, utilisation de la branche courante"
|
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
|
fi
|
||||||
|
|
||||||
# Supprime le tag local/distant si déjà présent
|
print_info "${sm_path} (${sm_url}) -> ${TAG_NAME} sur branche ${sm_branch}"
|
||||||
if git show-ref --tags --quiet --verify "refs/tags/${TAG_NAME}"; then
|
(
|
||||||
git tag -d "${TAG_NAME}" >/dev/null || true
|
cd "${sm_path}"
|
||||||
git push origin ":refs/tags/${TAG_NAME}" >/dev/null || true
|
git fetch --all --prune >/dev/null 2>&1 || true
|
||||||
fi
|
ensure_branch_checked_out "${sm_branch}"
|
||||||
|
tag_current_repo "${TAG_NAME}"
|
||||||
|
) || print_warn "échec sur ${sm_path}, on continue"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
HEAD_SHA=$(git rev-parse --verify HEAD)
|
main() {
|
||||||
git tag -a "${TAG_NAME}" -m "CI ${TAG_NAME} trigger" "${HEAD_SHA}"
|
cd "${ROOT_DIR}"
|
||||||
git push origin "refs/tags/${TAG_NAME}"
|
tag_root_repo
|
||||||
) || echo "[tag][warn] échec sur ${PATHsm}, on continue"
|
tag_submodules
|
||||||
done
|
print_info "terminé: ${TAG_NAME} posé sur le repo racine et submodules accessibles"
|
||||||
|
}
|
||||||
|
|
||||||
echo "[tag] terminé: ${TAG_NAME} posé sur le repo racine et submodules accessibles"
|
main "$@"
|
||||||
Loading…
x
Reference in New Issue
Block a user