Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/gh_retry.sh
2050 views
1
#!/bin/bash
2
3
# This script is used to retry failed workflows in github actions.
4
# It uses gh cli to fetch the failed workflows and then rerun them.
5
# It also checks the logs of the failed workflows to see if it is a flaky test.
6
# If it is a flaky test, it will rerun the failed jobs in the workflow.
7
# eg:
8
# ./gh_retry.sh -h to see the help.
9
# ./gh_retry.sh will run the script with default values.
10
11
# You can also pass the following arguments:
12
# ./gh_retry.sh -b master -l 30 -t "30 mins ago" -w "Build Test"
13
14
#Initialize variables to default values.
15
BRANCH=$(git symbolic-ref --short HEAD)
16
LIMIT=30
17
BEFORE="30 mins ago"
18
WORKFLOW="Build Test"
19
20
# You can add multiple patterns separated by |
21
GREP_ERROR_PATTERN='Test "http/interactsh.yaml" failed'
22
23
#Set fonts for Help.
24
NORM=$(tput sgr0)
25
BOLD=$(tput bold)
26
REV=$(tput smso)
27
28
HELP()
29
{
30
# Display Help
31
echo "Script to retry failed workflows in github actions."
32
echo
33
echo "Syntax: scriptTemplate [-b]"
34
echo "options:"
35
echo "${REV}-b${NORM} Branch to check failed workflows/jobs. Default is ${BOLD}$BRANCH${NORM}."
36
echo "${REV}-l${NORM} Maximum number of runs to fetch. Default is ${BOLD}$LIMIT${NORM}."
37
echo "${REV}-t${NORM} Time to filter the failed jobs. Default is ${BOLD}$BEFORE${NORM}."
38
echo "${REV}-w${NORM} Workflow to filter the failed jobs. Default is ${BOLD}$WORKFLOW${NORM}."
39
echo
40
}
41
42
while getopts :b:l:t:w:h FLAG; do
43
case $FLAG in
44
b)
45
BRANCH=$OPTARG
46
;;
47
l)
48
LIMIT=$OPTARG
49
;;
50
t)
51
BEFORE=$OPTARG
52
;;
53
w)
54
WORKFLOW=$OPTARG
55
;;
56
h) #show help
57
HELP
58
exit 0
59
;;
60
\?) #unrecognized option - show help
61
echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allowed."
62
HELP
63
exit 0
64
;;
65
esac
66
done
67
shift $((OPTIND-1))
68
69
function print_bold() {
70
echo "${BOLD}$1${NORM}"
71
}
72
73
function retry_failed_jobs() {
74
print_bold "Checking failed workflows for branch $BRANCH before $BEFORE"
75
76
date=$(date +%Y-%m-%d'T'%H:%M'Z' -d "$BEFORE")
77
78
workflowIds=$(gh run list --limit "$LIMIT" --json headBranch,status,name,conclusion,databaseId,updatedAt | jq -c '.[] |
79
select ( .headBranch==$branch ) |
80
select ( .name | contains($workflow) ) |
81
select ( .conclusion=="failure" ) |
82
select ( .updatedAt > $date) ' --arg date "$date" --arg branch "$BRANCH" --arg workflow "$WORKFLOW" | jq .databaseId)
83
84
# convert line separated by space to array
85
eval "arr=($workflowIds)"
86
87
if [[ -z $arr ]]
88
then
89
print_bold "Could not find any failed workflows in the last $BEFORE"
90
exit 0
91
fi
92
93
for s in "${arr[@]}"; do
94
print_bold "Checking logs of failed workflow $s to see if it is a flaky test"
95
gh run view "$s" --log-failed | grep -E "$GREP_ERROR_PATTERN" > /dev/null
96
if [ $? == 0 ] ; then
97
print_bold "Retrying failed jobs $s"
98
gh run rerun "$s" --failed
99
sleep 10s
100
gh run view "$s"
101
fi
102
done
103
}
104
105
retry_failed_jobs
106
107