4
votes

I'm trying to run a cypress test in an azure pipeline. In order to do that I need to run 'dotnet run' before I run the test. I have a pipeline task that successfully runs the 'dotnet run' command but because it never exits, the pipeline never gets to the next task. Is there a way using YAML to make the pipeline job go to the next step while keeping the 'dotnet run' command running?

If that isn't possible is there a way to run 'dotnet run' in the background while running a cypress test in an Azure pipeline?

Here's part of my YAML code:

- task: DotNetCoreCLI@2
  displayName: 'dotnet run'
  inputs:
        command: run
        projects: Api/Api.csproj
        arguments: '--configuration Release --verbosity normal'

- script: 'npx cypress verify' // pipeline job never gets to this step
    displayName: 'Verify Cypress Can Run'
    failOnStderr: true
3
Having a single job with tasks tied together cannot achieve what you intend to have. Think of making two jobs instead of onekoushik

3 Answers

1
votes

Azure Devops task won't navigate to next task unless the current task has completed its job.

Since the dotnet run task is running one xx.exe/api/... that keeps running and won't stop, it's expected behavior that your task will hang there.

Is there a way using YAML to make the pipeline job go to the next step while keeping the 'dotnet run' command running?

As a workaround you can use cmd task to run the command. Instead of calling the command directly in CMD task, we should call the CMD task to open new session to run the dotnet run command. So that the job will go to next job and the dotnet run command can keep running in the background.

trigger:
- master

pool:
  name: default

steps:
- script: 'start cmd.exe /c dotnet run WpfCore.csproj'
  displayName: 'Open new Session to run dotnet run'

- script: 'TaskList'
  displayName: 'List running tasks to check if the WpfCore.exe keeps running'

enter image description here

Note:

Since the project I used for test is a Wpf app with one timer, I have to use a self-hosted agent with interactive mode. If the workaround above can't work for you when you're using microsoft hosted agent, you may need to consider using self-hosted agent with appropriate mode. (interactive or service mode)

1
votes

I came up with a solution. I put 'dotnet run' in a powershell script that runs as a background process.

The powershell script:

Start-Process powershell -ArgumentList "-NoProfile", "-NonInteractive", "-NoExit", {
    dotnet run
}

Then I run the powershell script in a powershell build step:

- task: PowerShell@2
    inputs:
      targetType: 'filePath' # Optional. Options: filePath, inline
      filePath: $(Build.SourcesDirectory)\Api\dotnetrun.ps1
      errorActionPreference: 'stop'
      failOnStderr: false # Optional
      workingDirectory: ./Api
      displayName: 'Run dotnet run as background process'

The build step runs and goes to the next step as desired.

0
votes

You can also run this with a linux agent / using the bash task: dotnet run &

Source: https://linuxize.com/post/how-to-run-linux-commands-in-background/