forked from rjminsha/pipeline_service_docker_deployer_ice
-
Notifications
You must be signed in to change notification settings - Fork 6
/
git_util.sh
executable file
·73 lines (64 loc) · 2.36 KB
/
git_util.sh
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
#!/bin/bash
#********************************************************************************
# Copyright 2015 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
#********************************************************************************
# uncomment the next line to debug this script
#set -x
# this script is not in Osthanes/utilities because then git would be needed to access it
# and it contains the git retry code. not renamed for compatibility with existing pipelines
# use this function to add retries to a command that returns non-zero on fail
with_retry() {
if [[ $DEBUG -eq 1 ]]; then
local START_TIME=$(date +"%s")
fi
local RETRY_CALL="$*"
echo $RETRY_CALL
$RETRY_CALL
local RETRY_RC=$?
local CURRENT_RETRY_COUNT=0
if [ -z "$CMD_RETRY" ]; then
local CMD_RETRY=5
fi
while [[ $CURRENT_RETRY_COUNT -lt $CMD_RETRY && $RETRY_RC -ne 0 ]]; do
((CURRENT_RETRY_COUNT++))
echo -e "${label_color}${1} command failed; retrying in 3 seconds${no_color} ($CURRENT_RETRY_COUNT of $CMD_RETRY)"
sleep 3
echo $RETRY_CALL
$RETRY_CALL
RETRY_RC=$?
done
if [ $RETRY_RC -ne 0 ]; then
echo -e "${red}${1} command failed: $RETRY_CALL${no_color}" | tee -a "$ERROR_LOG_FILE"
fi
if [[ $DEBUG -eq 1 ]]; then
local END_TIME=$(date +"%s")
export LAST_CMD_TIME=$(($END_TIME-$START_TIME))
echo -e "Cmd '$RETRY_CALL' runtime of `date -u -d @\"$LAST_CMD_TIME\" +'%-Mm %-Ss'`"
fi
return $RETRY_RC
}
# use this function to help avoid pipeline problems when accessing git repositories
git_retry() {
if [ -n "$GIT_RETRY" ]; then
local SAVE_CMD_RETRY=$CMD_RETRY
export CMD_RETRY=$GIT_RETRY
fi
with_retry "git" $*
if [ -n "$SAVE_CMD_RETRY" ]; then
export CMD_RETRY=$SAVE_CMD_RETRY
fi
}
#export functions
export -f git_retry
export -f with_retry