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

Moving trigger pipeline from git Action to ADO pipeline #20

Merged
merged 13 commits into from
Oct 22, 2024
94 changes: 0 additions & 94 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,98 +20,4 @@ jobs:

- name: Build the project
run: dotnet build TestStream.Runner/TestStream.Runner.csproj

- name: Trigger Azure DevOps Pipeline
env:
AZURE_DEVOPS_ORG: ${{ vars.AZURE_DEVOPS_ORG }}
AZURE_DEVOPS_PROJECT: nanoFramework.IoT.TestStream
AZURE_DEVOPS_PIPELINE_ID: 111
AZURE_POOL_NAME: ${{ vars.AZURE_POOL_NAME }}
GITHUB_HEAD_REF: ${{ github.head_ref }}
run: |
# Validate required environment variables
for var in AZURE_DEVOPS_ORG AZURE_DEVOPS_PROJECT AZURE_DEVOPS_PIPELINE_ID AZURE_POOL_NAME GITHUB_HEAD_REF; do
if [ -z "${!var}" ]; then
echo "Error: Required environment variable $var is not set"
exit 1
fi
done

# Define the Azure DevOps organization, project, and pipeline
organization="${AZURE_DEVOPS_ORG}"
project="${AZURE_DEVOPS_PROJECT}"
pipelineId="${AZURE_DEVOPS_PIPELINE_ID}"
poolName="${AZURE_POOL_NAME}"
branch="refs/heads/${GITHUB_HEAD_REF}"

# Encode the PAT
patEncoded=$(echo -n ":${{ secrets.AZURE_DEVOPS_PAT }}" | base64)

# Define the headers
headers=(
-H "Authorization: Basic $patEncoded"
-H "Content-Type: application/json"
)

# Get the pool ID
url="https://dev.azure.com/${organization}/_apis/distributedtask/pools?poolName=${poolName}&api-version=7.1"
AZP_POOL_AGENTS=$(curl -s "${headers[@]}" -X GET "$url")
poolId=$(echo "$AZP_POOL_AGENTS" | jq -r '.value[0].id')

echo "Pool ID: $poolId"

# Define the URL to get all agents in the pool
url="https://dev.azure.com/${organization}/_apis/distributedtask/pools/${poolId}/agents?includeCapabilities=true&api-version=7.1"

response=$(curl -s -w "%{http_code}" "${headers[@]}" -X GET "$url")
http_code=${response: -3}
content=${response::-3}

if [ $http_code -eq 200 ]; then
# Extract all userCapabilities names for online and enabled agents as a unique list
capabilityNames=$(echo "$content" | jq -r '[.value[] | select(.status == "online" and .enabled == true) | .userCapabilities | keys] | unique | flatten | join("\n- ")')
else
echo "Failed to retrieve agent capabilities. HTTP Status Code: $http_code"
echo "Response: \"$content\""
exit 1
fi
echo "Unique userCapabilities names: \"$capabilityNames\""

# Prepare the parameters
parametersJson=$(jq -n --arg appComponents "- $capabilityNames" '{templateParameters: {appComponents: $appComponents}}')

echo "Parameters: \"$parametersJson\""
echo "Branch for PR: \"$branch\""

# Define the request body
bodyJson=$(jq -n --argjson parameters "$parametersJson" --arg branch "$branch" '{
resources: {
repositories:
{
self: {
refName: $branch
}
}
},
templateParameters: $parameters.templateParameters
}')

echo "Request body: \"$bodyJson\""

# Define the URL
url="https://dev.azure.com/${organization}/${project}/_apis/pipelines/${pipelineId}/runs?api-version=7.1"

# Trigger the pipeline
response=$(curl -s -w "%{http_code}" "${headers[@]}" -X POST -d "$bodyJson" "$url")
http_code=${response: -3}
content=${response::-3}

if [ $http_code -eq 200 ]; then
run_id=$(echo "$content" | jq -r '.id')
echo "Pipeline triggered successfully. Run ID: $run_id"
else
echo "Failed to trigger pipeline. HTTP Status Code: $http_code"
echo "Response: $content"
exit 1
fi

169 changes: 169 additions & 0 deletions azure-boostrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Copyright (c) .NET Foundation and Contributors
# See LICENSE file in the project root for full license information.

trigger:
branches:
include:
- main
- develop
- release-*
paths:
exclude:
- .github_changelog_generator
- .gitignore
- CHANGELOG.md
- CODE_OF_CONDUCT.md
- LICENSE.md
- README.md
- NuGet.Config
- assets/*
- config/*
- .github/*

# PR always trigger build
pr:
autoCancel: true

jobs:
- job: Trigger
displayName: Trigger Azure Dev Ops build and test pipeline
pool:
vmImage: 'ubuntu-latest'

variables:
AZURE_DEVOPS_ORG: nanoFramework
AZURE_DEVOPS_PROJECT: nanoFramework.IoT.TestStream
AZURE_DEVOPS_PIPELINE_ID: 111
AZURE_POOL_NAME: TestStream

steps:
- script: |
# Validate required environment variables
for var in AZURE_DEVOPS_ORG AZURE_DEVOPS_PROJECT AZURE_DEVOPS_PIPELINE_ID AZURE_POOL_NAME; do
if [ -z "${!var}" ]; then
echo "Error: Required environment variable $var is not set"
exit 1
fi
done

# Define the Azure DevOps organization, project, and pipeline
organization="${AZURE_DEVOPS_ORG}"
project="${AZURE_DEVOPS_PROJECT}"
pipelineId="${AZURE_DEVOPS_PIPELINE_ID}"
poolName="${AZURE_POOL_NAME}"
branch="${BUILD_SOURCEBRANCH}"

# Encode the PAT
patEncoded=$(echo -n ":${AZURE_DEVOPS_PAT}" | base64)

# Define the headers
headers=(
-H "Authorization: Basic $patEncoded"
-H "Content-Type: application/json"
)

# Get the pool ID
url="https://dev.azure.com/${organization}/_apis/distributedtask/pools?poolName=${poolName}&api-version=7.1"
AZP_POOL_AGENTS=$(curl -s "${headers[@]}" -X GET "$url")
poolId=$(echo "$AZP_POOL_AGENTS" | jq -r '.value[0].id')

echo "Pool ID: $poolId"

# Define the URL to get all agents in the pool
url="https://dev.azure.com/${organization}/_apis/distributedtask/pools/${poolId}/agents?includeCapabilities=true&api-version=7.1"

response=$(curl -s -w "%{http_code}" "${headers[@]}" -X GET "$url")
http_code=${response: -3}
content=${response::-3}

if [ $http_code -eq 200 ]; then
# Extract all userCapabilities names for online and enabled agents as a unique list
capabilityNames=$(echo "$content" | jq -r '[.value[] | select(.status == "online" and .enabled == true) | .userCapabilities | keys] | unique | flatten | join("\n- ")')
else
echo "Failed to retrieve agent capabilities. HTTP Status Code: $http_code"
echo "Response: \"$content\""
exit 1
fi
echo "Unique userCapabilities names: \"$capabilityNames\""

# Prepare the parameters
parametersJson=$(jq -n --arg appComponents "- $capabilityNames" '{templateParameters: {appComponents: $appComponents}}')

echo "Parameters: \"$parametersJson\""
echo "Branch for PR: \"$branch\""

# Define the request body
bodyJson=$(jq -n --argjson parameters "$parametersJson" --arg branch "$branch" '{
resources: {
repositories:
{
self: {
refName: $branch
}
}
},
templateParameters: $parameters.templateParameters
}')

echo "Request body: \"$bodyJson\""

# Define the URL
url="https://dev.azure.com/${organization}/${project}/_apis/pipelines/${pipelineId}/runs?api-version=7.1"

# Trigger the pipeline
response=$(curl -s -w "%{http_code}" "${headers[@]}" -X POST -d "$bodyJson" "$url")
http_code=${response: -3}
content=${response::-3}

if [ $http_code -eq 200 ]; then
run_id=$(echo "$content" | jq -r '.id')
echo "Pipeline triggered successfully. Run ID: $run_id"
echo "##vso[task.setvariable variable=run_id]$run_id"
else
echo "Failed to trigger pipeline. HTTP Status Code: $http_code"
echo "Response: $content"
exit 1
fi
displayName: 'Trigger Azure DevOps Pipeline'
env:
BUILD_SOURCEBRANCH: $(Build.SourceBranch)
AZURE_DEVOPS_PAT: $(AZURE_DEVOPS_PAT)

- script: |
echo "Pipeline to monitor Run ID: $(run_id)"
# Define the URL to get the pipeline run status
url="https://dev.azure.com/${AZURE_DEVOPS_ORG}/${AZURE_DEVOPS_PROJECT}/_apis/pipelines/${AZURE_DEVOPS_PIPELINE_ID}/runs/$(run_id)?api-version=7.1"

# Loop to monitor the pipeline run status
while true; do
response=$(curl -s -w "%{http_code}" -H "Authorization: Basic $(echo -n ":${AZURE_DEVOPS_PAT}" | base64)" -X GET "$url")
http_code=${response: -3}
content=${response::-3}

if [ $http_code -eq 200 ]; then
state=$(echo "$content" | jq -r '.state')
result=$(echo "$content" | jq -r '.result')

echo "Pipeline run state: $state"

if [ "$state" == "completed" ]; then
echo "Pipeline run completed with result: $result"
if [ "$result" == "succeeded" ]; then
exit 0
else
exit 1
fi
fi
else
echo "Failed to get pipeline run status. HTTP Status Code: $http_code"
echo "Response: $content"
exit 1
fi

# Wait for a while before checking again
sleep 30
done
displayName: 'Monitoring Azure DevOps pipeline'
env:
run_id: $(run_id)
AZURE_DEVOPS_PAT: $(AZURE_DEVOPS_PAT)
Loading
Loading