-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathclone-repo.yml
More file actions
179 lines (150 loc) · 6.85 KB
/
Copy pathclone-repo.yml
File metadata and controls
179 lines (150 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
parameters:
- name: targetShaId
type: string
- name: targetBranch
type: string
steps:
- ${{ if endsWith(variables['build.sourceBranch'], '/merge') }}:
- checkout: none
- bash: |
# Constants
RETRY_INTERVAL=10
MAX_ATTEMPTS=3
# Azure devops _doesn't_ clean up the contents of the repo after the pipeline has finished
# So we do it manually
echo "Cleaning $BUILD_REPOSITORY_LOCALPATH/ directory"
# This deletes everything in the folder, including hidden `.` folders
sudo find $BUILD_REPOSITORY_LOCALPATH/. -name . -o -prune -exec rm -rf -- {} +
# As this is a pull request, we need to do a fake merge
# uses similar process to existing checkout task
prBranch=$SYSTEM_PULLREQUEST_SOURCEBRANCH
echo "Checking out merge commit for ${{ parameters.targetShaId }} and $prBranch"
git version
git lfs version
echo "Initializing repository at $BUILD_REPOSITORY_LOCALPATH ..."
git init "$BUILD_REPOSITORY_LOCALPATH"
echo "Adding remote $BUILD_REPOSITORY_URI ..."
git remote add origin "$BUILD_REPOSITORY_URI"
git config gc.auto 0
git config http.retry $(GIT_HTTP_RETRY)
git config http.retryDelay $(GIT_HTTP_RETRY_DELAY)
git config --get-all http.$BUILD_REPOSITORY_URI.extraheader
git config --get-all http.extraheader
git config --get-regexp .*extraheader
git config --get-all http.proxy
git config http.version HTTP/1.1
# Attempt fetching and checking out
for attempt in $(seq 1 $MAX_ATTEMPTS)
do
echo "Attempt $attempt/$MAX_ATTEMPTS: Fetching and checking out: ${{ parameters.targetBranch }} and $prBranch"
git fetch --force --tags --prune --prune-tags --progress --no-recurse-submodules origin +refs/heads/${{ parameters.targetBranch }}:refs/remotes/origin/${{ parameters.targetBranch }} +refs/heads/$prBranch:refs/remotes/origin/$prBranch && git checkout --force $prBranch && break
if [ $attempt -lt $MAX_ATTEMPTS ]; then
echo "Fetch and checkout failed, retrying in $RETRY_INTERVAL seconds"
sleep $RETRY_INTERVAL
else
echo "All attempts to fetch and checkout failed"
exit 1
fi
done
echo "Resetting $prBranch to origin/$prBranch ..."
git reset origin/$prBranch --hard
echo "Running git clean -ffdx ..."
git clean -ffdx
displayName: checkout
condition: and(succeeded(), not(eq(variables['Agent.OS'], 'Windows_NT')))
- bash: |
echo "Updating credentials ..."
git config user.email "gitfun@example.com"
git config user.name "Automatic Merge"
prBranch=$SYSTEM_PULLREQUEST_SOURCEBRANCH
echo "Merging $prBranch with ${{ parameters.targetShaId }} ..."
git merge ${{ parameters.targetShaId }}
mergeInProgress=$?
if [ $mergeInProgress -ne 0 ]
then
echo "Merge failed, rolling back ..."
git merge --abort
exit 1;
fi
echo "Merge successful"
git status
displayName: merge
condition: and(succeeded(), not(eq(variables['Agent.OS'], 'Windows_NT')))
- powershell: |
echo "Cleaning up source directories ..."
$localPath=$env:BUILD_REPOSITORY_LOCALPATH
rm $localPath/* -r -fo -Exclude ".git"
# As this is a pull request, we need to do a fake merge
# uses similar process to existing checkout task
$prBranch=$env:SYSTEM_PULLREQUEST_SOURCEBRANCH
echo "Checking out merge commit for ${{ parameters.targetShaId }} and $prBranch"
echo "Checking out merge commit for ${{ parameters.targetShaId }} and $prBranch"
git version
# Disabled in the windows version
# git lfs version
echo "Initializing repository at $localPath ..."
git init --initial-branch=${{ parameters.targetBranch }} "$localPath"
cd $localPath
$repoUri=$env:BUILD_REPOSITORY_URI
echo "Adding remote $repoUri ..."
git remote add origin "$repoUri"
git config gc.auto 0
git config http.retry $(GIT_HTTP_RETRY)
git config http.retryDelay $(GIT_HTTP_RETRY_DELAY)
git config --get-all http.$repoUri.extraheader
git config --get-all http.extraheader
git config --get-regexp .*extraheader
git config --get-all http.proxy
git config http.version HTTP/1.1
echo "Force fetching ${{ parameters.targetBranch }} and $prBranch ..."
echo "git fetch --force --tags --prune --prune-tags --progress --no-recurse-submodules origin +refs/heads/${{ parameters.targetBranch }}:refs/remotes/origin/${{ parameters.targetBranch }} +refs/heads/${prBranch}:refs/remotes/origin/${prBranch}"
git fetch --force --tags --prune --prune-tags --progress --no-recurse-submodules origin +refs/heads/${{ parameters.targetBranch }}:refs/remotes/origin/${{ parameters.targetBranch }} +refs/heads/${prBranch}:refs/remotes/origin/${prBranch}
echo "Checking out $prBranch..."
git checkout --force $prBranch
echo "Resetting $prBranch to origin/$prBranch ..."
git reset origin/$prBranch --hard
echo "Running git clean -ffdx ..."
git clean -ffdx
displayName: checkout
condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))
- powershell: |
echo "Updating credentials ..."
git config user.email "gitfun@example.com"
git config user.name "Automatic Merge"
$prBranch=$env:SYSTEM_PULLREQUEST_SOURCEBRANCH
echo "Merging $prBranch with ${{ parameters.targetShaId }} ..."
git merge ${{ parameters.targetShaId }}
$mergeInProgress=$LASTEXITCODE
if("$mergeInProgress" -ne "0") {
echo "Merge failed, rolling back ..."
git merge --abort
exit 1;
}
echo "Merge successful"
git status
displayName: merge
condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))
- ${{ else }}:
# The clean doesn't always work, so sudo rm -rf it
- bash: |
echo "Cleaning $BUILD_REPOSITORY_LOCALPATH/ directory"
# This deletes everything in the folder _except the .git folder
# Deleting the git folder causes weird issues with git lfs
sudo find $BUILD_REPOSITORY_LOCALPATH/. -name . -o -prune -exec rm -rf -- {} +
displayName: clean
condition: and(succeeded(), not(eq(variables['Agent.OS'], 'Windows_NT')))
- powershell: rm "$env:BUILD_REPOSITORY_LOCALPATH/*" -r -fo -Exclude ".git"
displayName: clean
condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))
- checkout: self
clean: true
- bash: |
git submodule sync --recursive
git submodule update --init --recursive
displayName: initialize submodules
condition: and(succeeded(), not(eq(variables['Agent.OS'], 'Windows_NT')))
- powershell: |
git submodule sync --recursive
git submodule update --init --recursive
displayName: initialize submodules
condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))