diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0f7ff585fb5964fedf565e073c06b232305c5560..e0d51de8e7ff10bbac7209949dab1caeadfb03cc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,10 +8,19 @@ stages: variables: # Variables required by Common CI jobs - CI_COMMON_JOB_VERSION: "d4e29410ea168096e1a822f77c7ce741d9cfb57a" + CI_COMMON_JOB_VERSION: "latest" DOCKER_BUILDER_TAG: "$CI_COMMON_JOB_VERSION" DOCKER_DIND_TAG: "$CI_COMMON_JOB_VERSION" IMAGE_REMOVER_TAG: "$CI_COMMON_JOB_VERSION" + # Git configuration + # Fetch strategy reuses workspace between jobs, reducing GitLab server load. + # Full clone (depth 0) enables efficient incremental fetches - shallow clones + # don't reduce server CPU and make fetch less effective. + GIT_STRATEGY: fetch + GIT_DEPTH: 0 + # Temporary: separate clone path prevents clone-strategy jobs from erasing + # fetch workspaces during transition. Remove once all projects use fetch. + GIT_CLONE_PATH: $CI_BUILDS_DIR/fetch/$CI_RUNNER_SHORT_TOKEN/$CI_CONCURRENT_ID/$CI_PROJECT_PATH include: - template: Workflows/Branch-Pipelines.gitlab-ci.yml @@ -19,6 +28,56 @@ include: ref: d4e29410ea168096e1a822f77c7ce741d9cfb57a # develop file: "/templates/docker_image_jobs.gitlab-ci.yml" +default: + hooks: + pre_get_sources_script: + # Clean corrupt git state left by cancelled pipelines (see GitLab #296638, #4600) + # Wrapped in subshell to avoid changing working directory for subsequent git operations + - | + ( + cd "${CI_PROJECT_DIR:-/builds}" 2>/dev/null || exit 0 + echo "pre_get_sources: checking $(pwd) for corrupt git state" + if [ -d ".git" ]; then + # Remove stale lock files that block git operations + find .git -name "*.lock" -delete 2>/dev/null || true + + # Check if main repo is corrupt - if so, remove .git to force fresh clone + if ! git rev-parse HEAD >/dev/null 2>&1; then + echo "pre_get_sources: main repository corrupt, forcing fresh clone" + rm -rf .git + else + # Main repo OK - check and clean corrupt submodules + # Check both the working dir and .git/modules/ since either can be corrupt + if [ -f ".gitmodules" ]; then + git config --file .gitmodules --get-regexp path 2>/dev/null | awk '{print $2}' | while read submod; do + needs_clean=false + [ -z "$submod" ] && continue + # Check if submodule working directory exists but is corrupt + if [ -d "$submod" ] && [ -f "$submod/.git" ]; then + if ! git -C "$submod" rev-parse HEAD >/dev/null 2>&1; then + needs_clean=true + fi + fi + # Check if .git/modules exists but is corrupt (even if working dir is gone) + if [ -d ".git/modules/$submod" ]; then + if ! git --git-dir=".git/modules/$submod" rev-parse HEAD >/dev/null 2>&1; then + echo "pre_get_sources: $submod corrupt (rev-parse failed)" + needs_clean=true + fi + fi + if [ "$needs_clean" = true ]; then + echo "pre_get_sources: cleaning corrupt submodule: $submod" + rm -rf "$submod" ".git/modules/$submod" + fi + done + fi + echo "pre_get_sources: existing repo OK" + fi + else + echo "pre_get_sources: no .git directory (fresh workspace)" + fi + ) + docker-build: extends: .docker_image_builder_job_template stage: build