Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users/svelderrain/ci cd #88

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 25 additions & 84 deletions .github/workflows/trigger-azure-pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,114 +1,55 @@
name: Trigger Azure DevOps Pipeline and Show Progress
name: PR Triggered Tests

on:
push:
branches: [ main, develop ]
workflow_dispatch:
inputs:
branch:
description: "Branch to run the pipeline against"
required: false
default: "main"
# Using pull_request_target so we can access secrets
pull_request_target:
types: [opened, synchronize, reopened]

jobs:
trigger-and-monitor-azure-pipeline:
trigger-azure-pipeline:
runs-on: ubuntu-latest
if: contains(github.event.pull_request.base.ref, 'develop') # optional: trigger only if PR is into main
steps:
- name: Determine Branch
id: determine_branch
- name: Check if authorized to run tests
id: check
run: |
# If triggered by workflow_dispatch, use input branch, otherwise use GITHUB_REF
if [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ]; then
BRANCH="${{ github.event.inputs.branch }}"
else
BRANCH="${GITHUB_REF##*/}"
fi
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
# Implement your logic here. For example, check PR labels using GitHub CLI:
# gh pr view ${{ github.event.pull_request.number }} --json labels
# if no label or unauthorized contributor, exit
# For simplicity, we just say we always trigger. In reality, add conditions.
echo "ok=true" >> $GITHUB_OUTPUT

- name: Trigger Azure DevOps Pipeline
id: trigger
if: steps.check.outputs.ok == 'true'
run: |
PAT="${{ secrets.AZURE_DEVOPS_PAT }}"
ORG="sergiovelderrain"
PROJECT="sergiovelderrain"
PIPELINE_ID="1"
API_VERSION="6.0-preview.1"

# Encode PAT for Basic Auth

AUTH=$(echo -n ":$PAT" | base64)

BRANCH_NAME="${{ steps.determine_branch.outputs.branch }}"
# We can specify a branch or a commit ref from the PR. Usually you'd run tests against the PR's branch.
# Since we're using pull_request_target, be careful with checking out code from PR.
# It's safer to just run pipeline against the PR's head commit reference retrieved from the event payload.
PR_HEAD_REF="${{ github.event.pull_request.head.ref }}"
REPO_NAME="${{ github.event.pull_request.head.repo.name }}"
# If your Azure DevOps pipeline is configured to use the same repo, specify it here:
# Ensure that your pipeline definition knows how to handle arbitrary refs.

JSON_BODY='{
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/'"${BRANCH_NAME}"'"
"refName": "refs/heads/'"${PR_HEAD_REF}"'"
}
}
}
}'

RESPONSE=$(curl -s -X POST \
curl -s -X POST \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json" \
-d "$JSON_BODY" \
"https://dev.azure.com/$ORG/$PROJECT/_apis/pipelines/$PIPELINE_ID/runs?api-version=$API_VERSION")

echo "$RESPONSE"

# Extract the runId from response
RUN_ID=$(echo "$RESPONSE" | jq -r '.id')

if [ "$RUN_ID" = "null" ] || [ -z "$RUN_ID" ]; then
echo "Failed to trigger pipeline. Response:"
echo "$RESPONSE"
exit 1
fi

echo "run_id=$RUN_ID" >> $GITHUB_OUTPUT

- name: Monitor Azure DevOps Pipeline
run: |
PAT="${{ secrets.AZURE_DEVOPS_PAT }}"
ORG="myorg"
PROJECT="myproject"
API_VERSION="6.0-preview.1"
RUN_ID="${{ steps.trigger.outputs.run_id }}"

AUTH=$(echo -n ":$PAT" | base64)

# Poll until completed or timeout
MAX_ATTEMPTS=30
SLEEP=20

ATTEMPT=0
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
RESPONSE=$(curl -s -X GET \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json" \
"https://dev.azure.com/$ORG/$PROJECT/_apis/pipelines/$RUN_ID?api-version=$API_VERSION")

STATUS=$(echo "$RESPONSE" | jq -r '.state')
RESULT=$(echo "$RESPONSE" | jq -r '.result')

echo "Pipeline run ID: $RUN_ID, Status: $STATUS, Result: $RESULT"

if [ "$STATUS" = "completed" ]; then
# Pipeline is done, check result
if [ "$RESULT" = "succeeded" ]; then
echo "Azure DevOps pipeline succeeded!"
exit 0
else
echo "Azure DevOps pipeline failed with result: $RESULT"
exit 1
fi
fi

# If not completed, wait and try again
ATTEMPT=$((ATTEMPT+1))
sleep $SLEEP
done

echo "Pipeline did not complete within the expected time."
exit 1
"https://dev.azure.com/$ORG/$PROJECT/_apis/pipelines/$PIPELINE_ID/runs?api-version=$API_VERSION"
21 changes: 21 additions & 0 deletions LICENSE - Copy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 NI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions New Text Document - Copy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s
1 change: 1 addition & 0 deletions New Text Document.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s
Loading