forked from semaphoreci/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcheckout
More file actions
executable file
·143 lines (121 loc) · 3.8 KB
/
libcheckout
File metadata and controls
executable file
·143 lines (121 loc) · 3.8 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
function checkout() {
if [ -n "$SEMAPHORE_SNAPSHOT_ID" ]; then
checkout::snapshot
return 0
fi
if [ -z $SEMAPHORE_GIT_BRANCH ] || [ -z $SEMAPHORE_GIT_URL ] || [ -z $SEMAPHORE_GIT_DIR ] || [ -z $SEMAPHORE_GIT_SHA ]; then
checkout::validation_message
return 1
fi
if [ "$1" = "--use-cache" ]; then
echo "[Experimental stability] Using cached Git repository."
checkout::use_cache
else
checkout::shallow
fi
}
function checkout::validation_message() {
echo "[CHECKOUT ERROR] Some of these variables are unset:"
echo "SEMAPHORE_GIT_URL=$SEMAPHORE_GIT_URL"
echo "SEMAPHORE_GIT_DIR=$SEMAPHORE_GIT_DIR"
echo "SEMAPHORE_GIT_BRANCH=$SEMAPHORE_GIT_BRANCH"
echo "SEMAPHORE_GIT_SHA=$SEMAPHORE_GIT_SHA"
}
function checkout::snapshot() {
echo SEMAPHORE_SNAPSHOT_ID: $SEMAPHORE_SNAPSHOT_ID
WORKDIR=snapshot
mkdir -p ~/$WORKDIR
cd ~/$WORKDIR
DOMAIN=snapshots.semaphoreci.com
URL="https://$DOMAIN/api/v1alpha/snapshots/$SEMAPHORE_SNAPSHOT_ID"
AUTH_HEADER="Authorization: Token omsAfx8Ly51RJ127xX6c"
curl $URL -H "$AUTH_HEADER" | tar xzf -
}
function checkout::use_cache() {
cache restore git-cache-
if [ -d "$HOME/$SEMAPHORE_GIT_DIR" ]; then
checkout::fetch
else
checkout::clone
fi
checkout::cache_store
}
function checkout::fetch() {
local branch_ref="refs/heads/$SEMAPHORE_GIT_BRANCH"
local branch_origin="origin/$SEMAPHORE_GIT_BRANCH"
echo "Restored cache"
cd $HOME/$SEMAPHORE_GIT_DIR
git remote prune origin
git fetch --tags origin
if [[ -n $(git show-ref $branch_ref) ]]; then
git checkout $SEMAPHORE_GIT_BRANCH
else
git checkout -b $SEMAPHORE_GIT_BRANCH -t $branch_origin;
fi
git reset --hard $SEMAPHORE_GIT_SHA
}
function checkout::clone() {
git clone $SEMAPHORE_GIT_URL $SEMAPHORE_GIT_DIR
cd $SEMAPHORE_GIT_DIR
git checkout $SEMAPHORE_GIT_BRANCH
git reset --hard $SEMAPHORE_GIT_SHA
}
function checkout::cache_store() {
# update cache if older then 72h -> 25920s
if [ -z ${SEMAPHORE_GIT_CACHE_AGE} ]; then
SEMAPHORE_GIT_CACHE_AGE=259200
fi
local cache_key=$(cache list 2>&1 | grep git-cache- | awk '{ print $1 }' | head -1)
local cache_age=$(echo $cache_key | cut -d'-' -f8)
if [[ ! -z "$cache_age" ]] && [[ $cache_age =~ ^[0-9]+$ ]]; then
local now=$(date +%s)
local diff=$(expr $now - $cache_age)
echo "diff: $diff"
if (( diff > $SEMAPHORE_GIT_CACHE_AGE )); then
echo "Git cache outdated, refreshing..."
cd ..
checkout::cleanupcache
cache store "git-cache-$SEMAPHORE_JOB_ID-`date +%s`" $SEMAPHORE_GIT_DIR
cd $SEMAPHORE_GIT_DIR
else
echo "Git cache up-to-data."
fi
else
echo "No git cache... caching"
cd ..
cache store "git-cache-${SEMAPHORE_JOB_ID}-`date +%s`" $SEMAPHORE_GIT_DIR
cd $SEMAPHORE_GIT_DIR
fi
}
function checkout::cleanupcache {
if [ -z ${SEMAPHORE_GIT_CACHE_KEEP} ]; then
SEMAPHORE_GIT_CACHE_KEEP=0
fi
local k=$SEMAPHORE_GIT_CACHE_KEEP
local list=($(cache list 2>&1 | grep git-cache- | awk '{ print $1 }' | xargs))
for i in ${list[@]:$k}; do
cache delete $i
done
}
function checkout::shallow() {
if [ -z ${SEMAPHORE_GIT_DEPTH} ]; then
SEMAPHORE_GIT_DEPTH=50
fi
echo "Performing shallow clone with depth: $SEMAPHORE_GIT_DEPTH"
git clone --depth $SEMAPHORE_GIT_DEPTH -b $SEMAPHORE_GIT_BRANCH $SEMAPHORE_GIT_URL $SEMAPHORE_GIT_DIR 2>/dev/null
if [ $? -ne 0 ]; then
echo "Branch not found performing full clone"
git clone $SEMAPHORE_GIT_URL $SEMAPHORE_GIT_DIR
cd $SEMAPHORE_GIT_DIR
git reset --hard $SEMAPHORE_GIT_SHA 2>/dev/null
else
cd $SEMAPHORE_GIT_DIR
git reset --hard $SEMAPHORE_GIT_SHA 2>/dev/null
if [ $? -ne 0 ]; then
"SHA: $SEMAPHORE_GIT_SHA not found performing full clone"
git fetch --unshallow
git reset --hard $SEMAPHORE_GIT_SHA
fi
fi
}
export -f checkout