Azure - custom condition in YAML based pipeline - azure

I've an yaml based azure CI/CT pipeline. I've a task defined as below:
- task: PublishTestResults#2
displayName: 'Publish PCOM Test Results'
condition: always()
inputs:
testResultsFormat: NUnit
testResultsFiles: '*.xml'
searchFolder: '$(Common.TestResultsDirectory)/$(Build.SourceBranchName)/$(Build.BuildNumber)/test-results/PCOM'
mergeTestResults: true
testRunTitle: 'PCOM Tests'
It looks for all '*.xml' files under a particular s3 location. But the xml files will be created only if the below task is successful:
- bash: 'perl run_aws_cli.pl $(Build.BuildNumber) $(Build.SourceBranchName) $(s3Bucket) $(Build.SourcesDirectory)'
workingDirectory: 'azure-pipeline/scripts'
displayName: 'Run ECS Tasks'
So I want a custom condition to check if the later task is successful then only search xml files in the directory else don't search for it.
Currently it's searching always and the earlier task(Publish PCOM Test Results) is failing.

just do condition: succeeded() on your second task. this will only run if all the tasks before that run successfully. if you want a more sophisticated option, check this thread.
Alternatively, you can probably get more information on the build status from the API.
https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/get?view=azure-devops-rest-5.1

Related

Azure devops pipeline condition of plugin

link for the plugin : https://marketplace.visualstudio.com/items?itemName=touchify.vsts-changed-files
isPullRequest: ${{eq(variables['Build.Reason'], 'PullRequest')}}
- job: "check"
condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
displayName: Check Files Change
steps:
- task: ChangedFiles#1
name: CheckChanges
inputs:
rules: |
[Y]
X/**
variable: 'HasChanged'
isOutput: true
- job: "X"
dependsOn: check
condition: or(eq(dependencies.check.outputs['CheckChanges.Y'], 'true'),
or(succeeded(), ne(variables.isPullRequest, 'true')))
Hello I want to make the JOB run only if a change has been made to a particular folder or the run is not from Build Validation.
Need help exactly the conditions
You can define "policies" on branches to trigger specific pipeline definition(s) when file path(s) are involved.
Here you can find the Azure DevOps documentation (please have a look at the "path filters" part)

Azure pipeline selenium save screenshot

I have an azure pipeline that triggers a python selenium scrip that check that my website works properly.
But there is a stage that keeps failing because I need selenium to input a specific date, and as I am not sure if the date inputed is in the wrong format (locally works just fine) I would like to take a screenshot at that stage to fully understand what is happening in there.
locally this is my configuration to save the screenshot:
try:
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="root"]/div[2]/main/div[4]/div/button[2]'))).click()
except:
driver.save_screenshot('error.png')
This works just fine and it does output the png image in the local folder.
but running this on azure pipeline, is not saving the png file.
this is my pipeline configuration
stages:
- stage:
jobs:
- job: Configuration
steps:
- task: UsePythonVersion#0
inputs:
versionSpec: '3.8'
addToPath: true
- script: |
python -m pip install --upgrade pip
pip install selenium
printenv
- task: Pythonscript#0
inputs:
scriptSource: 'filePath'
scriptPath: './script1.py'
env:
USERNAMEOT: $(usernameot)
PASSWORDOT: $(passwordot)
- job: Artifact
steps:
- task: CopyFiles#2
displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
inputs:
SourceFolder: '$(system.defaultworkingdirectory)'
Contents: '**.png'
TargetFolder: '$(build.artifactstagingdirectory)'
flattenFolders: true
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: screenshots'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: screenshots
I do have a task to copy file and publish the artefact, but as this task runs in parallel, it completes before the previous job and return nothing.
I was wandering how can I save the png file I have to the artefact folder even if the Configuration job fails?
Thank you so much for any help you can provide me guys, I am really struggling on this
I was wandering how can I save the png file I have to the artefact folder even if the Configuration job fails?
You could try to set the Dependencies and condition for the job Artifact, like:
jobs:
- job: Artifact
dependsOn: Configuration
condition: succeededOrFailed()
With the dependsOn: Configuration, the job Artifact will executed after the job Configuration. And the condition: succeededOrFailed() will keep the job Artifact execute when the Configuration job fails.
You could check the document Specify conditions and Dependencies for some more details.

What is auto-creating new Build pipeline?

For a given project, I have four build pipelines, each pipeline Trigger has CI enabled, each has a branch filter for a single branch - master, Staging, QA, development. These work successfully, any completed pull request to one of those four branches are successfully kicking off a build process.
This morning, I created a new branch based off "development" branch. IT was a one-liner change, so I decided to make the change online in the browser using DevOps editor. I saved the change.
Immediately after saving the change online, I saw a new build pipeline was created (I had received an email saying my build failed). What caused the new Build pipeline to be created?
The new build pipeline looks to be auto-created, it is pure YAML:
pool:
vmImage: 'Ubuntu 16.04'
variables:
buildConfiguration: 'Release'
BuildPlatform: 'Any CPU'
Parameters.solution: = '*.sln'
Parameters.ArtifactName: = 'xxxxxx'
steps:
- task: NuGetToolInstaller#0
displayName: 'Use NuGet 4.4.1'
inputs:
versionSpec: 4.4.1
- task: NuGetCommand#2
displayName: 'NuGet restore'
inputs:
restoreSolution: '$(Parameters.solution)'
- task: VSBuild#1
displayName: 'Build solution'
inputs:
solution: '$(Parameters.solution)'
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
- task: PublishSymbols#2
displayName: 'Publish symbols path'
inputs:
SearchPattern: '**\bin\**\*.pdb'
PublishSymbols: false
continueOnError: true
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: '$(Parameters.ArtifactName)'
In the project, there were no pull requests created, and my private branch, I can see my change.
The email I received had this in the title (actual names removed):
[Build failed] MyProjectName CI - MyProjectName:MyBranchName - MyProejctName - bf9524f9
========
EDIT
I just found there is an azure-pipelines.yml file in the root folder of the branch. the contents match the above. Is this competing with the designer pipelines?
Yaml pipelines are better at scale, you can manage them in a central place, you can easily make mass edits and\or you can make them depend on each other to have more control. Visual designer is only good when you have couple of pipelines or you are only getting started with the whole pipelines thing.
Yaml pipelines do not necessary have to be in azure-pipelines.yml file. I store them in a separate repo :)
Updated comment:
Also the fact that there are no triggers added in your yaml mean that every new branch will queue builds. Read about 'trigger' on the yaml schema to get more understanding on this.
You can use something like below;
trigger:
branches:
include:
- master
- develop
exclude:
- no-build-branch
Given that there is none defined is behaves as below;
trigger:
branches:
include:
- '*'
These two are the same....
Designer picks the azure-pipelines.yml when you click edit. This is the default file name that gets picked up automatically to create a pipeline.
E.g. if you add the pipeline source to azure-pipelines.yml and commit/push it will automatically create a pipeline named 'Repo_Name CI' and queue a build as well.
Any new changes will work on it's merits as per the yaml definition.
you can always use different names and add as many pipelines you want as well.....

Azure DevOps PublishTestResults task -- how to publish results if tests fail

I am running a pytest-based suite of tests during my Azure DevOps build process. I have two jobs arranged to run these tests against two different environments.
In each job, I run the pytest tests using a script task and generate a junit-style xml output file, then have a PublishTestResults task publish that xml file. This is working great, and I'm able to peruse my test results in the azure build tests report UI -- but only if all the tests pass. If any tests fail, the publish task is skipped, and the tests aren't reported in the UI.
YML extract:
- job: 'RunTestsQA'
continueOnError: True
steps:
- task: UsePythonVersion#0
inputs:
versionSpec: '3.6'
architecture: 'x64'
- task: DownloadSecureFile#1
inputs:
secureFile: 'ConfigFile'
- script: pip install -r requirements.txt
displayName: 'Install Requirements'
- script: |
pytest -m smoke --ENV=qa --log-file $SYSTEM_ARTIFACTSDIRECTORY/smoke-qa.log --junitxml="TEST-qa-smoke.xml"
displayName: 'Test with pytest'
# PUBLISH JUNIT RESULTS
- task: PublishTestResults#2
inputs:
condition: succeededOrFailed()
testResultsFormat: 'JUnit' # Options: JUnit, NUnit, VSTest, xUnit
testResultsFiles: '**/TEST-*.xml'
#searchFolder: '$(System.DefaultWorkingDirectory)' # Optional
mergeTestResults: false # Optional
testRunTitle: 'API_CHECK QA'
#buildPlatform: # Optional
#buildConfiguration: # Optional
publishRunAttachments: true # Optional
Through some experimentation, I've been able to confirm the XML file is always created. What do I need to fix here? A test report isn't super helpful if it only shows up when the tests pass.
In your task description, the condition is effectively listed as a task input, and hence won't be taken into account at all.
You had:
# PUBLISH JUNIT RESULTS
- task: PublishTestResults#2
inputs:
condition: succeededOrFailed()
testResultsFormat: 'JUnit' # Options: JUnit, NUnit, VSTest, xUnit
testResultsFiles: '**/TEST-*.xml'
The correct setup is
# PUBLISH JUNIT RESULTS
- task: PublishTestResults#2
inputs:
testResultsFormat: 'JUnit' # Options: JUnit, NUnit, VSTest, xUnit
testResultsFiles: '**/TEST-*.xml'
condition: succeededOrFailed()
The full list of things you can do with conditions is here
I'm using Ruby and Minitest, but I have found that the following setting allows the PublishTestResults task to run:
- script: |
pytest -m smoke --ENV=qa --log-file $SYSTEM_ARTIFACTSDIRECTORY/smoke-qa.log --junitxml="TEST-qa-smoke.xml"
displayName: 'Test with pytest'
continueOnError: true
The only issue that I have found with this setting is that if the build fails, it reports as "Partially Succeeded" and not "Failed".
edit:
Of course, if your build process has any deploy tasks after the test task, you may not want to use this setting.

How do we trigger a build only for tags matching a pattern

I want to create a build script specifically when I push a tag pattern on git (not a branch).
But I cannot find it in the
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=vsts
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=vsts&tabs=yaml%2Cbatch
https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=vsts
I'm looking specifically at "pattern" rather than a static string
I used the following in my .vsts-ci
- task: Npm#1
displayName: 'npm run build'
inputs:
command: custom
verbose: false
customCommand: 'run build'
- task: Npm#1
displayName: 'npm publish'
inputs:
command: custom
verbose: false
customCommand: 'publish'
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
Specifically the condition you need to put in your control block is
and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v')) for v* but you can change that to something else
When I want VSTS to build on git tag pattern I do this
refs/tags/v*
will build for git tags 'v1.2.3'
For all tags:
refs/tags/*
I use this in conjunction with GitVersion.
EDIT: I should have said that this is added to the Build Pipeline BRANCH FILTERS trigger. When you specify a branch filter you usually get a drop-down list of branches. But you can also manually type in any other filter, which is what I do.

Resources