Skip to content

CI for Pull Requests #9

CI for Pull Requests

CI for Pull Requests #9

name: CI for Pull Requests
on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened] # Trigger the workflow on PR creation and updates
workflow_dispatch: # This allows you to manually trigger the workflow
permissions:
contents: read
jobs:
trigger-and-wait:
runs-on: ubuntu-latest
steps:
# Step 1: Trigger the Azure DevOps Pipeline
- name: Trigger Azure DevOps Pipeline
id: trigger-pipeline
run: |
response=$(curl -L -X POST \
-H "Authorization: Basic ${{ secrets.AZURE_DEVOPS_TOKEN }}" \
-d "{\"definition\": {\"id\": \"1548\"}, \"sourceBranch\": \"refs/pull/${{ github.event.pull_request.number }}/merge\"}" \
https://dev.azure.com/SqlClientDrivers/mssql-jdbc/_apis/build/builds?api-version=6.0)
echo "Response: $response"
# Extract the build ID from the response
build_id=$(echo "$response" | jq -r '.id')
echo "Triggered Build ID: $build_id"
# Set the build ID as an environment variable for later steps
echo "BUILD_ID=$build_id" >> $GITHUB_ENV
# Step 2: Poll the Azure DevOps Pipeline until it finishes
- name: Wait for Azure DevOps Pipeline to Finish
id: wait-for-pipeline
run: |
while true; do
# Poll the status of the pipeline using the build ID
status=$(curl -L -X GET \
-H "Authorization: Basic ${{ secrets.AZURE_DEVOPS_TOKEN }}" \
https://dev.azure.com/SqlClientDrivers/mssql-jdbc/_apis/build/builds/${{ env.BUILD_ID }}?api-version=6.0 \
| jq -r '.status')
# If the pipeline status is completed, break the loop
if [[ "$status" == "completed" ]]; then
echo "Pipeline completed."
break
fi
echo "Waiting for pipeline to finish..."
sleep 30 # Wait for 30 seconds before polling again
done
# Step 3: Get the final status of the pipeline and fail the GitHub action if failed
- name: Check Azure DevOps Pipeline Result
run: |
# Fetch the final result of the pipeline
final_result=$(curl -L -X GET \
-H "Authorization: Basic ${{ secrets.AZURE_DEVOPS_TOKEN }}" \
https://dev.azure.com/SqlClientDrivers/mssql-jdbc/_apis/build/builds/${{ env.BUILD_ID }}?api-version=6.0 \
| jq -r '.result')
echo "Pipeline final result: $final_result"
# If the result is not "succeeded", fail the workflow
if [[ "$final_result" != "succeeded" ]]; then
echo "Azure DevOps pipeline failed."
exit 1 # Fail the GitHub Action if the pipeline failed
fi