I'm trying to get a cron schedule running by using yaml instead of azure pipeline scheduling.
This is my YAML:
trigger: none
pr: none
schedules:
- cron: "0 0 * * *"
displayName: Daily midnight build when changes have been pushed
always: true
branches:
include:
- branch1
- branch2
variables:
- group: BranchVariables
First it ran every time someone made a PR, figured out that I've to add "pr: none". Now I don't know what is missing. What am I potentially missing? The ms documentation is sparsely and I already read all the related topics on this issue.
Thanks in advance!
Update:
When I want to check the schedules on the pipeline it doesn't show me any and I can't sync
When I edit the pipeline I get the current yaml version on master branch we do not use anymore and is not the default branch. The pipeline should only run for the two branches I include in the yaml branches property.
The pipeline still didn't run this night and my guess was now that the yaml with the schedules must be also checked into the master branch. I figured out that the default branch can be set for every pipeline and I changed it now and finally can sync and see the schedules. What's still mysterious to me is that I had to add the default pipeline branch to the included branches before the schedules showed up. I'm now using only one included branch.
All seems fine to be with your pipeline. I tested it with this code:
trigger: none
pr: none
schedules:
- cron: "0 0 * * *"
displayName: Daily midnight build when changes have been pushed
always: true
branches:
include:
- branch1
- branch2
- master
variables:
- group: BranchVariables
steps:
- script: echo "hello $(test)"
Please keep in mind that you have filter for specific branches, so make sure that branch1 or branch2 exists.
I created a new branch branch1 and changed to default branch for the pipeline and ten synced schedules:
Related
I've two Azure DevOps CI pipelines
DataPipeline\Windows - Build
DataPipeline\TestPipeline
The second pipeline is a pipeline resource trigger. I've used this doc as a reference.
When a pull request is created (e.g. with dev branch as source branch and main branch as target branch, not merged yet) both these pipelines are triggered (by default configuration). The second pipeline may pick up old artefacts (this is a different part altogether). A second run for pipeline DataPipeline\TestPipeline is triggered automatically as soon as DataPipeline\Windows - Build completes for a commit in the dev branch. Is this possible? I've specified the branch name in the resources trigger. Is something wrong with the YAML configuration?
My expected behaviour is when a pull request is created for branch name: xyz (not main), DataPipeline\TestPipeline shouldn't be triggered again after DataPipeline\Windows - Build completes.
DataPipeline\TestPipeline.yml
trigger: none
resources:
pipelines:
- pipeline: DataPipeline
source: 'DataPipeline\Windows - Build'
branch: main
trigger:
branches:
include:
- main
name: TestPipeline_$(variable)
jobs:
// template based job
If I wanted to run a scheduled task once a month for checking for outdated dependencies but I already have a CI pipeline how can I do that? For example I have a pipeline that runs though code sniffs -> checkmarx + twistlock -> deploy to dev -> stage and whatnot. This triggers on master. I want to also include the ability to have a scheduled task of dependabot to occur once every month. How can I mix this scheduled task into an established CI pipeline? This is all contained within Azure Devops as well.
I only want to run the single task of dependabot once a month. I don't want to run the entire pipeline once a month
I suggest creating a second - entirely separate - pipeline to run dependabot once a month.
That way, you can have the appropriate triggers for the CI pipeline, and the appropriate schedule for the dependabot pipeline, with exactly the right tasks in each one and no duplication.
You can run a pipeline using both the trigger and schedules.
For example, to run a stage on the 1st day of every month at 08:00 UTC, you can use:
trigger:
- master #This is the trigger for other stages. It is not needed for the scheduled stage.
schedules:
- cron: '0 8 1 * *'
displayName: 'Deploy every 1st day of every month at 08:00Z'
branches:
include:
- master
always: true
Then, to ensure that a specific stage runs as part of the scheduled run, use the condition expression, for example:
- stage: 'Test'
displayName: 'Deploy to the test environment'
dependsOn: Dev
condition: eq(variables['Build.Reason'], 'Schedule')
Refer to this MSDocs article for more on the syntax of schedules: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/scheduled-triggers?view=azure-devops&tabs=yaml#scheduled-triggers
Azure scheduled pipeline runs for every push in master.What am I missing?Here's the yaml code:
schedules:
- cron: "0 23 * * *"
displayName: Nightly build
branches:
include:
- master
Apparently, the previous builds aren't failing.
I've already tried to remove this pipeline and create it again, but it keeps running for every push.
The scheduled runs are correct:
Ok, I think I figured this out.
Just added the following lines to the yml file and it no longer runs the scheduled pipeline on every push.
trigger: none
pr: none
Is there a way to customize the pipeline scheduling options in Azure to have it run only the second week of each month?
I know you can schedule it to run on individual days of the week, but I cannot figure out how I would do this on a monthly scale.
Can I do this if my pipeline was made as a classic/GUI based, and not
as a YAML pipeline?
In the classic pipelines, you can only set scheduled triggers for each week. As far as I know, you can not have it run only the second week of each month in the classic pipelines. However, you can set schedule triggers in yaml pipeline and use it to trigger your classic pipeline.
Here is the sample if you are going to use a YAML pipeline:
schedules:
- cron: "0 0 8-14 * *"
displayName: schedule
branches:
include:
- main
always: true
In this example:
The pipeline will be triggered from the 8th to the 14th of this
month. You need to update the date each month.
always: true means run even when there are no code changes.
Agree with iikkoo that if you want to run your pipeline by only using scheduled triggers, you must disable PR and continuous integration triggers by specifying pr: none and trigger: none in your YAML file.
You can add a build completion trigger in this yaml pipeline to trigger your classic pipeline:
Please find more detailed information about Configure schedules for pipelines in the document.
You can achive this by creating a scheduling trigger in your YAML config. Note tough, you must disabled PR and CI triggers to run your pipeline by using scheduled triggers.
You disable the trigger by setting pr: none and trigger: none. Then you define the schedule using cron syntax.
schedules:
- cron: "0 0 1/14 * *" # At 00:00 on every 14th day-of-month from 1 through 31.
displayName: Second week of each month
branches:
include:
- master
...
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/scheduled-triggers?view=azure-devops&tabs=yaml
https://github.com/atifaziz/NCrontab/wiki/Crontab-Expression
https://crontab.guru/
It doesn't seem to do so in the UI, but you can still trigger the build via an API call on your own schedule.
https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.1
I'm a bit confused with how to set this workflow up using pull requests.
I have in place an existing multi-stage YAML build pipeline that in summary does the following:
Runs a build from any branch
Runs a deployment job to a Development Environment/Resource, if the source branch is feature/* or release/* and the build succeeds
Runs a deployment job to a UAT Environment/Resource, if the source branch is release/*
Runs a deployment job to Live/Production Environment/Resource, if the source branch is master
So off the back of CI this workflow seems to work fine, the correct stages are run depending on the branch etc.
I then decided that branch policies and pull requests might be a better option for code quality rather than let any of the main branches be direct committed to, so I started to rework the YAML to account - mainly by removing the trigger to
trigger: none
This now works correctly in line with a branch policy, the build only gets kicked off when a pull request on to develop or master is opened.
However this is then where I'm a bit confused with how this is supposed to work and how I think it works ....
Firstly - is it not possible to trigger the multi-stage YAML off the back of pull requests (using Azure Repos) ? In my head all I want to do is introduce the pull request and branch policies but keep the multi-stage deployments to environments as is.
However, the deployment job stages all get skipped now - but this might be to do with my conditions within the YAML, which are as follows:
- stage: 'Dev'
displayName: 'Development Deployment'
dependsOn: 'Build'
condition: |
and
(
succeeded()
eq(variables['Build.Reason'], 'PullRequest'),
ne(variables['System.PullRequest.PullRequestId'], 'Null'),
or
(
startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/'),
startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')
)
)
jobs:
- deployment: Deploy
pool:
name: 'Development Server Agent Pool'
variables:
Parameters.WebsitePhysicalPath: '%SystemDrive%\inetpub\wwwroot\App'
Parameters.VirtualPathForApplication: ''
Parameters.VirtualApplication: ''
environment: 'Development.Resource-Name'
.....
Is there something I am missing?
Or do I have to remove the multi-stage deployments from YAML and revert back to using Release Pipelines for pull requests (maybe with approval gates??)
Thanks in advance!
It looks like the issue of the conditions within the YAML.
If the pipeline is triggered by a PR. the value variables['Build.SourceBranch'] will be refs/pull/<PR id>/merge. The express in above condtion startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/') will be false, which caused the stage to be skipped. See build variables for more information.
You can try using variables['System.PullRequest.SourceBranch'], which will be evaluated to the value of the source Branch of the PR. Check System variables for more information. See below:
condition: |
and
(
succeeded(),
eq(variables['Build.Reason'], 'PullRequest'),
ne(variables['System.PullRequest.PullRequestId'], ''),
or
(
startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/feature/'),
startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/release/')
)
)