-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-init-tekton-compatible.sh
More file actions
executable file
·57 lines (45 loc) · 1.68 KB
/
git-init-tekton-compatible.sh
File metadata and controls
executable file
·57 lines (45 loc) · 1.68 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
#!/usr/bin/env bash
[ -z "$DEBUG" ] || set -x
set -eo pipefail
# Replaces Tekton's git-init which lacks retries: https://github.com/tektoncd/pipeline/issues/3515
# typical use:
# args:
# - -url
# - $(params.giturl)
# - -revision
# - $(params.revision)
# - -path
# - /workspace/source
# - -submodules=false
# - -depth=1
[ $# != 8 ] && echo "Expected 8 args, got $# for $0: $@" && exit 1
[ "$1" != "-url" ] && echo "First arg should be -url, was $1" && exit 1
[ "$3" != "-revision" ] && echo "Third arg should be -revision, was $3" && exit 1
[ "$5" != "-path" ] && echo "Fifth arg should be -path, was $5" && exit 1
[ "$7" != "-submodules=false" ] && echo "Seventh arg should be -submodules=false, was $7" && exit 1
[ "$8" != "-depth=1" ] && echo "Eighth arg should be -depth=1, was $8" && exit 1
URL="$2"
[ -z "$URL" ] && echo "Second arg should be URL" && exit 1
REVISION="$4"
[ -z "$REVISION" ] && echo "Fourth arg should be revision" && exit 1
CLONEPATH="$6"
[ -z "$CLONEPATH" ] && echo "Sixth arg should be clonepath" && exit 1
[ -d $CLONEPATH ] || mkdir -m 700 -p $CLONEPATH
cd $CLONEPATH
[ -d "$CLONEPATH/.git" ] && git remote -v && git remote set-url origin $URL || {
git init
git remote add origin $URL
}
# https://github.com/tektoncd/pipeline/blob/v0.41.0/pkg/git/git.go#L285
git config core.sparsecheckout true
retries=3
until git fetch --depth=1 origin --update-head-ok --force $REVISION; do
[ $retries -gt 0 ] || exit 1
retries=$(( $retries - 1 ))
wait=$((10 + $RANDOM % 50))
echo "Git failed, retrying in ${wait}s"
sleep $wait
done
git show-ref "origin/$REVISION" \
&& git checkout -f -B $REVISION origin/$REVISION \
|| git checkout -f $REVISION