I have a pipeline which triggers the second one. I am trying to find a method to stop that first pipeline for a while until the second deployment pipeline completes.
Meanwhile I read about Azure Pipelines app in MS Teams, however it is external solutions (which still might be a good as well), but at first glance I am looking for solution built-in the pipelines itself. The goal is to get a notification about the status from the second pipeline. I am thinking for adding the variable:
- script: |
echo "##vso[task.setvariable variable=CICD_Status]succeeded"
But it doesn’t make sense. On one hand, the second pipeline cannot pass the execution status back to the first one in this way. On the other hand, having the first pipeline wait for the result of the second one also isn’t feasible because the second pipeline can only start after the first one finishes. Once the first pipeline finishes, it essentially "closes" and can’t receive any feedback about the execution of the second pipeline.
Update 1.
First pipeline which only monitor branches, and triggers the deployment
pipeline::
trigger:
branches:
include:
- development
- release
- bugs/*
steps:
- script: |
if [ -n "${SYSTEM_PULLREQUEST_PULLREQUESTID}" ]; then
echo "Pull Request detected:"
echo "- Source Branch: ${SYSTEM_PULLREQUEST_SOURCEBRANCH}"
echo "- Target Branch: ${SYSTEM_PULLREQUEST_TARGETBRANCH}"
echo "- PR Number: ${SYSTEM_PULLREQUEST_PULLREQUESTID}"
else
echo "Repository Name: $(Build.Repository.Name)"
echo "Regular build from branch $(Build.SourceBranch)"
fi
displayName: 'Pipeline Info'
First pipeline allows to push changes from the above branches, but for main branch it must be a PR, and on Azure DevOps it looks like that (example with PR, for branches
it shows the main of the branch in the output:
The goal is to have in that pipeline the another job below? Or stage? which shows the status of deployment
pipeline.
Now deployment
pipeline:
trigger: none
pr: none
resources:
pipelines:
- pipeline: APP
source: 'APP'
project: new
trigger: true
stages:
- stage: Validation
displayName: 'Branch Check'
jobs:
- job: Validate
displayName: 'Verifying Branch'
steps:
- script: |
echo "Triggering repository: $(resources.pipeline.APP.sourceBranch)"
- stage: APP_TEST_DEV
displayName: APP DEV || TEST DEV
dependsOn: Validation
condition: eq(variables['resources.pipeline.APP.sourceBranch'], 'refs/heads/development')
jobs:
- deployment: DeployToAPPDEV
displayName: Deploy to APP TEST DEV
environment:
name: APP_DEV_TEST
strategy:
runOnce:
deploy:
steps:
- template: /azure_templates/app_ansible.yml@CICD
parameters:
extraVars: >-
{
"git_branch": "development"
}
When branch from the first pipeline is passed to the deployment
pipeline it enables specific stage as shown in above code, when it completes the status of execution should be passed back to the first pipeline to inform developers that deployment
was finished with success/failed They do not have an access to the deployment
pipeline (different org/project/repo) so as I said at the beginning maybe I should think about notification on Teams if that approach is not possible?
we have two separate repositories in two different Azure DevOps organization/project/repository
Ok, in this case, we can try to explore other ways to complete what you need:
My general idea is to use Definitions - Update and Builds - Update Build rest API in the second pipeline to disable the first pipeline and update the status information of the second pipeline run on the run of the first pipeline.
Here are the detailed steps of my test pipeline for your reference:
name: $(rev:r)-$(Date:dd.MM.yyyy)
trigger:
branches:
include:
- main
# I add this to avoid the run name being too long
appendCommitMessageToRunName: false
steps:
- script: |
if [ -n "${SYSTEM_PULLREQUEST_PULLREQUESTID}" ]; then
echo "Pull Request detected:"
echo "- Source Branch: ${SYSTEM_PULLREQUEST_SOURCEBRANCH}"
echo "- Target Branch: ${SYSTEM_PULLREQUEST_TARGETBRANCH}"
echo "- PR Number: ${SYSTEM_PULLREQUEST_PULLREQUESTID}"
else
echo "Repository Name: $(Build.Repository.Name)"
echo "Regular build from branch $(Build.SourceBranch)"
fi
displayName: 'Pipeline Info'
name: $(rev:r)-$(Date:dd.MM.yyyy)
trigger: none
pr: none
resources:
pipelines:
# Trigger on successful APP pipeline completions
- pipeline: APP
source: 'APP'
project: new
trigger: true
stages:
- stage: Validation
displayName: 'Branch Check'
jobs:
- job: Validate
displayName: 'Verifying Branch'
steps:
- script: |
echo "Triggering repository: $(resources.pipeline.APP.sourceBranch)"
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$organization =""
$project=""
$personalAccessToken="$(pat)"
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
}
$runName = "$(resources.pipeline.app.runName)"
$buildNumber = $runName.Substring(0, 10)
# Get the build and update the build name
$buildurl = "https://dev.azure.com/$organization/$project/_apis/build/builds/$(resources.pipeline.app.runID)?api-version=7.2-preview.7"
$buildinfo= Invoke-RestMethod -Uri $buildurl -Method get -Headers $headers -ContentType "application/json"
$buildinfo.buildNumber ="$buildNumber" + "_The pipeline is disabled when the triggered deployment pipeline is running"
Invoke-RestMethod -Uri $buildurl -Method PATCH -Headers $headers -ContentType "application/json" -Body ($buildinfo | ConvertTo-Json -Depth 99)
# Get the pipeline definition and disable the pipeline
$definitionurl = "https://dev.azure.com/$organization/$project/_apis/build/definitions/$(resources.pipeline.app.pipelineID)?api-version=7.2-preview.7"
$definitioinfo= Invoke-RestMethod -Uri $definitionurl -Method get -Headers $headers -ContentType "application/json"
$definitioinfo.queueStatus = "disabled"
Invoke-RestMethod -Uri $definitionurl -Method put -Headers $headers -ContentType "application/json" -Body ($definitioinfo | ConvertTo-Json -Depth 99)
displayName: 'Disable Pipeline App and Rename the run '
- stage: APP_DEV
displayName: APP DEV main
dependsOn: Validation
condition: startsWith(variables['resources.pipeline.APP.sourceBranch'], 'refs/heads/main')
jobs:
- deployment: DeployDEV
displayName: DEV main
environment:
name: APP_DEV
strategy:
runOnce:
deploy:
steps:
- script: echo APP_DEV deployment running , it may success or fail.
- task: PowerShell@2
condition: always()
inputs:
targetType: 'inline'
script: |
$organization =""
$project=""
$personalAccessToken="$(pat)"
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
}
$runName = "$(resources.pipeline.app.runName)"
$buildNumber = $runName.Substring(0, 10)
# Get the build and update the build name
$buildurl = "https://dev.azure.com/$organization/$project/_apis/build/builds/$(resources.pipeline.app.runID)?api-version=7.2-preview.7"
$buildinfo= Invoke-RestMethod -Uri $buildurl -Method get -Headers $headers -ContentType "application/json"
# Modify the name and status based on JobStatus's status
if ("$(Agent.JobStatus)" -eq "Succeeded") {
$buildinfo.buildNumber = "$buildNumber"+"_The triggered deployment pipeline run successful"
} else {
$buildinfo.buildNumber = "$buildNumber"+"_The triggered deployment pipeline run failed"
}
Invoke-RestMethod -Uri $buildurl -Method PATCH -Headers $headers -ContentType "application/json" -Body ($buildinfo | ConvertTo-Json -Depth 99)
# Get the pipeline definition and enable the pipeline
$definitionurl = "https://dev.azure.com/$organization/$project/_apis/build/definitions/$(resources.pipeline.app.pipelineID)?api-version=7.2-preview.7"
$definitioinfo= Invoke-RestMethod -Uri $definitionurl -Method get -Headers $headers -ContentType "application/json"
$definitioinfo.queueStatus = "enabled"
Invoke-RestMethod -Uri $definitionurl -Method put -Headers $headers -ContentType "application/json" -Body ($definitioinfo | ConvertTo-Json -Depth 99)
displayName: 'Enable Pipeline APP and update status of the deployment'
Add a secret variable with your personal access token named PAT
in the second pipeline. (If you don't want to use PAT, we can use System.AccessToken instead and we need to make sure the build service account has the permission to update the definition and runs of the first pipeline. You can refer this document of Job authorization scope for more details.)
After the first Validation stage runs, the first pipeline is disabled and show the current status of the deployment pipeline:
After the APP_DEV stage finished the last PowerShell task, the first pipeline is enabled again and shows the success or fail status of the deployment pipeline: