Skip to content

Instantly share code, notes, and snippets.

@yoomlam
Last active September 23, 2022 19:08
Show Gist options
  • Save yoomlam/c99bb70086e79af0bafb281fa9130e43 to your computer and use it in GitHub Desktop.
Save yoomlam/c99bb70086e79af0bafb281fa9130e43 to your computer and use it in GitHub Desktop.
Trigger GitHub Action
trigger-action(){
WORKFLOW_FILE=${1:-mirror.yml}
MY_BRANCH=${2:-`git rev-parse --abbrev-ref HEAD`}
TARGET_REPO=${3:-department-of-veterans-affairs/abd-vro}
echo "Triggering Mirror https://github.com/$TARGET_REPO/actions/workflows/$WORKFLOW_FILE"
gh workflow run --repo "$TARGET_REPO" "$WORKFLOW_FILE" --ref "$MY_BRANCH"
# Alternatives:
# curl -XPOST -u "$GITHUB_USERNAME:$GITHUB_ACCESS_TOKEN" -H "Accept: application/vnd.github+json" -H "Content-Type: application/json" https://api.github.com/repos/department-of-veterans-affairs/abd-vro/actions/workflows/mirror.yml/dispatches --data "{\"ref\": \"$MY_BRANCH\"}"
# gh api /repos/$TARGET_REPO/actions/workflows/$WORKFLOW_FILE/dispatches --input - <<< "{\"ref\": \"$MY_BRANCH\"}"
sleep 2 # to give GH time to initiate the action
MIRROR_JSON=$(gh run list --repo "$TARGET_REPO" -w "$WORKFLOW_FILE" -L 1 --json databaseId,headBranch,status,url --jq ".[] | select((.headBranch == \"$MY_BRANCH\") and (.status != \"completed\"))")
[ "$MIRROR_JSON" ] || { echo "An active $WORKFLOW_FILE action was not found for branch $MY_BRANCH!"; return 3; }
echo $MIRROR_JSON | jq
MIRROR_RUN_ID=$(echo $MIRROR_JSON | jq '.databaseId')
echo "Waiting for action $MIRROR_RUN_ID to complete ..."
sleep 3 # allow time to read the screen before next line clears it
gh run watch $MIRROR_RUN_ID --repo "$TARGET_REPO"
MIRROR_RUN_URL=$(echo $MIRROR_JSON | jq '.url')
echo $MIRROR_JSON | jq
}
# Example usage
MY_BRANCH=${1:-`git rev-parse --abbrev-ref HEAD`}
trigger-action mirror.yml "$MY_BRANCH"
trigger-action secrel.yml "$MY_BRANCH" department-of-veterans-affairs/abd-vro-internal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment