... |
... |
@@ -4,7 +4,21 @@ |
4
|
4
|
before_script:
|
5
|
5
|
- git init
|
6
|
6
|
- git remote add local "$LOCAL_REPO_PATH"
|
7
|
|
- - git fetch --depth 500 local
|
|
7
|
+ - |
|
|
8
|
+ # Determine the reference of the target branch in the local repository copy.
|
|
9
|
+ #
|
|
10
|
+ # 1. List all references in the local repository
|
|
11
|
+ # 2. Filter the references to the target branch
|
|
12
|
+ # 3. Remove tags
|
|
13
|
+ # 4. Keep a single line, in case there are too many matches
|
|
14
|
+ # 5. Clean up the output
|
|
15
|
+ # 6. Remove everything before the last two slashes, because the output is like `refs/heads/...` or `refs/remotes/...`
|
|
16
|
+ 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|[^/]*/[^/]*/||')
|
|
17
|
+ if [ -z "$TARGET_BRANCH" ]; then
|
|
18
|
+ echo "Target branch $TARGET_BRANCH is not yet in local repository. Stopping the pipeline."
|
|
19
|
+ exit 1
|
|
20
|
+ fi
|
|
21
|
+ - git fetch --depth 500 local $TARGET_BRANCH
|
8
|
22
|
- git remote add origin "$CI_REPOSITORY_URL"
|
9
|
23
|
- |
|
10
|
24
|
if [ -z "${CI_COMMIT_BRANCH:-$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}" ]; then
|
... |
... |
@@ -26,7 +40,16 @@ |
26
|
40
|
before_script:
|
27
|
41
|
- git init
|
28
|
42
|
- git remote add local $env:LOCAL_REPO_PATH
|
29
|
|
- - git fetch --depth 500 local
|
|
43
|
+ - |
|
|
44
|
+ $branchName = $env:CI_COMMIT_BRANCH
|
|
45
|
+ if ([string]::IsNullOrEmpty($branchName)) {
|
|
46
|
+ $branchName = $env:CI_MERGE_REQUEST_TARGET_BRANCH_NAME
|
|
47
|
+ }
|
|
48
|
+ $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 '^[^/]*/[^/]*/', '' }
|
|
49
|
+ if ([string]::IsNullOrEmpty($TARGET_BRANCH)) {
|
|
50
|
+ Write-Output "Target branch $TARGET_BRANCH is not yet in local repository. Stopping the pipeline."
|
|
51
|
+ exit 1
|
|
52
|
+ }
|
30
|
53
|
- git remote add origin $env:CI_REPOSITORY_URL
|
31
|
54
|
- |
|
32
|
55
|
$branchName = $env:CI_COMMIT_BRANCH
|