Skip to content

CI for Pull Requests #15

CI for Pull Requests

CI for Pull Requests #15

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:
- name: Set up GitHub environment variables
run: |
# Extract the PR number from the GitHub reference (GITHUB_REF)
pr_id=$(echo $GITHUB_REF | awk -F'/' '{print $3}')
echo "PR_ID=$pr_id" >> $GITHUB_ENV
# Step 1: Trigger the Azure DevOps Pipeline
- name: Trigger Azure DevOps Pipeline
id: trigger-pipeline
run: |
response=$(curl -u :${{ secrets.AZURE_DEVOPS_TOKEN }} \
-X POST \
-H "Content-Type: application/json" \
-d "{\"definition\": {\"id\": \"1548\"}, \"sourceBranch\": \"refs/pull/$PR_ID/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 -u :${{ secrets.AZURE_DEVOPS_TOKEN }} \
-X GET \
-H "Content-Type: application/json" \
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 -u :${{ secrets.AZURE_DEVOPS_TOKEN }} \
-X GET \
-H "Content-Type: application/json" \
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