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 | |
pull-requests: write | |
env: | |
DEBUG_MODE: "false" # Set to "false" to reduce verbosity | |
jobs: | |
trigger-azure-pipeline: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Trigger Azure DevOps Pipeline | |
id: trigger | |
uses: Azure/[email protected] | |
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 in-progress 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=10&queryOrder=queueTimeDescending&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::Attempt $ATTEMPT/$MAX_ATTEMPTS to find in-progress build 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 | |
BUILD_ID=$(jq -r '.value[] | select(.status == "inProgress") | .id' response.json | head -n 1 || true) | |
if [ -n "$BUILD_ID" ]; then | |
echo "BUILD_ID=$BUILD_ID" >> $GITHUB_ENV | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Found in-progress BUILD_ID=$BUILD_ID" | |
fi | |
break | |
else | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::No in-progress build found this attempt." | |
fi | |
echo "No in-progress build found yet. Waiting $SLEEP_SECONDS seconds before retry..." | |
sleep $SLEEP_SECONDS | |
ATTEMPT=$((ATTEMPT+1)) | |
fi | |
done | |
if [ -z "$BUILD_ID" ]; then | |
echo "Error: No in-progress build 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" | |
TIMELINE_URL="https://dev.azure.com/${ORGANIZATION}/${PROJECT_NAME}/_apis/build/builds/${BUILD_ID}/timeline?api-version=6.0" | |
MAX_ATTEMPTS=100 | |
SLEEP_SECONDS=10 | |
ATTEMPT=1 | |
rm -f previous_timeline_state.json current_timeline_state.json || true | |
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 | |
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 | |
exit 1 | |
fi | |
echo "Current Status: $STATUS" | |
if [ "$DEBUG_MODE" = "true" ]; then | |
echo "::debug::Current Result: $RESULT" | |
fi | |
rm -f timeline.json || true | |
if ! wget --quiet --header="Authorization: Basic ${AUTH_HEADER}" -O timeline.json "$TIMELINE_URL"; then | |
echo "Error: Failed to fetch timeline." >&2 | |
exit 1 | |
fi | |
jq '[.records[] | {id: .id, name: .name, state: .state, result: .result, startTime: .startTime, finishTime: .finishTime, issues: .issues}]' timeline.json > current_timeline_state.json | |
if [ ! -f previous_timeline_state.json ]; then | |
echo "---- Pipeline Detailed Timeline (Initial) ----" | |
cat current_timeline_state.json | jq '.[]' | |
echo "---------------------------------------------" | |
else | |
CHANGES=$(jq -n --argfile old previous_timeline_state.json --argfile new current_timeline_state.json ' | |
[ | |
($new[] as $n | | |
($old[]? | select(.id==$n.id)) as $o | | |
if $o == null or $o.state != $n.state or $o.result != $n.result then $n else empty end | |
) | |
] | map(select(. != null)) | |
') | |
if [ "$(echo "$CHANGES" | jq 'length')" -gt 0 ]; then | |
echo "---- Pipeline Detailed Timeline (Changes) ----" | |
echo "$CHANGES" | jq '.[]' | |
echo "---------------------------------------------" | |
else | |
echo "No new timeline changes since last check." | |
fi | |
fi | |
cp current_timeline_state.json previous_timeline_state.json | |
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!" | |
rm -f previous_timeline_state.json current_timeline_state.json timeline.json status.json | |
- name: Merge the pull request | |
if: ${{ success() && github.event_name == 'pull_request_target' }} | |
uses: peter-evans/merge-pull-request@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
pull-request: ${{ github.event.pull_request.number }} | |
merge-method: merge | |