2222222222 #77
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Triggered Tests | |
on: | |
pull_request_target: | |
types: [opened, synchronize, reopened] | |
workflow_dispatch: | |
permissions: | |
contents: read | |
env: | |
DEBUG_MODE: "true" # Set to "false" to reduce verbosity | |
jobs: | |
trigger-azure-pipeline: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Trigger Azure DevOps Pipeline | |
id: trigger | |
uses: Azure/pipelines@v1.2 | |
with: | |
azure-devops-project-url: 'https://dev.azure.com/sergiovelderrain/sergiovelderrain' | |
azure-pipeline-name: 'ni.labview-icon-editor-test' | |
azure-devops-token: ${{ secrets.AZURE_DEVOPS_PAT }} | |
- name: Find the most recent pipeline run | |
id: find-run | |
env: | |
ORGANIZATION: sergiovelderrain | |
PROJECT_NAME: sergiovelderrain | |
PIPELINE_DEFINITION_ID: "3" # Replace with your pipeline definition ID | |
DEBUG_MODE: ${{ env.DEBUG_MODE }} | |
AZURE_DEVOPS_PAT: ${{ secrets.AZURE_DEVOPS_PAT }} | |
run: | | |
set -euo pipefail | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Debug mode enabled." | |
fi | |
if [ -z "${AZURE_DEVOPS_PAT}" ]; then | |
echo "Error: AZURE_DEVOPS_PAT not set." >&2 | |
exit 1 | |
fi | |
AUTH_HEADER=$(printf ":%s" "${AZURE_DEVOPS_PAT}" | base64 -w 0) | |
API_URL="https://dev.azure.com/${ORGANIZATION}/${PROJECT_NAME}/_apis/build/builds?definitions=${PIPELINE_DEFINITION_ID}&\$top=1&api-version=6.0" | |
MAX_ATTEMPTS=10 | |
SLEEP_SECONDS=5 | |
ATTEMPT=1 | |
BUILD_ID="" | |
while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Fetching latest build (Attempt $ATTEMPT/$MAX_ATTEMPTS) from $API_URL" | |
fi | |
rm -f response.json || true | |
if ! wget --quiet --header="Authorization: Basic ${AUTH_HEADER}" -O response.json "$API_URL"; then | |
echo "Error: Failed to fetch build runs." >&2 | |
exit 1 | |
fi | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Full response:" | |
cat response.json | jq . | |
fi | |
# Extract the BUILD_ID if available | |
BUILD_ID=$(jq -r '.value[0].id // empty' response.json || true) | |
if [ -n "$BUILD_ID" ]; then | |
# We found a build run, break out of the loop | |
echo "BUILD_ID=$BUILD_ID" >> $GITHUB_ENV | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Found BUILD_ID=$BUILD_ID" | |
fi | |
break | |
else | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::No build found yet. Response:" | |
cat response.json | |
fi | |
echo "No current build found. Waiting $SLEEP_SECONDS seconds before retry..." | |
sleep $SLEEP_SECONDS | |
ATTEMPT=$((ATTEMPT+1)) | |
fi | |
done | |
# After retries, if still no BUILD_ID | |
if [ -z "$BUILD_ID" ]; then | |
echo "Error: No new build ID found after multiple attempts." >&2 | |
cat response.json | |
exit 1 | |
fi | |
- name: Poll pipeline status until completion | |
id: poll-status | |
env: | |
ORGANIZATION: sergiovelderrain | |
PROJECT_NAME: sergiovelderrain | |
DEBUG_MODE: ${{ env.DEBUG_MODE }} | |
AZURE_DEVOPS_PAT: ${{ secrets.AZURE_DEVOPS_PAT }} | |
run: | | |
set -euo pipefail | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Debug mode enabled for polling." | |
fi | |
if [ -z "${AZURE_DEVOPS_PAT}" ]; then | |
echo "Error: AZURE_DEVOPS_PAT not set." >&2 | |
exit 1 | |
fi | |
if [ -z "${BUILD_ID:-}" ]; then | |
echo "Error: BUILD_ID not set." >&2 | |
exit 1 | |
fi | |
AUTH_HEADER=$(printf ":%s" "${AZURE_DEVOPS_PAT}" | base64 -w 0) | |
STATUS_URL="https://dev.azure.com/${ORGANIZATION}/${PROJECT_NAME}/_apis/build/builds/${BUILD_ID}?api-version=6.0" | |
MAX_ATTEMPTS=30 | |
SLEEP_SECONDS=10 | |
ATTEMPT=1 | |
while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Attempt $ATTEMPT of $MAX_ATTEMPTS" | |
fi | |
rm -f status.json || true | |
if ! wget --quiet --header="Authorization: Basic ${AUTH_HEADER}" -O status.json "$STATUS_URL"; then | |
echo "Error: Failed to fetch build status." >&2 | |
exit 1 | |
fi | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Pipeline status response:" | |
cat status.json | jq . | |
fi | |
STATUS=$(jq -r '.status // empty' status.json) | |
RESULT=$(jq -r '.result // empty' status.json) | |
if [ -z "$STATUS" ]; then | |
echo "Error: Status not found in pipeline response." >&2 | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Parsed response:" | |
cat status.json | |
fi | |
exit 1 | |
fi | |
echo "Current Status: $STATUS" | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Current Result: $RESULT" | |
fi | |
if [ "$STATUS" = "completed" ]; then | |
break | |
fi | |
sleep $SLEEP_SECONDS | |
ATTEMPT=$((ATTEMPT+1)) | |
done | |
if [ "$STATUS" != "completed" ]; then | |
echo "Error: Build not completed within the timeout." >&2 | |
exit 1 | |
fi | |
if [ "$RESULT" != "succeeded" ]; then | |
echo "Error: Build completed but did not succeed. Result: $RESULT" >&2 | |
exit 1 | |
fi | |
echo "Build succeeded!" |