Skip to content

Commit

Permalink
chore: Fix rerun failing workflow (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylimnardy authored Sep 9, 2024
1 parent 0dcb472 commit e075f5c
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions hack/await_workflow.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,63 @@ set -o pipefail # prevents errors in a pipeline from being masked
# Variables (you need to set these)
REPO_OWNER="kyma-project"
REPO_NAME="telemetry-manager"
WORKFLOW_NAME="Build Image"
CHECK_NAME="Build-Image-Success"

# retry until workflow conclusion is success and status is completed
# retry until check is found and status is completed
# timeout after 15 minutes

TIMEOUT=900
QUERY_INTERVAL=10

START_TIME=$SECONDS

found=false
status=""
conclusion=""

until [[ $status == "completed" ]]; do
until [[ $found == true && $status == "completed" ]]; do
# Wait for timeout
if (( SECONDS - START_TIME > TIMEOUT )); then
echo "Timeout reached: Workflow not found within $(( TIMEOUT/60 )) minutes"
echo "Timeout reached: Check not finished within $(( TIMEOUT/60 )) minutes"
exit 1
fi

echo "Waiting for workflow: $WORKFLOW_NAME"
echo "Waiting for check: $CHECK_NAME"

# Get the latest workflow run status
# Get the latest check run status
response=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/actions/runs)
https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/commits/$COMMIT_SHA/check-runs)


# Check if .workflow_runs exists and is not null
if [ "$(echo "$response" | jq -r '.workflow_runs')" == "null" ]; then
# Check if .check_runs exists and is not null
if [ "$(echo "$response" | jq -r '.check_runs')" == "null" ]; then
echo "$response"
exit 1
fi

# Extract all head_sha and status from response and put it into an array
workflows=$(echo $response | jq -c '.workflow_runs[] | {name, head_sha, status, conclusion}')
checks=$(echo "$response" | jq -c '.check_runs[] | {name, head_sha, status, conclusion}')

# Iterate over the JSON objects
while IFS= read -r workflow; do
workflow_name=$(echo "$workflow" | jq -r '.name')
head_sha=$(echo "$workflow" | jq -r '.head_sha')
status=$(echo "$workflow" | jq -r '.status')
conclusion=$(echo "$workflow" | jq -r '.conclusion')
while IFS= read -r check; do
check_name=$(echo "$check" | jq -r '.name')
head_sha=$(echo "$check" | jq -r '.head_sha')
status=$(echo "$check" | jq -r '.status')
conclusion=$(echo "$check" | jq -r '.conclusion')

if [[ "$head_sha" == "$COMMIT_SHA" && "$workflow_name" == "$WORKFLOW_NAME" ]]; then
if [[ "$head_sha" == "$COMMIT_SHA" && "$check_name" == "$CHECK_NAME" ]]; then
found=true
break
fi
done <<< "$workflows"
done <<< "$checks"

if [ "$found" = false ]; then
echo "Workflow not found"
exit 1
echo "Check not yet found."
sleep $QUERY_INTERVAL
continue
fi

# Output the results
Expand All @@ -72,12 +74,11 @@ until [[ $status == "completed" ]]; do
echo "Conclusion: $conclusion"
echo ""

sleep 10
done

if [ $conclusion != "success" ]; then
echo "Workflow $status with conclusion: $conclusion"
if [ "$conclusion" != "success" ]; then
echo "Check $status with conclusion: $conclusion"
exit 1
fi

echo "Workflow '$WORKFLOW_NAME' $status with conclusion: $conclusion"
echo "Check '$CHECK_NAME' $status with conclusion: $conclusion"

0 comments on commit e075f5c

Please sign in to comment.