Azure build pipeline path filter to only include particular file extension - azure-devops

I'm working on creating build pipeline in Azure DevOps. I want to trigger it against master branch but only when commit has changes under src/Project/tds/Serialization.Master/ Project - this project contains only .item files
If the commit includes any other files together with .item then this pipeline shouldn't trigger, tried path exclude
BDD
Scenario 1
Given | I've changes for src/Project/tds/Serialization.Master/*
Then | build pipeline should trigger
Scenario 2
Given | I've changes for src/Project/tds/Serialization.Master/*
And | I've changes for src/Foundation/*
Then | build pipeline shouldn't trigger
Scenario 3
Given | I've changes for src/Foundation/*
Then | build pipeline shouldn't trigger
trigger:
branches:
include:
- master
paths:
include:
- src/Project/tds/Serialization.Master/*
exclude:
- src/Foundation/*

Azure build pipeline path filter to only include particular file extension
If want to use path filter to trigger the build for those particular file-extension files in the folder Serialization.Master, you could use following syntax:
trigger:
paths:
exclude:
- src/Foundation/*
include:
- src/Project/tds/Serialization.Master/*
You could check the document YAML schema reference for some more details.
Note: Do not forget the keyword paths.

Related

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.

How do I create a "copy" of a build with the number from another project pipeline

Because of the limitation of Azure Test Plans not being able to choose a build from a different project, I was wondering if it is possible to create a pipeline that would at least clone the build number from another project.
Here's the narrative:
There's a project ProjA with pipeline P1 that generates a build number using the following line
name: $(date:yyyyMMdd)$(rev:.r)
I want it such that:
Another project ProjB has a pipeline P1 which matches the name in ProjA gets triggered so that there's a build recorded whenever ProjA.P1 is successful and have the build recorded with the same name as the build run from ProjA.P1
UPDATE note I am looking specifically for ProjA.P1 and not whatever would've triggered ProjA.P1. The original accepted answer works for the simple case where ProjA.P1 is triggered from the ProjA.P1 pipeline.
However, if ProjA.P1 has triggers: none and uses resources.pipelines to trigger it's build it uses the build number of the referenced pipeline rather than ProjA.P1.
We can set a pipeline completion trigger to Trigger one pipeline after another. (ProjB.P1 is triggered when ProjA.P1 completes.)
We can get the trigger build name using the resource variable $(resources.pipeline.<Alias>.runName) in YAML. (It will retrieve the buildNumber of the pipeline ProjA.P1 in pipeline.) See Pipeline resource metadata as predefined variables for details.
Then using the UpdateBuildNumber logging command (##vso[build.updatebuildnumber]build number) to update the build number of the ProjB.P1.
ProjB.P1 YAML for your reference:
trigger: none
resources:
pipelines:
- pipeline: ProjA-Trigger # Any alias here
source: P1 # The real pipeline name (ProjA.P1 here)
project: ProjA # Project ProjA
trigger:
branches:
include:
- refs/heads/main
steps:
- task: Bash#3
inputs:
targetType: 'inline'
script: 'echo "##vso[build.updatebuildnumber]$(resources.pipeline.ProjA-Trigger.runName)"'

How to access pipeline identifier in azure devops resource triggered pipeline inside a template

I have Azure DevOps yaml deployment pipelines that trigger when a build pipeline completes and publishes an artifact. According to docs found here, artifacts will be downloaded to
to $(PIPELINE.WORKSPACE)/pipeline-identifier/artifact-identifier folder.
my trigger is similar to
resources:
pipelines:
- pipeline: SmartHotel-PipelineIdentifier
project: DevOpsProject
source: SmartHotel-CI
trigger:
branches:
include:
- main
How can i access the pipeline-identifier from a template? I need to be able to create
$(PIPELINE.WORKSPACE)/SmartHotel-PipelineIdentifier/artifact-identifier
based upon the pipeline definition above.
When the pipeline is triggered by a build, I'm able to use
$(Pipeline.Workspace)/$(resources.triggeringAlias)/artifact-identifier
to give me what I need, but that value is blank when the pipeline is triggered manually.
How can I access what appears to be called the pipeline-identifier value for a specific pipeline to be used within templates in the deployment jobs of pipelines?
You can get various properties of the pipeline resource, but that's all assuming you know the alias. But there's no way to get that alias if it's not triggered automatically. If you had more pipeline resources, how would you know which one to get?
In this case, you could:
pass the pipeline identifier (SmartHotel-PipelineIdentifier) as a parameter to the template.
assume a convention for well-known pipeline identifiers (i.e. always use artifact-pipeline instead of SmartHotel-PipelineIdentifier)
download (or move) the artifact to a well-known location before calling the template. That's assuming you already use the pipeline identifier for the download task anyway, so you could simply do mv $(Pipeline.Workspace)/SmartHotel-PipelineIdentifier/artifact-identifier $(Pipeline.Workspace)/artifact-identifier right after download.
You can also use the full DownloadPipelineArtifact task (which download is a shorthand for) and configure it to download to a different directory.
determine the directory name in runtime, once the artifact is downloaded and set a pipeline variable dynamically:
# there will be directories in $(Pipeline.Workspace) like "a", "b", "s" for checked out repositories - ignore them
- pwsh: |
ls $(Pipeline.Workspace)
$alias = Get-ChildItem $(Pipeline.Workspace) | ? { $_.Name.Length -gt 1 } | select -ExpandProperty Name -First 1
echo "##vso[task.setvariable variable=ArtifactAlias]$alias"
displayName: determine artifact pipeline alias
- pwsh: echo 'alias: $(ArtifactAlias)' # use $(ArtifactAlias) where you would use $(resources.triggeringAlias)

Azure DevOps : excluding updates to yaml pipeline files from triggering those same pipelines?

Somehow I'm not getting how I can exclude updates yaml pipelines from trigger the pipelines themselves. I have tried some wildcards as described here both in the include item (with the ! operator) or in the exclude item and nothing worked. I've added the plain pipeline names with no wildcards in the excluded item and again it did not work. The yaml files sit in the root of my repository (no containing folder).
These don't work
trigger:
branches:
include:
- MyBranch
paths:
exclude:
- "*.yml"
- "**/*.yml"
Clearly adding the full name of the pipeline works but you can't always edit exisitng yaml files each and every time you add a new one
If you don’t want to trigger the pipeline when updating azure-pipelines.yml file, you can set the following trigger:
trigger:
branches:
include:
- master
paths:
exclude:
- /azure-pipelines.yml
I'm confused, with the following syntax CI pipelines are not triggered when updating *.yml files
trigger:
branches:
include:
- MyBranch
paths:
include:
- '!**/*.yml'
while with the following one (which as I understand it should mean the same thing as the above one) pipelines keep being triggered when updating any *.yml file
trigger:
branches:
include:
- MyBranch
paths:
exclude:
- "**/*.yml"
What am I not getting?

Trigger azure pipeline based on multiple file changes

I'm trying to trigger an Azure DevOps Build pipeline based on two files being changed simultaneously on my master repository, with the following trigger:
trigger:
branches:
include:
- master
paths:
include:
- '**/*task.json'
- '**/vss-extension.json'
My folder structure for this repository is something like this:
repository:
|--run-stryker
--vss-extension.json
--other files here ...
|--task
--task.json
--other files here ...
Yet changes to these files at the same time are not triggering my pipeline. What am I doing wrong here?
Unfortunately using wildcards like this in CI triggers is not supported at this moment. You can use * only at the end of paths but this is working the same as without this.
Wildcards like this are working in file matching in tasks but not in path trigger. So you must use exact paths like:
trigger:
branches:
include:
- master
paths:
include:
- 'run-stryker/task/task.json'
- 'run-stryker/vss-extension.json'
or using only folder paths like:
trigger:
branches:
include:
- master
paths:
include:
- 'run-stryker/*' # is the same as 'run-stryker/'
Documentation reference: Wildcards in CI triggers