'Azure Pipeline: Bash script fails to run when running a self hosted agent as a service

I would like to run a self hosted agent on Windows 10 to build a project in a pipeline using a local compiler.

I was able to create the following YAML file to call the compiler:

- task: Bash@3
  displayName: BuildProject
  inputs:
    targetType: 'inline'
    script: |
      path=""

      # Search for the project file
      while IFS= read -r -d $'\0' file; do
        path=$(readlink -f "$file")
        path="${path/"/mnt/c/"/"C:/"}"
      done < <(find . -maxdepth 2 -name "*.apj" -print0)

      # Compile the project
      if [[ "$path" == *".apj" ]]; then
        echo "Starting build process of \"$path\"..."
        /mnt/c/path/to/compiler/Build.exe $"\"$path\"" -all
        result=$?
        if [[ $result -lt 3 ]]; then
          exit 0
        else
          echo "Build failed!"
          exit $result
        fi
      else
        echo "Project not found!"
        exit 2
      fi

When I start the agent with the .\run command in PowerShell and run the pipeline, this script works fine and I get over 900 lines of output, which looks something like this:

Starting build process of "C:/agent/_work/2/s/MyProject.apj"...
------------- Building project MyProject, configuration Main -------------
Analyzing project ...
Generating header files ...
WidgetLibrary 'Keyboards' was built successfully.
WidgetLibrary 'Keyboards' was built successfully.
No stored online configurations found. Adding default connections.
Delete directory 'C:\agent\_work\2\s\Temp\Objects\Main\Main_X20CP0483_00\.mappView\data'.
Looking for stylessets duplicates.
Looking for themes duplicates.
Looking for styles duplicates.
Looking for bindingsListsSets duplicates.
Merge mapping files into file 'merged.expressiontype'.
Start to copy files.
...

The compiler finishes after approx. 4 minutes with the return code 1 which means that there were some warnings but no errors.
This is exactly what I expected.

Sounds great so far...
But here is the problem.

If I set up my agent to run as a service, the compiler stops after a few seconds and gives return code 3.
Here is the output I get:

Starting build process of "C:/agent/_work/2/s/MyProject.apj"...
------------- Building project MyProject, configuration Main -------------
Build: 0 error(s), 0 warning(s)

Build failed!
##[error]Bash exited with code '3'.

It looks like the bash script is not waiting for the compiler to finish and stops the process immediately.
Has anyone an idea why this happens when I setup the agent to run as a service but works fine when starting it manually?

Other bash scripts that only gather some information about the project (e.g. current version, onfiguration name, etc.), which run before I start the compiler, work fine.

I tried to call the compiler in a function such as:

cmd() {
  /mnt/c/path/to/compiler/Build.exe $"\"$path\"" -all
}
cmd

But this did not work either...

I also tried to call the compiler in a seperate task of the pipeline:

- task: BatchScript@1
  inputs:
    filename: 'C:\path\to\compiler\Build.exe'
    arguments: '$(BuildInfo.path) -all'

This worked but the pipeline still fails because I get the return code 1 from the compiler...

Does anyone know how I can get the bash script to wait for the compiler to finish when I run the agent as a service?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source