Continue build after script fails in Azure DevOps - azure-devops

In my Azure DevOps build task I run a Cypress test. If the test fails the build is canceled. But I want to run another task after Cypress the publishes the test results.
I've tried it with this task in my pipeline.yml file:
- task: PowerShell#2
inputs:
targetType: "inline"
script: "yarn test:cypress"
errorActionPreference: "continue"
displayName: "start server and run cypress"
But this doesn't seem to have any effect.
I've tried add -ErrorAction 'Continue' to to script"
"start": "npm-run-all -s build:shared-web run:shell",
"cy:run": "cypress run -ErrorAction 'Continue'",
"test:cypress": "start-server-and-test start http://localhost:3000 cy:run"
But this fails with:
error: unknown option: -E
It looks like Cypress is seeing the ErrorAction as a Cypress parameter.
So what's the correct way of continuing a build if a task fails?

You can add to the task this:
continueOnError: true
Now the build will keep running even the tests will fail.
If you want to fail the build if the tests failed but you want to run only one task to publish the results, you can add this to the publish tests task:
condition: always()
Now the publish task will be running always, even the tests are failed and the build canceled/failed.

Related

Azure Devops - Run a task only if previous task has run successfully

I am currently writing a pipeline to deploy my infrastructure in Terraform.
I would like that some tasks do not run unless the first task has run successfully. I have found the use of conditions such as success() or failed().
However, upon reading closely, I realize that these conditions are based on Job Status, while in my case there is just one job containing all my tasks.
Is there any way to specify such condition ? And will it be able to refer to a specific task name ? (For example, Task C can run only if Task A ran with success).
You could add a PowerShell task just behind the first task to set a variable (ones for example) by using ##vso[task.setvariable variable=ones]true. Then, use this variable in condition for those tasks that you want them do not run unless the first task has run successfully.
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Write-Host "##vso[task.setvariable variable=ones]true"
Then, set condition: condition: eq(variables.ones, 'true') for each task that you do not want it run unless the first task has run successfully. for example:
- task: PowerShell#2
displayName: task4
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host "Hello World"
condition: eq(variables.ones, 'true')
The PowerShell task will run if the previous tasks run successfully, in my case, there only is task1. Thus, if the task1 succeed, PowerShell task will set the variable value as true. Next tasks will run when the variable equal true. When the task1 failed, neither PowerShell task will run nor the next tasks.

Is there a way to run powershell scripts with elevated privileges as part of a yaml pipeline?

I'm struggling with creating an azure yaml pipeline to download and install a chocolatey package. I figured the best way to do that was via a Powershell#2 task but I'm open to ideas, but I'm running into access issues. Below is the task:
- task: PowerShell#2
displayName: 'Install Chocolatey Package'
inputs:
targetType: 'inline'
errorActionPreference: 'stop'
failOnStderr: true
script: |
choco install -y MyPackage --version 1.0.0 -s MySource
This is run on a self-hosted agent that is spun up as part of a scaleset, so the machine isn't known in advance. When this runs, it appears to download the package fine, but then is unable to do anything with it and the logs show lots of errors trying to access the package itself as well as warnings that it's not running with elevated privileges. Assuming that running this task with elevated privileges would fix this, how can that be done?

SonarQube in Azure DevOps Pipeline leads to error MSB6006: "csc.dll" exited with code 137

We are trying to use the SonarQube v5.5 extension in our Azure DevOps pipelines. The build runs fine as long as we do not include the pipeline task SonarQubePrepare in our azure-pipelines.yml file. As soon as we include the mentioned task, the MSBuild seems to end in a loop with the error error MSB6006: "csc.dll" exited with code 137 and then keeps trying to build the same project.
We did a little research and found that this could be related to a memory issue. We have allocated 16gb of ram to the build AgentPool. The only effect seems to be that the loop runs faster than before.
Has anyone encountered a similar problem?
Pipeline:
jobs:
- job: 'BackendBuild'
pool:
name: 'xyz'
workspace:
clean: all
steps:
- checkout: git://$(repository) # Variable for repository name to checkout
persistCredentials: true
clean: true
# Build runs fine without this task
- task: SonarQubePrepare#5
displayName: Prepare analysis with SonarQube (MSBuild)
inputs:
SonarQube: 'Sonarqube' # Service connection name
scannerMode: 'MSBuild'
projectKey: $(repository)
projectName: $(repository)
- task: DotNetCoreCLI#2
displayName: 'Build projects'
inputs:
projects: '**/*.csproj'
arguments: '--configuration Release'
Log:
##[warning]myproj.Domain.Email/Services/ViewRenderingService.cs(31,35): Warning S4457: Split this method into two, one handling parameters
check and the other handling the asynchronous code.
Services/ViewRenderingService.cs(31,35): warning S4457: Split this
method into two, one handling parameters check and the other handling
the asynchronous code.
[/azp/agent/_work/1/s/myproj.Domain.Email/myproj.Domain.Email.csproj]
Sonar: (myproj.Domain.Email.csproj) Project processed successfully
Sonar: Preparing for Razor compilation, moved files
(/azp/agent/_work/1/.sonarqube/out/9/Issues.json;/azp/agent/_work/1/.sonarqube/out/9/output-cs/encoding.pb;/azp/agent/_work/1/.sonarqube/out/9/output-cs/file-metadata.pb;/azp/agent/_work/1/.sonarqube/out/9/output-cs/metrics.pb;/azp/agent/_work/1/.sonarqube/out/9/output-cs/symrefs.pb;/azp/agent/_work/1/.sonarqube/out/9/output-cs/token-cpd.pb;/azp/agent/_work/1/.sonarqube/out/9/output-cs/token-type.pb;/azp/agent/_work/1/.sonarqube/out/9/ProjectInfo.xml) to /azp/agent/_work/1/.sonarqube/out/9.tmp. Sonar: After Razor
compilation, moved files
(/azp/agent/_work/1/.sonarqube/out/9/Issues.Views.json;/azp/agent/_work/1/.sonarqube/out/9/output-cs/file-metadata.pb)
to /azp/agent/_work/1/.sonarqube/out/9.Razor. Sonar: After Razor
compilation, moved files
(/azp/agent/_work/1/.sonarqube/out/9.tmp/Issues.json;/azp/agent/_work/1/.sonarqube/out/9.tmp/output-cs/encoding.pb;/azp/agent/_work/1/.sonarqube/out/9.tmp/output-cs/file-metadata.pb;/azp/agent/_work/1/.sonarqube/out/9.tmp/output-cs/metrics.pb;/azp/agent/_work/1/.sonarqube/out/9.tmp/output-cs/symrefs.pb;/azp/agent/_work/1/.sonarqube/out/9.tmp/output-cs/token-cpd.pb;/azp/agent/_work/1/.sonarqube/out/9.tmp/output-cs/token-type.pb;/azp/agent/_work/1/.sonarqube/out/9.tmp/ProjectInfo.xml)
to /azp/agent/_work/1/.sonarqube/out/9 and will remove the temporary
folder.
##[error]/usr/share/dotnet/sdk/3.1.420/Roslyn/Microsoft.CSharp.Core.targets(70,5): Error MSB6006: "csc.dll" exited with code 137.
/usr/share/dotnet/sdk/3.1.420/Roslyn/Microsoft.CSharp.Core.targets(70,5):
error MSB6006: "csc.dll" exited with code 137.
[/azp/agent/_work/1/s/myproj.Data/myproj.Data.csproj]
myproj.Domain.Messages ->
/azp/agent/_work/1/s/myproj.Domain.Messages/bin/Release/netstandard2.1/myproj.Domain.Messages.dll
myproj.Domain.Email ->
/azp/agent/_work/1/s/myproj.Domain.Email/bin/Release/netcoreapp3.1/myproj.Domain.Email.dll
Sonar: (myproj.Domain.Messages.csproj) Project processed successfully
myproj.Domain.Email ->
/azp/agent/_work/1/s/myproj.Domain.Email/bin/Release/netcoreapp3.1/myproj.Domain.Email.Views.dll
myproj.backoffice.sts ->
/azp/agent/_work/1/s/myproj.backoffice.sts/bin/Release/netcoreapp3.1/myproj.backoffice.sts.dll
Sonar: (myproj.backoffice.sts.csproj) Project processed successfully
Build FAILED.

Not found scriptPath in azure devops

I put a shell script file in a folder on my repo root and tried to run that in my devops pipeline but it says that cannot find the scriptPath:
[error]Not found scriptPath: /home/vsts/work/1/s/pipelines/databricks-cli-config.sh
I am simply creating a task to run the shell script, like this:
- task: ShellScript#2
inputs:
scriptPath: 'pipelines/databricks-cli-config.sh'
args: '$(databricks_host) $(databricks_token)'
displayName: "Install and configure the Databricks CLI"
Any idea?
Make sure you checkout your code and you are on correct level. So if you are on regular job please add working directory:
- task: ShellScript#2
inputs:
scriptPath: 'pipelines/databricks-cli-config.sh'
args: '$(databricks_host) $(databricks_token)'
cwd: '$(System.DefaultWorkingDirectory)'
displayName: "Install and configure the Databricks CLI"
and if you use it on deployment job, by default code is not being checked out there. So you need you need to publish this script as artifact and then download it in deployment job (deployment jobs download artifact by default) or add
- checkout: self
step do download code on deployment job.
I assumed that you use YAML.

Azure DevOps. Reference Pipeline Settings in PowerShell

I'd like to reference a user who ran pipeline and indicate that a previous specific task in multi-stage pipeline ran successfully or not in a PowerShell script. How can I do that?
Something like:
Write-Host $env:UserThatRanPipeline $env:Task:BuildApp1:SuccessOrFail
So I'd get output:
John Smith Success
I'd like to reference a user who ran pipeline and indicate that a
previous specific task in multi-stage pipeline ran successfully or not
in a PowerShell script.
1.Just as Shayki Abramczyk suggests above, you can use Build.RequestedFor to output the user who runs the pipeline. See predefined variables, you can use something like: Write-Host $(Build.RequestedFor)
2.To get status of your AzureRmWebAppDeployment#4 task, for now there's no predefined variable available to do that job. So you have to do that with some logic...
As a workaround:
You can set one variable SuccessOrFail: 'Succeed' like this in yml:
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
SuccessOrFail: 'Succeed'
And then add one powershell task right after your AzureRmWebAppDeployment#4 task:
- task: PowerShell#2
condition: failed()
inputs:
targetType: 'inline'
script: |
# This script will run only when any previous task failed
echo "##vso[task.setvariable variable=SuccessOrFail]Fail"
3.Make sure there's no custom condition set for your AzureRmWebAppDeployment#4 task. As I know this task is not a independent task, so it will run when all previous tasks succeed.
To sum up:
This ps script will run when AzureRmWebAppDeployment#4 task failed or skipped, and it will reset the value of SuccessOrFail to Fail. So if your AzureRmWebAppDeployment#4 succeeds, the value of $(SuccessOrFail) is Succeed, if it fails or skips, the value would be Fail.
Now the value Succeed represents the task should certainly succeed, and if the value is Fail, it actually represents your task is NotSucceed(Fail+Skip).
The order of your tasks should be: other tasks =>AzureRmWebAppDeployment#4=> PS task above=> other tasks => Your own Powershell Task.