Skip to content

How to wait for any action that finished in the past with given name and SHA #14

Description

@gladykov

Hi! Thank you @kamilchodola for sharing your work. As this was not enough for us, I took liberty to adjust it for our needs. Sharing here in case someone would like to reuse:

If you don't care about run id, and you want to take into account runs, which happened before current action was triggered, you can use this:

#!/bin/bash

# Set the maximum waiting time (in minutes) and initialize the counter
timeout="${TIMEOUT}"
interval="${INTERVAL}"
workflow_name="${WORKFLOW}"
repository_name="${REPOSITORY}"
sha="${SHA}"
counter=0

echo "ℹ️ Inputs:"
echo "ℹ️   Repository: ${repository_name}"
echo "ℹ️   Workflow file name: ${workflow_name}"
echo "ℹ️   Commit SHA: ${sha}"
echo "ℹ️   Timeout for the workflow to complete: ${timeout} minutes"
echo "ℹ️   Interval between checks: ${interval} seconds"

if [[ ! "$SHA" =~ ^[0-9a-f]{40}$ ]]; then
  echo "❌ Invalid commit SHA provided. Exiting."
  exit 1
fi

  while true; do

    response=$(gh run list --repo "${repository_name}" --commit="${sha}" --workflow="${workflow_name}" --status=success --json=status 2>gh_error.log | jq length)
    
    if [ $? -ne 0 ]; then
      echo "❌ Error executing 'gh run list'. Check the error log below:"
      cat gh_error.log
      exit 1
    fi
    
    if [ "$response" != "0" ]; then
      echo "🎉 Workflow ${workflow_name} finished for ${sha}"
      break
    fi

    # Increment the counter and check if the maximum waiting time is reached
    counter=$((counter + 1))
    if [ $((counter * interval)) -ge $((timeout * 60)) ]; then
      echo "Maximum waiting time for the workflow to finish reached. Exiting."
      exit 1
    fi

    sleep "$interval"
  done

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions