Azure DevOps : how to disable CI trigger on a YAML template pipeline? - azure-devops

In template pipelines you can't place any trigger statement such as trigger: none as specified in Microsoft's docs to disable ci trigger, so I wonder how do you prevent these pipelines from being executed every time you update them or any other yaml file in the same branch?

The pipeline yaml definition now supports disabling all trigers with
trigger: none
Reference

So in the end in a template pipeline you can't state something like trigger: none (to set only manual triggering) and you cannot specify stages or jobs, only steps are allowed (so you can't define any condition to prevent pipeline execution on the job or on the stage).
You have an option to disable the CI trigger by going in the triggers section for the template pipeline and select the following:
I don't like much this option because it means having pipeline configuration that is not captured in the yaml pipeline definition but I found no other way to disable the template pipeline from being triggered every time something (including the pipeline itself) is updated in its branch.

Other answers are fine.
Here is another approach I found. I have a yaml based build pipeline. I did not want to touch the yaml just for disabling the trigger. So I disabled the pipeline as follows.
Pick your pipeline and double click to edit it.
Go to the settings.
Now you should see an option to disable. Note here we are disabling the pipeline, not just disabling trigger to the pipeline.

First you need to go to your Pipelines dashboard, then select the pipeline that you want to disable CI for it
then Click on Edit
You will be redirected to the pipeline yaml file, from there you will click on the vertical ellipsis ⋮ => Triggers
and here you go, you can disable CI for your pipeline that easily
Save it, and you are done.

If you want to update your template without affecting pipelines which uses this template make a change on separate branch and merge it to master (or whatever you use) once you are sure that you have what you want.
The same applies if you use template from different repo:
# Repo: Contoso/WindowsProduct
# File: azure-pipelines.yml
resources:
repositories:
- repository: templates
type: github
name: Contoso/BuildTemplates
ref: refs/tags/v1.0 # optional ref to pin to
jobs:
- template: common.yml#templates # Template reference
parameters:
vmImage: 'vs2017-win2016'
By default you use template from your main branch, but you can point to any branch you want. Thus you can also test that your changes on limited pipelines as you need to point directly to a branch where you modified your template.
Let's say you have that branching:
master
|_ feature/add-extra-step
And you make a change in template but on feature/add-extra-step by adding additional step.
Now hen you trigger pipeline which uses that template:
trigger goes from master - your additional step in template won't be run
trigger goes from feature/add-extra-step - your additional step will be run
I made a change in template on feature/extra-step branch:
And this is change is not avialable when I run pipeline (even the same pipeline) on master:
If you for instance don't want to trigger ci build making changes on template, pleace commit those changes with phrase [skip ci] in th git message. Check here for more details.

Related

Why is my Devops pipleline being triggered by the completion of another pipeline, despite me replacing the pipeline trigger with a schedule only

I have a pipeline that is being automatically triggered upon completion of another pipline despite me removing the code from the YAML that should be responsible for this.
Taking it back to the beginning: I have created 3 yaml Pipelines. Originally Pipeline 1 was configured for a Scheduled Trigger only. Pipeline 2 was triggered by completion of Pipeline 1. Pipeline 3 was triggered by completion of Pipeline 2. These pipeline triggers were defined in the yaml. Below is an example of the relevant snippet from Pipeline 3's "original" yaml
trigger: none
resources:
pipelines:
- pipeline: Pipeline2 .
source: Pipeline2
trigger:
branches:
include:
- integration
However I then decided that I did not want to do this, and I removed the above code block and replaced it with the below:
trigger: none
schedules:
- cron: "35 1 * * 1-5"
displayName: Daily Schedule
branches:
include:
- main
always: true
Below shows that no triggers have been configured via the GUI
However Pipeline 3 is still being triggered by completion of Pipeline 2....Can someone please help me understand why my Pipeline is triggered by anything other than the schedule?
Below is a screenshot of the build summary
I have resolved this. It appears to have been a bug. When I updated my YAML to remove the pipeline trigger block and merged it across all the branches, Devops appears not to have updated my pipeline to remove this trigger.
To fix the problem I simply deleted the pipeline and recreated it, pointing at exactly the same YAML file and setting exactly the same default branch. I did nothing else and now my pipeline is no longer being triggered by another pipeline

Can I use one AzureDevOps pipeline to run other pipelines?

I would like to have a master pipeline capable of running the pipelines of our system's individual components. I'd also like to be able to run any of those components' pipelines individually. Additionally, some of the component pipelines are configured using yaml, while others are using the classic approach. (I'm not sure if that figures into any possible solutions to this problem.) Those that are configured using yaml typically contain multiple jobs, and I'd need all of the jobs to run in those cases.
Using approach #2 recommended here, I tried the following:
jobs:
- job: build_and_deploy
displayName: Build and Deploy
cancelTimeoutInMinutes: 1
pool:
name: some-pool
steps:
- checkout: self
- template: component_one_pipeline.yml
- template: component_two_pipeline.yml
I receive an error for the following "unexpected values": trigger, resources, name, variables, and jobs. I'm guessing these aren't allowed in any yaml file referenced in the template step of another pipeline yaml file. As I mentioned above, though, I need these values in their files because we need to run the pipelines individually.
If possible, could someone point me in the direction of how to get this done?
EDIT: I have also tried the approach given here. I was thinking I'd have a master pipeline that essentially did nothing except serve as a trigger for all of the child pipelines that are supposed to run sequentially. Essentially, the child pipelines should subscribe to the master pipeline and run when it's done. I ended up with the following 2 files:
# master-pipeline.yml
trigger: none
pool:
name: some agent pool
steps:
- script: echo Running MASTER PIPELINE
displayName: 'Run master pipeline'
#child-pipeline.yml
trigger: none
#- testing-branch (tried these combinations trying to pick up master run)
#- main
pool:
name: some agent pool
resources:
pipelines:
- pipeline: testing_master_pipeline
source: TestingMasterPipeline
trigger: true
steps:
- script: echo Running CHILD PIPELINE 1
displayName: 'Run Child Pipeline 1'
Unfortunately, it's not working. I don't get any exceptions, but the child pipeline isn't running when I manually run the master pipeline. Any thoughts?
Thanks in advance.
The way that those approaches you linked work, and Azure DevOps build triggering works in general, is that a build completion can trigger another build, and you have to have the trigger in build to be triggered. So:
Yaml templates can't have things like triggers, so they won't really help you here (though you can of course split any of the individual pipelines to templates). Triggers are in the main yaml pipeline fail, which references the template-files. So you can't have a individual component pipelines as templates.
Yaml pipelines can be chained with the resources-declaration mentioned in the first link. The way this works is that the resource declaration is in the pipeline to be triggered, and you configure the conditions (like branch filters: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/pipeline-triggers?view=azure-devops#branch-filters) to the pipeline to be triggered. For example, in your component pipeline you declare the master pipeline as resource, and set the conditions when the component pipeline will be triggered, like when the master pipeline is run against /release/* -branch. Or just set the trigger to true in order to trigger the component pipeline from any succesful run of the master pipeline. The component pipeline can still have its own pipeline triggers at the start of the pipeline declaration.
The classic build definitions can also be chained via edit build definition -> triggers -> build completion (see, for example, here: https://jpearson.blog/2019/03/27/chaining-builds-in-azure-devops/). This works the same way as with yaml pipelines; you configure the conditions for this the classic pipeline to trigger, so add the master pipeline as trigger to the component pipelines. Again, you can also set pipeline triggers for the component pipeline.
The limitation here is, that a classic pipeline can be triggered by an yaml pipeline, but not vice versa. A similar limitation in the yaml resources-declaration; they can't be triggered by a classic pipeline. If you need such triggering, or otherwise find the "native" triggers not to be enough, you can of course shoot an Azure DevOps API call in either type of pipeline to trigger any pipeline. See: https://blog.geralexgr.com/cloud/trigger-azure-devops-build-pipelines-using-rest-api, or just search for the azure devops rest api and associated blog posts that trigger the api with powershell, the rest api -task or by some other means.
As it turns out, I needed to set the pipelines' default branch to the one I was testing on for things to work correctly. The code in my original post works fine.
If you run into this problem and you're using a testing branch to test your pipelines, check out the information here on how to configure your pipeline to listen for triggers on your branch. Apparently a pipeline only listens for them on its default branch. Note: The example in the link uses the "classic" approach to pipeline configuration as an example, but you can reach the same page from a yaml configuration's edit screen by clicking the 3 dots on the right and selecting "Triggers."
Hope this helps someone.

Multi job pipeline always checks out same commit?

I have defined a multi job azure pipeline where every job needs to clone and checkout the source git repository. Some of these jobs can take a while so I am wondering if every job always clones and checks out the momentary HEAD version/commit of the Git branch or the pipeline trigger commit is remembered by the pipeline and then used for every job in order to have a consistent pipeline run.
I am pretty sure resp. can only hope the 2nd to be the case (and never saw anything else) but I am wondering if someone can point me to some Azure docs that officially confirm it.
When a pipeline is queued the ref is set. All jobs in the pipeline will checkout that reference. It's one of the reasons why the agent checks out the ref specifically and leaves the repo in detached HEAD state.
I can't find a doc that explicitly explains this, but that's how it works.
In a build pipeline (both YAML and Classic), by default each job in the pipeline will check out the commit version which triggered the current pipeline run.
In YAML pipeline, if you do not want a job to check out the commit ref, you can add the following step as the first one in the job.
steps:
- checkout: none
. . .
Both #Jesse and #Bright are right
There's a pipeline decorator that is evaluated on the first stage that looks for the presence of the checkout task. If the task is not found, it is dynamically inserted as the first step in the job.
You can see the pipeline decorator if you look at the top-level logs of the stage and expand the Job preparation parameters:
By specifying the checkout task with different settings, you can prevent this task from being injected:
steps:
- checkout: none

Azure YAML Pipelines: Is it possible to find out which pipeline triggered a build?

I have two repos on my Azure DevOps project. One for the Cloud Infrastructure deployment and another that contains my application code.
I have a YAML pipeline that is triggered after any of those repos build pipeline finishes. The pipeline looks a bit like this like this:
resources:
pipelines:
- pipeline: MyProject-Code
- pipeline: MyProject-Infrastructure
jobs:
- job: DeployInfrastructure
steps:
# Here are the tasks the deploy the project infrastructure
- job: DeployCode
steps:
# Here are the tasks that deploy the code
I would like to put a condition on the DeployInfrastructure job so it is only executed if the triggering pipeline is the infrastructure one as I do not need to redeploy it if the change only affects the application code.
However, when reading the documentation from Microsoft there does not seem to be a very straightforward way of doing this.
Have a look at Pipeline resource variables
In each run, the metadata for a pipeline resource is available to all
jobs in the form of predefined variables. The is the
identifier that you gave for your pipeline resource. Pipeline
resources variables are only available at runtime.
There are also a number of predefined variables called Build.TriggeredBy.*, amongst them Build.TriggeredBy.DefinitionName, however documentation suggests that for yaml pipeline with pipeline triggers the resource variables should be used instead
If the build was triggered by another build, then this variable is set
to the name of the triggering build pipeline. In Classic pipelines,
this variable is triggered by a build completion trigger.
This variable is agent-scoped, and can be used as an environment
variable in a script and as a parameter in a build task, but not as
part of the build number or as a version control tag.
If you are triggering a YAML pipeline using resources, you should use
the resources variables instead.

Multi branch Pipeline in Azure Devops

I am trying to build a YAML release pipeline in Azure DevOps
for that I have created multiple branches to keep environment-specific files
I created 4 pipelines for release as well:
Problem: Whenever I am making any changes in any branch, all the branch pipeline starts running. If I run an individual pipeline it works fine.
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- acc
pool:
name: 'Agent'
steps:
- task: Kubernetes#1
displayName: 'Deploy on K8s Cluster'
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'vs-aks-sc'
azureResourceGroup: 'azwe-rg-01'
kubernetesCluster: 'azwe-aks-01'
command: 'apply'
arguments: '-f $(System.DefaultWorkingDirectory)/kubernetes/acc.yaml'
Problem: Whenever I am making any changes in any branch, all the branch pipeline starts running.
If you just want to run the corresponding pipeline of the branch you modified, you need to make sure set the pipeline with the YAML file in the corresponding branch and set the correct branch trigger.
For example, for the Acc branch:
We need create a YAML file under the branch Feature/TestSample-acc with the branch trigger in the YAML file:
trigger:
branches:
include:
- Feature/TestSample-acc
Then create a pipeline with existing Azure pipeline YAML file:
New Pipeline-> Azure Repos Git(YAML)-> Select your repository-> Select existing Azure pipeline YAML file:
Now, this pipeline only triggered by the modification on the branch Feature/TestSample-acc:
You could set the same this for other branches, like
trigger:
branches:
include:
- Feature/TestSample-dev
Besides, if you do not want to control the trigger by the YAML file, you could override it by the UI trigger settings:
This option is disable by default, so that we could control the trigger in the YAML file directly.
If you enable it, you just need to add the Branch filters for just one branch:
If I am not understand your question correctly, please let me know for free that what you want to achieve.
You should check the CI trigger setting of the pipeline to only allow it to trigger on your wanted branches
Change CI Trigger
The answer by "Leo Liu-MSFT" is correct.
BUT you also need to make sure that on each branch the YAML file has a different name, otherwise a commit to a single branch triggers more than one pipeline build.