Skip to content

Commit

Permalink
[POC] Add ability to trigger remote workflow runner outside functiona…
Browse files Browse the repository at this point in the history
…l test repo

Signed-off-by: manasvinibs <[email protected]>
  • Loading branch information
manasvinibs committed Nov 20, 2023
1 parent 76ec82d commit f230951
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/test-remote-cypress-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Trigger Remote Cypress Workflow

on:
workflow_dispatch:
inputs:
remote_workflow:
description: 'Trigger Remote Cypress Workflow'
required: false
push:
branches: [ '**' ]

jobs:
trigger-cypress:
runs-on: ubuntu-latest
env:
REMOTE_GITHUB_PAT: ${{secrets.REMOTE_GITHUB_PAT}}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Run Bash Script
run: |
./remoteCypress.sh
11 changes: 10 additions & 1 deletion integtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ function usage() {
echo "--------------------------------------------------------------------------"
}

while getopts ":hb:p:s:c:t:v:o:" arg; do
# DOESN'T HAPPEN HERE. PASS OPTIONS TO RUNNER
# USE MANIFEST
# OR pass by jenkins
# build urls on our side
# For opensearch: https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/$version/latest/$platform/$arch/$dist/dist/opensearch/opensearch-$version-$platform-$arch.tar.gz
# For osd: https://ci.opensearch.org/ci/dbc/distribution-build-opensearch-dashboards/$version/$build_number/$platform/$arch/$dist/dist/opensearch-dashboards/opensearch-dashboards-$version-$platform-$arch.tar.gz
# unpack and scale up
# run tests

while getopts ":hb:p:s:c:t:v:o:r:" arg; do
case $arg in
h)
usage
Expand Down
113 changes: 113 additions & 0 deletions remoteCypress.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash

REMOTE_REPO="manasvinibs/OpenSearch-Dashboards" # This is for POC, will be updated to read from manifest file.
WORKFLOW_NAME="remote_cypress_workflow.yml"
API_URL="https://api.github.com/repos/$REMOTE_REPO/actions/workflows/$WORKFLOW_NAME/dispatches"
PAYLOAD="{\"ref\": \"POC\",\"inputs\":{\"build_id\":\"abc1003\"}}" # Build id is the unique id generated for each execution.
GITHUB_TOKEN="$REMOTE_GITHUB_PAT"

# Maximum number of retries for triggering remote runner
MAX_RETRIES=3

# Trigger the remote github workflow using curl and the PAT token
trigger_remote_workflow() {
curl -L -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-w "%{http_code}" \
"$API_URL" -d "$PAYLOAD"
}
echo "Triggering remote GitHub workflow for Cypress tests in repository: $REMOTE_REPO"

# Attempt to trigger the remote workflow with retries
for ((i = 1; i <= MAX_RETRIES; i++)); do
echo "Attempting to trigger remote workflow (Attempt $i)"
status_code=$(trigger_remote_workflow)
echo "status_code: $status_code"

if [[ $status_code -ge 200 && $status_code -lt 300 ]]; then
echo "Remote workflow triggered successfully."
break
else
echo "Failed to trigger remote workflow. Retrying..."
sleep 10 # Adds a delay between retries
fi

if [ $i -eq $MAX_RETRIES ]; then
echo "Maximum number of retries reached. Exiting."
exit 1
fi
done

# Function to check the status of the remote workflow by constantly polling the workflow-run
check_remote_workflow_status() {
local run_id
local status
local conclusion
local run_details
local workflow_runs

# Make a GET request to the GitHub API to get the list of workflow runs
workflow_runs=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/$REMOTE_REPO/actions/workflows/$WORKFLOW_NAME/runs")
echo "$workflow_runs: $workflow_runs"

# Extract the JSON object whose "name" field contains the string with unique id of the workflow
matching_workflow=$(echo "$workflow_runs" | jq '.workflow_runs[] | select(.name | contains("abc1003"))')

# Check if a matching object was found
if [ -n "$matching_workflow" ]; then
# Extract the "jobs_url" value from the matching object
jobs_url=$(echo "$matching_workflow" | jq -r '.jobs_url')
run_id=$(echo "$matching_workflow" | jq -r '.id')
echo "Job URL: $jobs_url"
echo "Run Id: $run_id"

local retries=1
local max_retries=4 # This will be adjusted to the polling window period.

while [ $retries -lt $max_retries ]; do

echo "Checking the workflow run API status, attempt: $retries"

run_details=$(curl -L -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/$REMOTE_REPO/actions/runs/$run_id")
echo "response: $run_details"

# Extract status and conclusion from the run details
status=$(echo "$run_details" | jq -r ".status")
conclusion=$(echo "$run_details" | jq -r ".conclusion")

# Check if the status indicates that the workflow is complete
if [[ "$status" == "completed" ]]; then
echo "Workflow completed with status: $status"

# Check if it was successful
if [[ $conclusion == "success" ]]; then
echo "Remote workflow completed successfully."
return 0 # Success
else
echo "Remote workflow completed with errors. Conclusion: $conclusion"

# Parse the workflow to find any failures in the test
failures=$(echo "$run_details" | jq -r '.jobs[] | select(.conclusion == "failure") | .name')
echo "Test failures: $failures"

return 1 # Failure
fi
else
echo "Remote workflow is still running. Waiting..."
sleep 600 # Wait for 10 minutes before checking again
((retries++))
fi
done

echo "Remote workflow didn't complete within the specified time."
return 1 # Failure
else
echo "No matching workflow run object found."
fi
}

# Check the status of the remote workflow
check_remote_workflow_status

exit 0

0 comments on commit f230951

Please sign in to comment.