mixins.yml (3956B)
1 .with-local-repo-bash: 2 variables: 3 GIT_STRATEGY: "none" 4 FETCH_TIMEOUT: 180 # 3 minutes 5 before_script: 6 - git init 7 - git remote add local "$LOCAL_REPO_PATH" 8 - | 9 # Determine the reference of the target branch in the local repository copy. 10 # 11 # 1. List all references in the local repository 12 # 2. Filter the references to the target branch 13 # 3. Remove tags 14 # 4. Keep a single line, in case there are too many matches 15 # 5. Clean up the output 16 # 6. Remove everything before the last two slashes, because the output is like `refs/heads/...` or `refs/remotes/...` 17 TARGET_BRANCH=$(git ls-remote local | grep ${CI_COMMIT_BRANCH:-$CI_MERGE_REQUEST_TARGET_BRANCH_NAME} | grep -v 'refs/tags/' | awk '{print $2}' | tail -1 | sed 's|[^/]*/[^/]*/||') 18 if [ -z "$TARGET_BRANCH" ]; then 19 echo "Target branch $TARGET_BRANCH is not yet in local repository. Stopping the pipeline." 20 exit 1 21 fi 22 - git fetch --depth 500 local $TARGET_BRANCH 23 - git --no-pager log FETCH_HEAD --oneline -n 5 24 - git remote add origin "$CI_REPOSITORY_URL" 25 - | 26 if [ -z "${CI_COMMIT_BRANCH:-$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}" ]; then 27 echo "No branch specified. Stopping the pipeline." 28 exit 1 29 fi 30 - echo "Fetching from remote branch ${CI_COMMIT_BRANCH:-$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} with a ${FETCH_TIMEOUT}s timeout." 31 - | 32 fetch_with_timeout() { 33 local remote=$1 34 local branch=$2 35 36 set +e 37 timeout ${FETCH_TIMEOUT} git fetch "$remote" "$branch" 38 local fetch_exit=$? 39 set -e 40 41 if [ "$fetch_exit" -eq 124 ]; then 42 echo "Fetching failed for branch ${remote}/${branch} due to a timeout. Try again later." 43 echo "Gitlab may be experiencing slowness or the local copy of the repository on the CI server may be oudated." 44 return 1 45 fi 46 47 return $fetch_exit 48 } 49 50 if ! fetch_with_timeout origin "${CI_COMMIT_BRANCH:-$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"; then 51 echo "Fetching failed for branch ${CI_COMMIT_BRANCH:-$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}." 52 echo "Attempting to fetch the merge request branch, assuming this pipeline is not running in a fork." 53 54 fetch_with_timeout origin "merge-requests/${CI_MERGE_REQUEST_IID}/head" || exit 1 55 fi 56 - git checkout FETCH_HEAD 57 58 .with-local-repo-pwsh: 59 variables: 60 GIT_STRATEGY: "none" 61 before_script: 62 - git init 63 - git remote add local $env:LOCAL_REPO_PATH 64 - | 65 $branchName = $env:CI_COMMIT_BRANCH 66 if ([string]::IsNullOrEmpty($branchName)) { 67 $branchName = $env:CI_MERGE_REQUEST_TARGET_BRANCH_NAME 68 } 69 $TARGET_BRANCH = git ls-remote local | Select-String -Pattern $branchName | Select-String -Pattern -NotMatch 'refs/tags/' | Select-Object -Last 1 | ForEach-Object { $_.ToString().Split()[1] -replace '^[^/]*/[^/]*/', '' } 70 if ([string]::IsNullOrEmpty($TARGET_BRANCH)) { 71 Write-Output "Target branch $TARGET_BRANCH is not yet in local repository. Stopping the pipeline." 72 exit 1 73 } 74 - git remote add origin $env:CI_REPOSITORY_URL 75 - | 76 $branchName = $env:CI_COMMIT_BRANCH 77 if ([string]::IsNullOrEmpty($branchName)) { 78 $branchName = $env:CI_MERGE_REQUEST_SOURCE_BRANCH_NAME 79 } 80 if ([string]::IsNullOrEmpty($branchName)) { 81 Write-Output "No branch specified. Stopping the pipeline." 82 exit 1 83 } 84 - Write-Output "Fetching from remote branch $branchName" 85 - | 86 if (! git fetch origin $branchName) { 87 Write-Output "Fetching failed for branch $branchName from $env:CI_REPOSITORY_URL." 88 Write-Output "Attempting to fetch the merge request branch, assuming this pipeline is not running in a fork." 89 git fetch origin "merge-requests/$env:CI_MERGE_REQUEST_IID/head" 90 } 91 - git checkout FETCH_HEAD