Skip to content

Instantly share code, notes, and snippets.

@xalvarez
Created July 26, 2024 09:04
Show Gist options
  • Select an option

  • Save xalvarez/c446a47ae2467471889a1bc6afd780b6 to your computer and use it in GitHub Desktop.

Select an option

Save xalvarez/c446a47ae2467471889a1bc6afd780b6 to your computer and use it in GitHub Desktop.
Trigger a GitHub Workflow for all of a team's repositories
#!/bin/bash -e
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <github_organization> <github_team> <workflow_filename>"
echo " e.g.: $0 fancy-org fancy-team fancy.yml"
exit 1
fi
ORG=$1
TEAM=$2
WORKFLOW_FILENAME=$3
fetch_repositories() {
gh api graphql -f query="query {
organization(login: \"$ORG\") {
team(slug: \"$TEAM\") {
repositories(first: 100, orderBy: {field: NAME, direction: ASC}) {
nodes {
name
isArchived
}
}
}
}
}" | jq -r '.data.organization.team.repositories.nodes[] | select(.isArchived | not) | .name'
}
workflow_exists() {
local repo=$1
gh api "repos/$ORG/$repo/actions/workflows" | jq -e --arg WORKFLOW_PATH ".github/workflows/$WORKFLOW_FILENAME" '.workflows[] | select(.path == $WORKFLOW_PATH)' >/dev/null
}
trigger_workflow() {
local repo=$1
echo "Triggering workflow $WORKFLOW_FILENAME for repository $repo"
if ! gh workflow run "$WORKFLOW_FILENAME" -R "$ORG/$repo"; then
echo "Failed to trigger workflow for repository $repo" >&2
fi
}
repositories=$(fetch_repositories)
echo "$repositories" | while read -r REPO; do
if workflow_exists "$REPO"; then
trigger_workflow "$REPO"
else
echo "Workflow $WORKFLOW_FILENAME does not exist for repository $REPO. Skipping."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment