Azure Devops Pipeline Scheduled trigger on other repository - azure-devops

I have a repo with Yaml definition of pipelines, that reference other repo for steps.
I'm trying to have the pipeline triggered based on a scheduled (cron) definition (only when source code changed) of a repo that is not the repo of the pipeline.
I tried that but the pipeline is not triggered:
resources:
repositories:
- repository: self
type: git
ref: master
- repository: my-project
name: my-project
type: git
ref: master
schedules:
- cron: 55 10 * * 1,2,3,4,5
branches:
include:
- master
always: false
trigger: none
pr: none
(...)
Repository "self" references the repo with pipeline YAML file(s)
If I get rid of the last two lines, the pipeline is triggered when I save it, but not when I change code on "my-project"
I tried also with ref/heads prefixes for branch name without any change.
Any idea?

This can't be done, the way you're trying. A repository resource can include a trigger, but not a schedule; see the docs at https://learn.microsoft.com/en-us/azure/devops/pipelines/process/resources?view=azure-devops&tabs=schema#define-a-repositories-resource
What you can do is a non-scheduled trigger:
resources:
repositories:
- repository: self
type: git
ref: master
- repository: my-project
name: my-project
type: git
ref: master
trigger:
branches:
include:
- master
This won't work on any schedule; it will simply be immediately triggered by any change to the code in my-project, in the master branch.

Related

Scheduled run not running even though it was set up correctly?

I've added a schedule block to my pipeline that backs up my RDS database. This is the main yaml file for the pipeline, and yaml validator finds no errors. So why is it not running? Nothing shows up in Scheduled Runs section in the UI, and I actually waited 3 hours for it to run, to no avail. What am I missing?
name: $(Date:yyyyMMdd)$(Rev:.r)
variables:
- template: ../global-vars.yml
resources:
repositories:
- repository: self
type: git
name: Deployment
trigger: none
schedules:
- cron: "0 */3 * * *"
displayName: DB backup every 3 hours
branches:
include:
- master
always: true
stages:
- stage: DBBackup
displayName: DB Backup
jobs:
- template: /templates/db/backup.yml
There is no property schedules which can be placed in resources > repositories > repository according to YAML syntax documentation
https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/resources-repositories-repository?view=azure-pipelines
You can add schedules on the top level of YAML
https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/schedules-cron?view=azure-pipelines
try something like this sample
name: $(Date:yyyyMMdd)$(Rev:.r)
variables:
- template: ../global-vars.yml
schedules:
- cron: "0 */3 * * *"
displayName: DB backup every 3 hours
branches:
include: master
always: true
resources:
repositories:
- repository: self
type: git
name: Deployment
trigger: none
stages: (...)

Azure DevOps multi-repo, multi-branch-trigger, selecting the branch to build from

In an Azure DevOps pipeline, I had
resources:
repositories:
- repository: self
type: git
name: MyProject.Web
trigger:
- master
- repository: UiRepo
type: git
name: MyProject.Web.UI
trigger:
- main
[other stuff]
steps:
- checkout: self
- checkout: UiRepo
[other stuff]
This has worked fine: The pipeline ran when triggered by completion of a pull request to either the master branch of the "self" repo or the main branch of the UiRepo. The checkouts pulled from the master branch of "self" and the main branch of UiRepo.
Now I'm adding a branch called release/1.0 to each of the repos, and will introduce other release/x.x branches in the future. So now I have:
resources:
repositories:
- repository: self
type: git
name: MyProject.Web
trigger:
- master
- release/*
- repository: UiRepo
type: git
name: MyProject.Web.UI
trigger:
- main
- release/*
[other stuff]
steps: # ??????
- checkout: self
- checkout: UiRepo
[other stuff]
Now the following is required:
If the triggering branch's name ($Build.SourceBranchName) is either 'master' or 'main', check out master from "self" and main from UiRepo.
Otherwise, check out a branch whose value is $Build.SourceBranch from both repos.
At https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#checking-out-a-specific-ref, I've read that I can use inline syntax in the checkout steps to include specific refs. In my case, I think this comes out as follows:
- ${{ if in(variables['Build.SourceBranchName''], 'master', 'main') }}:
- checkout: git://MyProject/MyProject.Web#refs/heads/master
- checkout: git://MyProject/MyProject.Web.UI#refs/heads/main
- ${{ else }}
- checkout: git://MyProject/MyProject.Web#$(Build.SourceBranch)
- checkout: git://MyProject/MyProject.Web.UI#$(Build.SourceBranch)
(or do I need to be using [ variables['Build.SourceBranch'] ] here instead of $(Build.SourceBranch)?)
But, by dispensing with the references in the checkout steps to "self" and UiRepo, this seems to be divorcing the connection between the checkout steps and the repos as I've already defined them. Does that matter? Is this correct anyway? If it isn't, how can I accomplish my goal?
The if/else you are using is named Conditional Insertion in DevOps concept.
Need to clarify is in this concept, it indeed can accept predefined variables, but only which is 'Available in templates'.
I notice you are using Build.Repository.SourceBranchName, this variable not even in the list of predefined variables, let alone the concept of 'Available in templates'. So the first part of your Conditional Insertion will not be process. Only the second part will be able to process.
And please do not use 'self' as the alias of the repository, this will causes ambiguity.(Your current situation use this is no problem, this is just a suggestion.)
So basically, your pipeline definition should be like this:
trigger:
- none
resources:
repositories:
- repository: self
type: git
name: Repo1
trigger:
- master
- release/*
- repository: UiRepo
type: git
name: Repo2
trigger:
- main
- release/*
pool:
vmImage: ubuntu-latest
steps:
- ${{ if in(variables['Build.SourceBranchName'], 'master', 'main') }}: #This place needs to change
- checkout: git://BowmanCP/Repo1#refs/heads/master
- checkout: git://BowmanCP/Repo2#refs/heads/main
- ${{ else }}:
- checkout: git://BowmanCP/Repo1#$(Build.SourceBranch)
- checkout: git://BowmanCP/Repo2#$(Build.SourceBranch)
Works prefect on my side:

Azure Devops Pipeline - Repository branch on trigger

I have a pipeline using a git reposiory
resources:
repositories:
repository: myrepo
type: git
name: src/myrepo
ref: nameofbranch
trigger:
branches:
include:
- triggeringbranch
I want to be able to change repo branch (nameofbranch could be a parameter - for manual run),
but then when the pipeline is automatically triggered by changes on a branch (for example changes on triggeringbranch), I'd like of course the pipeline to use that triggeringbranch...
How to deal with it ?
Can i use some condition to set the value of ref , using Build.SourceBranch if not empty, or nameofbranch otherwise ?
Thank you
You can create a local variable and fill it using the conditions. After that, you can use the variable as input for "ref":
parameters:
- name: "branchName"
default: ""
variables:
- name: "branchName"
${{ if eq(parameters.branchName, '') }}:
value: $(Build.SourceBranch)
${{ else }}:
value: ${{ parameters.branchName }}
resources:
repositories:
- repository: myrepo
type: git
name: src/myrepo
ref: $(branchName)
trigger:
branches:
include:
- triggeringbranch

disabling pipeline trigger when branch gets created

I am having multiple repo triggers in single YAML pipeline and for one repository I want to disable trigger when new branch created for test repository. I tried only including the path but didn't help.
repositories:
- repository: BBB
type: git
name: ABC/tools
ref: master
trigger:
branches:
include:
- testbranch/*
- repository: AAA
type: git
name: ABC/test
ref: master
trigger:
paths:
include:
- origin/testbranch/*
Any suggestion to above sniffet
you could add "exclude" for this trigger. When new branches are created in this "testbranch" folder, the pipeline will be triggered. If you don't want the pipeline to be triggered, create the new branches with "old" prefix.
repositories:
- repository: BBB
type: git
name: ABC/tools
ref: master
trigger:
branches:
include:
- testbranch/*
- repository: AAA
type: git
name: ABC/test
ref: master
trigger:
paths:
include:
- origin/testbranch/*
exclude:
- origin/testbranch/old***

azure devops yaml pipeline with template does not checkout referenced repository

I am using the new Azure DevOps Yaml multi stage pipeline functionality
I've got an Azure DevOps yaml pipeline file for which I want to use templates. I would like the pipeline to checkout self and another repository.
For some reason, self repo has been checked out when this runs, but the repo: pipelines is not being checked out and therefore the job fails (because some of the file dependencies it requires are not there.
Here is an excerpt from my template:
resources:
repositories:
- repository: self
- repository: pipelines
name: vstsproject/pipelines
type: git
source: pipelines
variables:
# Container registry service connection established during pipeline creation
imageRepository: 'vstsprojectweb'
dockerfilePath: '$(Build.SourcesDirectory)/src/Dockerfile.CI'
BuildConfiguration: 'Release'
tag: '$(Build.BuildId)'
stages:
- stage: 'PRD'
jobs:
- template: update-connection-string-db.yml#pipelines
parameters:
resourceGroup: 'application-DEV'
DBSearchString: '###dbservername###'
What is it that I am doing wrong?
I have referred to this microsoft documentation.
You don't need to reference the linked repo in the resources (i.e. self), and if it is the only repo, then it is checked out by default in jobs (not deployment jobs), but if you have additional repos, then you need to check them out manually (with -checkout: <name_of_repo>).
So just do (PS: Cleaned up a little, assumed that the repo is in the same project):
resources:
repositories:
- repository: pipelines
source: pipelines
variables:
# Container registry service connection established during pipeline creation
imageRepository: 'vstsprojectweb'
dockerfilePath: '$(Build.SourcesDirectory)/src/Dockerfile.CI'
BuildConfiguration: 'Release'
tag: '$(Build.BuildId)'
stages:
- stage: 'PRD'
jobs:
- checkout: self
- checkout: pipelines
- template: update-connection-string-db.yml#pipelines
parameters:
resourceGroup: 'application-DEV'
DBSearchString: '###dbservername###'
I ended up putting everything into the same repo and then checking out self in a job.
That worked for me.
jobs:
- job: dbconnectionstring
displayName: 'db connection string'
pool: Windows
steps:
- checkout: self
- template: templates/update-connection-string-db.yml