简体   繁体   English

从 Azure 管道中的 `az vm run-command` 获取退出代码

[英]Get exit code from `az vm run-command` in Azure pipeline

I'm running a rather hefty build in my Azure pipeline, which involves processing a large amount of data, and hence requires too much memory for my buildagent to handle.我在我的 Azure 管道中运行了一个相当大的构建,它涉及处理大量数据,因此需要太多的 memory 来让我的 buildagent 处理。 My approach is therefore to start up an linux VM, run the build there, and push up the resulting docker image to my container registry.因此,我的方法是启动 linux VM,在那里运行构建,然后将生成的 docker 映像推送到我的容器注册表。

To achieve this, I'm using the Azure CLI task to issue commands to the VM (eg az vm start , az vm run-command... etc).为此,我使用Azure CLI 任务向 VM 发出命令(例如az vm startaz vm run-command...等)。

The problem I am facing is that az vm run-command "succeeds" even if the script that you run on the VM returns a nonzero status code.我面临的问题是az vm run-command “成功”,即使您在 VM 上运行的脚本返回非零状态代码。 For example, this "bad" vm script:例如,这个“坏”的 vm 脚本:

az vm run-command invoke -g <group> -n <vmName> --command-id RunShellScript --scripts "cd /nonexistent/path"

returns the following response:返回以下响应:

{
  "value": [
    {
      "code": "ProvisioningState/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "Enable succeeded: \n[stdout]\n\n[stderr]\n/var/lib/waagent/run-command/download/87/script.sh: 1: cd: can't cd to /nonexistent/path\n",
      "time": null
    }
  ]
}

So, the command succeeds, presumably because it succeeded in executing the script on the VM.所以,命令成功了,大概是因为它成功地在虚拟机上执行了脚本。 The fact that the script actually failed on the VM is buried in the response "message"脚本实际上在 VM 上失败的事实隐藏在响应“消息”中

I would like my Azure pipeline task to fail if the script on the VM returns a nonzero status code.如果 VM 上的脚本返回非零状态代码,我希望我的 Azure 管道任务失败。 How would I achieve that?我将如何实现这一目标?

One idea would be to parse the response (somehow) and search the text under stderr - but that sounds like a real hassle, and I'm not sure even how to "access" the response within the task.一个想法是解析响应(以某种方式)并在stderr下搜索文本 - 但这听起来很麻烦,我什至不确定如何“访问”任务中的响应。

Have you enabled the option " Fail on Standard Error " on the Azure CLI task?您是否在 Azure CLI 任务上启用了“标准错误失败”选项? If not, you can try to enable it and run the pipeline again to see if the error " cd: can't cd to /nonexistent/path " can make the task run failed.如果没有,您可以尝试启用它并再次运行管道,看看错误“ cd: can't cd to /nonexistent/path ”是否会使任务运行失败。

在此处输入图像描述

If the task still is passed, the error " cd: can't cd to /nonexistent/path " should not be a Standard Error.如果任务仍然通过,错误“ cd: can't cd to /nonexistent/path ”不应该是标准错误。 In this situation, you may need to add more command lines in your script to monitor the output logs of the az command.在这种情况下,您可能需要在脚本中添加更多命令行来监控az命令的 output 日志。 Once there is any output message shows error, execute " exit 1 " to exit the script and return a Standard Error to make the task be failed.一旦有任何 output 消息显示错误,执行“ exit 1 ”退出脚本并返回标准错误以使任务失败。

I solved this by using the SSH pipeline task - this allowed me to connect to the VM via SSH, and run the given script on the machine "directly" via SSH.我通过使用SSH 管道任务解决了这个问题 - 这允许我通过 SSH 连接到 VM,并通过 SSH 在机器上“直接”运行给定的脚本。

This means from the context of the task, you get the status code from the script itself running on the VM.这意味着从任务的上下文中,您可以从在 VM 上运行的脚本本身获取状态代码。 You also see any console output inside the task logs, which was obscured when using az vm run-command .您还会在任务日志中看到任何控制台 output,在使用az vm run-command时会被遮挡。

Here's an example:这是一个例子:

  - task: SSH@0
    displayName: My VM script
    timeoutInMinutes: 10
    inputs:
      sshEndpoint: <sshConnectionName> 
      runOptions: inline
      inline: |
        echo "Write your script here" 

Not that the SSH connection needs to be set up as a service connection using the Azure pipelines UI.并不是说需要使用 Azure 管道 UI 将 SSH 连接设置为服务连接 You reference the name of the service connection you set up in yaml.您引用您在 yaml 中设置的服务连接的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM