triggering one pipeline from another with particular branch - azure-devops

I have ABC & DEF pipelines and I want DEF to run post ABC successful run so I have written YAML like below in DEF.
pipelines:
- pipeline: test # Internal name of the source pipeline
source: ABC
trigger: true
Pipeline triggering but I want to trigger pipeline for branches under release/* latest branch. The pipeline triggering for master in DEF. ABC is just cron JOB. is there any way to trigger DEF for branch we want instead of master.

You can try to modify the following yaml to have a try:
trigger:
- DEF
resources:
pipelines:
- pipeline: test
project: your_project_name
source: ABC
branch: release/* #latest branch
trigger:
branches:
- release/* #latest branch

Related

Azure Resource Pipeline trigger not working

I have a question on the pipeline resource trigger and I have 3 branches in the single repo in the project.
Repo name: test-repo
Branch name's:
- vishnu/branch1
- branch1.yaml
- vishnu/branch2
- branch2.yaml
- vishnu/branch3
- child.yaml
Pipelines created for all the 3 pipelines are named as follows:
poc_branch1 (branch1.yaml)
poc_branch2 (branch2.yaml)
poc_child (child.yaml)
The child.yaml contains the resource trigger like below:
resources:
pipelines:
- pipeline: poc_branch1
source: poc_branch1 # name of the pipeline that produces an artifact.
trigger:
branches:
include:
- refs/heads/vishnu/branch1
- pipeline: poc_branch2
source: poc_branch2 # name of the pipeline that produces an artifact.
trigger:
branches:
include:
- refs/heads/vishnu/branch2
# -------------------------------------------------------------------------
trigger: none
pr: none
stages:
- stage: Consume_Artifact
displayName: Consume Artifact From Main Pipeline
jobs:
- job: Consume
displayName: Consume
steps:
- task: PowerShell#2
displayName: 'Extracting the Triggered Pipeline alias'
inputs:
targetType: inline
script: |
Write-output "This pipeline has been triggered by: $(Resources.TriggeringAlias)"
Ideally, poc_child pipeline should be triggered once the poc_branch1 or poc_branch2 pipelines get completed. But it's not happening.
What might be a reason?
If I keep child.yaml in poc_branch1 where branch1.yaml file exists. And point the child.yaml to poc_child pipeline, and the trigger works.
Appreciate the input.
What might be reason?
The reason is when the two pipelines are in the same repository, the triggered pipeline version in the same branch as the triggering pipeline is run. That means, when poc_branch1 completed based on branch1, poc_child will be triggered and run based on branch1 as well. In your scenario, child.yaml only stores in branch3 but don't have other child.yaml versions in branch1 or branch2. Thus, poc_child can't run based on branch1 and branch2 even if branch1 and branch2 triggered poc_child. You could refer to Branch considerations for details.
If I keep child.yaml in poc_branch1 where branch1.yaml file is exists.
And point the child.yaml to poc_child pipeline, the trigger works.
Not clear about it. If you keep child.yaml in both branch1 and branch3 and the completed pipeline met the resource trigger filter, then, it should be expected. Please share more information about the details.

How to combine trigger filters in azure pipeline?

I have a CD pipeline, which uses a CI pipeline as a resource. In my CD pipeline, I have triggers that exclude some project paths that I have in the solution, in my resource I have included my main branch on the trigger.
CD pipeline:
trigger:
branches:
include:
- main
paths:
exclude:
- src/Customer.Worker.Converter
pool:
vmImage: "ubuntu-latest"
resources:
pipelines:
- pipeline: customer-ci
source: Customer-API-CI
trigger:
branches:
include:
- main
stages:
...
What I want to happen is when I push from my main branch, the CI pipeline runs and activates the CD pipeline only when what I changed is not in the paths I excluded from the trigger, in this case, when I change something in the Customer.Worker.Converter project, I don't want this pipeline to be triggered.
But even when I change something from path Customer.Worker.Converter, the CD pipeline triggers after the CI pipeline, ignoring my exclude path trigger and apparently obeying the CI resource pipeline trigger I use.
Is there any way to get what I want using pipeline resource?
As per this article, you've defined two separate triggers, but I think you only want one when the CI pipeline completes. At the moment, you will trigger changes based on the CI pipeline and when there are code-pushes.
If you only want the CD pipeline to be triggered when the CI pipeline completes, change the trigger for the CD pipeline to be none:
# ci pipeline
trigger:
branches:
include:
- main
paths:
include:
- src/Customer.Worker.Converter/*
----
# cd pipeline
resources:
pipelines:
- pipeline: customer-ci
source: customer-api-ci
trigger:
branches:
include:
- main # only trigger the CD pipeline when the CI pipeline on 'main' completes.
# do not trigger changes to this pipeline when code-changes are made
trigger: none
In theory, you can combine resource triggers:
When filters are specified, the source pipeline run must match all of the filters to trigger a run.
So one possibility would be to configure the cd pipeline to only run when the CI pipeline completes on the main branch and has a specific tag.

to Trigger one pipeline after another and also triggered by every changes

I have 2 pipelines. i need B pipeline to run after A pipeline completes and I also need B pipeline to triggers by Every test branch changes. I am not sure if using resources and trigger at the same file (B pipeline) is correct?
trigger:
batch: true
branches:
include:
- test
resources:
pipelines:
- pipeline: A
source: A
trigger: true
Test the scenario, it works as expected.
If that doesn't work, then please try to add the pipeline yaml file to Test branch, then create the pipeline B from Existing Azure Pipelines YAML file.
trigger:
batch: true
branches:
include:
- Test
resources:
pipelines:
- pipeline: A # Any Alias
source: A # The real pipeline name
project: Basic # Project name if from another project within the same org
trigger:
branches:
include:
- refs/heads/master

Azure Devops multiple triggers condition in a single file

Is it possible to have a single file azure-pipelines.yaml that can :
Trigger a job A on a push from any branch BUT main
Trigger a job B on PR to main and all subsequent commit on that PR
Trigger a job C when main is merged
I have tried to play arround with trigger, pr keywords and even with condition(), variables['Build.Reason'], or System.PullRequest.TargetBranch but I didn't manage to reach the expected result.
I start thinking it cannot be done with a single file - am I wrong?
You can set conditions on your stages to run depending on a variable but I am not pretty sure this will work with your conditions. Maybe you could also combine some variable values.
For example source branch main and pr is created.
and(eq(variables['Build.Reason'], 'PullRequest'), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
Azure documentation sample:
variables:
isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
stages:
- stage: A
jobs:
- job: A1
steps:
- script: echo Hello Stage A!
- stage: B
condition: and(succeeded(), eq(variables.isMain, 'true'))
jobs:
- job: B1
steps:
- script: echo Hello Stage B!
- script: echo $(isMain)
Keep in mind that triggers are appending resources. This means that if you specify a trigger like the below, it will be triggered whether the branch filter is triggered OR the pr is created.
trigger:
branches:
include:
- '*'
pr:
branches:
include:
- current
As you said this can be accomplished for sure with separate files for the pipelines.
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml
Your first question is possible by using trigger with include and exclude branches as below
trigger:
branches:
include:
- stage
- releases/*
exclude:
- master
Refer CI triggers in Azure Repos Git documentation to have more understanding.

Azure DevOps yaml pipeline trigger

My pipeline trigger in Azure DevOps does not fire.
Enviroment:
PipelineA (branch dev)
PipelineB (branch dev)
PipelineB should be fired if PipelineB was running successfully
Here is my current code of PipelineB.yaml
trigger: none
resources:
pipelines:
- pipeline: build_pipeline
source: PipelineA
branch: dev
trigger:
branches:
- dev
steps:
- task: Bash#3
inputs:
targetType: 'inline'
script: |
echo 'Hello world'
It worked in the past, but suddenly it stopped
Here's the document of Pipeline Triggers, please check this statement:
However, if the two pipelines use different repositories, then the triggered pipeline will use the latest version of the code from its default branch.
Cause of your issue:
The triggered pipeline is PipelineB in your scenario, and the default branch is always the master. Since the pipeline trigger for dev branch of PipelineA is defined in your dev branch instead of the default master branch, it's expected behavior that the pipeline trigger won't fire.
To make the pipeline trigger work:
1.You can choose to define the pipeline trigger for dev of PipelineA in default master of PipelineB.
2.Or you can change the Default branch for manual and scheduled builds of PipelineB (Change it from master to dev). You can find detailed steps about how to find this setting from #2 of this answer.
Both two choices above can resolve your issue and make the pipeline trigger work.
I just created almost identical pipeline
trigger: none
resources:
pipelines:
- pipeline: build_pipeline
source: kmadof.devops-manual (14)
branch: master
trigger:
branches:
- master
steps:
- task: Bash#3
inputs:
targetType: 'inline'
script: |
echo 'Hello world'
and all works fine. Are you sure you run PipelineA on dev branch?