azureazure-devops

Azure CLI run-command invoke runshellscript - How to get output of shell script


I am running a command in an azure pipeline

az vm run-command invoke --command-id RunShellScript --name DevServer -g Takeoffs --scripts "cd /home/user/workdir/project/ && bash ./buildDev.sh > /home/user/workdir/project/exlogs.txt"

This shell script has different exit codes for different errors.

I would like to be able to catch them.

It sounds like the output I can catch is the output of the "az vm run-command invoke", but the exit codes of the actual shell script are lost.

How can I actually get the exit code?

I tried getting the command output with something like this:

command_output=$(az vm run-command invoke --command-id RunShellScript --name AIDevServer -g Takeoffs --scripts "cd /home/aiadmin/workdir/wall_classification/ && bash ./buildDev.sh > /home/aiadmin/workdir/wall_classification/exlogs.txt" -o json)
echo "Command output: $command_output"

But this only gives the output of the az vm run-command command, and not the underlying shell script exit code.


Solution

  • I can reproduce the same situation. When using the command: az vm run-command invoke, it only shows the command result message without the exit code.

    For example:

    enter image description here

    This is a limitation from the azure cli itself.

    I am afraid that the az vm run-command invoke command doesn't support output the exit code of the command result.

    Here is a suggestion ticket in Github: Support exit code reporting in az run-command invoke You can monitor the suggestion ticket and add comment to share your ideas.

    For a workaround, you can use SSH task in azure pipeline to run the shell script in Azure VM.

    It will use SSH to connect to Azure VM to run a shell script. And you can see the exit code.

    For example:

    - task: SSH@0
      inputs:
        sshEndpoint: 'test0405ssh'
        runOptions: 'inline'
        inline: |
          cd /home/aiadmin/workdir/wall_classification/ && bash ./buildDev.sh > /home/aiadmin/workdir/wall_classification/exlogs.txt
          echo $?
        readyTimeout: '20000'
    

    Result:

    enter image description here