generated from ni/github-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
129 lines (110 loc) · 4.83 KB
/
trigger-azure-pipeline.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: PR Triggered Tests
on:
pull_request_target:
types: [opened, synchronize, reopened]
workflow_dispatch:
permissions:
contents: read
checks: write # Required to create/update check runs
jobs:
trigger-and-check-pipeline:
runs-on: ubuntu-latest
steps:
- name: Check if authorized to run tests
id: check
run: |
# Add conditions if needed. For now, always allow.
echo "ok=true" >> $GITHUB_OUTPUT
- name: Create GitHub Check Run (Pipeline Status)
if: steps.check.outputs.ok == 'true'
id: create_check
uses: actions/github-script@v1
with:
script: |
const { data } = await github.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: "Azure DevOps Pipeline",
head_sha: context.payload.pull_request ? context.payload.pull_request.head.sha : context.sha,
status: "in_progress",
started_at: new Date().toISOString()
});
core.setOutput("check_run_id", data.id);
- name: Trigger Azure DevOps Pipeline
if: steps.check.outputs.ok == 'true'
uses: Azure/pipelines@v1
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_TOKEN }}
azure-pipeline-variables: '{"variable1": "value1", "variable2": "value2"}'
id: trigger_pipeline
- name: Poll Azure DevOps Pipeline
if: steps.check.outputs.ok == 'true'
run: |
PAT="${{ secrets.AZURE_DEVOPS_TOKEN }}"
ORG="sergiovelderrain"
PROJECT="sergiovelderrain"
API_VERSION="7.0"
# The `Azure/pipelines` action does not currently output a run ID directly.
# We must retrieve the run ID from the triggered pipeline response if available.
# If no direct output is provided, consider adding a step in Azure DevOps pipeline
# that sets a variable or check if the action returns a pipeline run URL/ID.
# Placeholder: If the action does not provide a run ID, you'd need to modify
# the pipeline or method of triggering to get the run ID. For demonstration,
# assume the run ID is obtained from an environment variable or known location.
# Replace this with actual logic to retrieve the run ID.
RUN_ID_FILE="pipeline_run_id.txt"
if [ ! -f "$RUN_ID_FILE" ]; then
echo "Could not find pipeline run ID file. Modify this step to retrieve the run ID from Azure DevOps."
exit 1
fi
RUN_ID=$(cat $RUN_ID_FILE)
AUTH=$(echo -n ":$PAT" | base64 | tr -d '\n')
# Poll every 15 seconds for up to 30 minutes (120 polls)
for i in {1..120}; do
STATUS_RESPONSE=$(curl --fail --http1.1 -s \
-H "Authorization: Basic $AUTH" \
"https://dev.azure.com/$ORG/$PROJECT/_apis/pipelines/runs/$RUN_ID?api-version=$API_VERSION" || true)
if ! echo "$STATUS_RESPONSE" | grep -q '^{'; then
echo "The response is not JSON. Here is the raw response:"
echo "$STATUS_RESPONSE"
exit 1
fi
STATE=$(echo "$STATUS_RESPONSE" | jq -r '.state')
RESULT=$(echo "$STATUS_RESPONSE" | jq -r '.result')
echo "Pipeline state: $STATE"
echo "Pipeline result: $RESULT"
if [ "$STATE" = "completed" ]; then
echo "pipeline_result=$RESULT" >> $GITHUB_OUTPUT
exit 0
fi
echo "Pipeline not completed yet. Waiting..."
sleep 15
done
echo "Pipeline did not complete within the expected time."
exit 1
id: poll_pipeline
- name: Update GitHub Check Run with Pipeline Status
if: steps.check.outputs.ok == 'true'
uses: actions/github-script@v1
with:
script: |
const check_run_id = ${{ steps.create_check.outputs.check_run_id }};
const pipelineResult = ${{ steps.poll_pipeline.outputs.pipeline_result }};
let conclusion = 'failure';
if (pipelineResult === 'succeeded') {
conclusion = 'success';
}
await github.checks.update({
owner: context.repo.owner,
repo: context.repo.repo,
check_run_id,
completed_at: new Date().toISOString(),
status: 'completed',
conclusion: conclusion,
output: {
title: 'Azure Pipeline Result',
summary: `The Azure DevOps pipeline finished with status: ${pipelineResult}`
}
});