I have a problem with dependency on yaml. When I cancel the previous stage I want my next stage not to execute. The problem is with the or() and() part. When I add succeeded() to and() part it is working fine but it has to work on the second or() condition. Unfortunately when I add succeeded() to or() part it is not working as expected. The below code is not working, when I cancel the previous stage this one executes ?
dependsOn: 'CI'
condition: or(succeeded(), ne(variables['Build.Reason'], 'PullRequest'), eq('${{ parameters.devEnvironment }}', 'dev'), and(eq('${{ parameters.devEnvironment }}', 'dev'), eq(variables['Build.SourceBranch'], 'refs/heads/master'),succeeded()))
When I cancel the previous stage I want my next stage not to execute.
What you need to do is to use and() condition to connect your original conditions and suceeded():
condition: and({original condition}, succeeded())
Here is the exmaple:
condition: and(or(ne(variables['Build.Reason'], 'PullRequest'), eq('${{ parameters.devEnvironment }}', 'dev'), and(eq('${{ parameters.devEnvironment }}', 'dev'), eq(variables['Build.SourceBranch'], 'refs/heads/master')), succeeded())
Related
I have a pipeline that will clean, convert a VM to an image, and delete all of the resources. I would like to use that same pipeline to delete VMs without capturing an image. I can make all of the stages conditional, but when I end up with no stages that have no conditions. I keep seeing that the accepted solution to this problem is to make the dependsOn conditional as shown below. the deleteVM parameter is a boolean.
Parameters:
- name: deleteVM
displayName: "just delete it"
type: boolean
default: false
- stages:
- stage: aStage
${{ if eq(parameters.deleteVM, 'false') }}:
dependsOn: anActualStage
${{ if eq(parameters.deleteVM, 'true') }}:
dependsOn: []
The issue is that when I do this, and try to run the pipeline with the second condition being true, then it errors when parsing the pipeline YAML: 'dependsOn' is already defined.
P.S. I've looked at the following and they imply that the way I'm doing it is the "right" way:
Azure pipeline - Stage condition dependson
Azure Pipeline - Stage with Multiple depends on with if condition
Instead of 2 if statements, use if/else. Or simply leave out the else altogether, since you're setting the depends on to an empty list.
- stages:
- stage: aStage
${{ if eq(parameters.deleteVM, 'false') }}:
dependsOn: anActualStage
${{ else }}:
dependsOn: []
- stage: aStage
${{ if eq(parameters.deleteVM, 'false') }}:
dependsOn: anActualStage
#4c74356b41´s answer about using if-else is probably your best bet.
The way yaml works is that the dependsOn in your example is only really added to the "compiled" yaml file if your if statement resolves to true. Thus, there must be something wrong with your variables, as it looks like both of your logical statements resolves to true. I cannot really see why as it looks impossible by your example, but that is the only way I can see you getting the error you are seeing.
I have pipeline that makes some changes and commits another change which triggers another pipeline and I dont want that automatic update to trigger pipeline.
I had an idea that I will specify commit message and that ignore it, but for some reason I cannot get it working.
Can you help me with that?
variables:
COMMIT_MESSAGE: "MyCommitMessage"
workflow:
rules:
- if: $CI_COMMIT_MESSAGE != $COMMIT_MESSAGE
...
You have to add the never keyword and use a regex like this :
variables:
COMMIT_MESSAGE: "MyCommitMessage"
workflow:
rules:
- if: $CI_COMMIT_MESSAGE =~ /^.*COMMIT_MESSAGE/
when: never
- when: always
The if will be evaluate to true and the pipeline will never run.
I am recently doing an CI/CD setup using Azure. The goal is to have the developer select the type of build to be created i.e Staging / Prod.
Thanks to How to write if else condition in Azure DevOps Pipeline, I have added following code -
parameters:
- name: selectConfiguration
displayName: Select build configuration
type: string
default: Debug
values:
- Debug
- Release
variables:
- name: config
${{ if eq(variables['parameters.selectConfiguration'], 'Debug') }}:
value: Debug
${{ else }}:
value: Release
This gives me the following result -
But no matter what I select in this radio group, it always run the else block. i.e. the if-else always fails. Any help to understand what I am doing wrong here?
Try the below, it should work. I am using the same logic to switch between different agent pools.
variables:
${{ if eq(parameters.selectConfiguration, 'Debug') }}:
config: Debug
${{ else }}:
config: Release
In YAML pipeline, you can not use the if...else expression to assign different values to a variable in different conditions.
You can only use the if expression to determine a variable which has one specified value can be available in the specified condition. See "Conditionally assign a variable".
The if...else expression can be used to:
assign different values to an input of the task in different conditions. See "Conditionally set a task input".
run different steps in a job in different conditions. See "Conditionally run a step".
Is it possible to run only one build pipeline task on a schedule and not the whole pipeline on a schedule? I have a task in a build pipeline to generate some report about the pipeline. I would want the task to run once every month.
Yes.. it is possible in several ways:
you can set previously to the task the condition:
${{ if eq(variables['isBuild'], true) }}:
You can configure conditions to run the tasks that you want, depending of whatever, for example in this case using the variable isBuild, defined previously:
task: PublishBuildArtifacts#1
displayName: 'Publish artifact: drop'
inputs:
PathtoPublish: 'whatever'
ArtifactName: 'drop'
publishLocation: 'Container'
condition: eq(variables['isBuild'], true)
You can configure conditions to run the stages that you want, so previously you can group your tasks in stages, depending of whatever, for example using in this case in stage the variable isBuild, defined previously:
stage: Build
displayName: 'Build'
condition: eq(variables['isBuild'], true)
In every example in case of IsBuild be different to true will not be run.
You can find more info in https://github.com/MicrosoftDocs/azure-devops-docs/blob/main/docs/pipelines/process/conditions.md
Also If you want to schedule your task to be executed only 1st day of every month at 07:00:
trigger:
master #This is the trigger for other stages. It is not needed for the scheduled stage.
schedules:
cron: '0 7 1 * *'
displayName: 'Deploy every 1st day of every month at 07:00Z'
branches:
include:
main
always: true
Then to ensure that task runs as part of schedule, use the condition:
- stage: 'Test'
displayName: 'Deploy to the test environment'
dependsOn: Dev
condition: eq(variables['Build.Reason'], 'Schedule')
For more detail you can to to:
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/scheduled-triggers?view=azure-devops&tabs=yaml#scheduled-triggers
Put the task in a second pipeline, and run that on a monthly schedule.
If you want to avoid yaml code duplication, you could define a template containing that one task and have that template called from both pipelines.
I want to trigger a single job manually in GitLab CI/CD. What steps I need to follow?
I have declared environment variable as well but while triggering a pipeline manually, selecting the branch it trigger all other jobs as well.
A Gitlab pipeline is a stream of jobs, by default you can not just execute a single job in isolation.
To work around it, you can use the rules keyword https://docs.gitlab.com/ee/ci/jobs/job_control.html#specify-when-jobs-run-with-rules
Example
job1:
...
rules:
- if: '$execute_job_1 == "true"'
when: always
- when: never
job2:
...
rules:
- if: '$execute_job_2 == "true"'
when: always
- when: never
So if want to execute only job1, you pass the appropriate variable when you run the pipeline