当前操作系统无法执行 运行 此任务

The current operating system is not capable of running this task

在 Azure DevOps 管道中,在 docker 上使用自托管 linux 构建代理 运行,我在 terraform 计划中收到以下错误。我尝试了很多东西,甚至 运行 构建代理上 bash shell 的步骤:效果很好。

你有什么建议吗?

2019-09-12T13:55:21.8133489Z ##[debug]Evaluating condition for step: 'Terraform plan'
2019-09-12T13:55:21.8134075Z ##[debug]Evaluating: succeeded()
2019-09-12T13:55:21.8134246Z ##[debug]Evaluating succeeded:
2019-09-12T13:55:21.8134443Z ##[debug]=> True
2019-09-12T13:55:21.8134723Z ##[debug]Result: True
2019-09-12T13:55:21.8134976Z ##[section]Starting: Terraform plan
2019-09-12T13:55:21.8138406Z ==============================================================================
2019-09-12T13:55:21.8138526Z Task         : Run Terraform
2019-09-12T13:55:21.8138605Z Description  : Run a Terraform on the build agent
2019-09-12T13:55:21.8138647Z Version      : 2.4.0
2019-09-12T13:55:21.8138688Z Author       : Peter Groenewegen - Xpirit
2019-09-12T13:55:21.8138772Z Help         : [More Information](https://pgroene.wordpress.com/2016/06/14/getting-started-with-terraform-on-windows-and-azure/)
2019-09-12T13:55:21.8138828Z ==============================================================================
2019-09-12T13:55:21.8347594Z ##[error]The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell.
2019-09-12T13:55:21.8361383Z ##[debug]System.Exception: The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell.
   at Microsoft.VisualStudio.Services.Agent.Worker.TaskRunner.RunAsync()
   at Microsoft.VisualStudio.Services.Agent.Worker.StepsRunner.RunStepAsync(IStep step, CancellationToken jobCancellationToken)
2019-09-12T13:55:21.8365066Z ##[section]Finishing: Terraform plan

管道在 "terraform plan" 失败。这是 yml:

variables:
  env: 'environment'

steps:
- task: petergroenewegen.PeterGroenewegen-Xpirit-Vsts-Release-Terraform.Xpirit-Vsts-Release-Terraform.Terraform@2
  displayName: 'Terraform plan'
  inputs:
    TemplatePath: '$(System.DefaultWorkingDirectory)/_repository/tf'
    Arguments: 'plan -var-file=$(System.DefaultWorkingDirectory)/_repository/tf/$(env)/$(env).tfvars  '
    InstallTerraform: true
    UseAzureSub: true
    ConnectedServiceNameARM: 'deploy-sco'
    ManageState: true
    SpecifyStorageAccount: true
    StorageAccountResourceGroup: 'rg-terraform'
    StorageAccountRM: sta
    StorageContainerName: terraform
    InitArguments: '-backend-config=$(System.DefaultWorkingDirectory)/_repository/tf/$(env)/$(env).beconf'

构建代理是一个 docker 容器,使用以下 docker 文件

构建
FROM ubuntu:16.04
ENV DEBIAN_FRONTEND=noninteractive
RUN echo "APT::Get::Assume-Yes \"true\";" > /etc/apt/apt.conf.d/90assumeyes
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl jq git iputils-ping libcurl3 libunwind8 netcat libssl-dev unzip wget apt-utils apt-transport-https make binutils gcc lsb-release gnupg
RUN wget -P /tmp/download https://releases.hashicorp.com/terraform/0.12.7/terraform_0.12.7_linux_amd64.zip
RUN wget -P /tmp/download -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
RUN unzip /tmp/download/terraform_0.12.7_linux_amd64.zip -d /tmp/download/
RUN mv /tmp/download/terraform /usr/local/bin
RUN chmod a+x /usr/local/bin/terraform
RUN apt-get install /tmp/download/packages-microsoft-prod.deb
RUN apt-get update
RUN apt-get -y install powershell
RUN wget -P /tmp/download -q https://curl.haxx.se/download/curl-7.65.3.tar.gz
RUN cd /tmp/download; tar xzf curl-7.65.3.tar.gz
RUN cd /tmp/download/curl-7.65.3; ./configure --prefix=/opt/curl-7.65.3 --disable-ipv6 --with-ssl; make; make install
RUN mv /usr/bin/curl /tmp; ln -s /opt/curl-7.65.3/bin/curl /usr/bin/curl
RUN curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null
RUN AZ_REPO=$(lsb_release -cs); echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | tee /etc/apt/sources.list.d/azure-cli.list
RUN apt-get update
RUN apt-get install azure-cli
RUN pwsh -c "Install-Module -Name Az -Force"
RUN pwsh -c "Install-Module -Name Azure -Force"

WORKDIR /azp

COPY ./start.sh .
RUN chmod +x start.sh

CMD ["./start.sh"]

正如您所说,是的,Terraform 计划可以兼容包括 linux、windows 和 MacOS。但是现在,您面临的问题是您正在使用的名为 Run Terraform 的扩展和任务只能在安装在 Windows 上的代理中执行。您可以查看其文档以了解:Getting started with Terraform on Windows and Azure.

还有另一个由我们的 Microsoft DevLabs 和个人开发人员创建的扩展:Terraform and Terraform Build & Release Tasks。这两个扩展中的任务都可以在 windows、linux 和 macOS 中编译。

你最好改用这两个扩展中的任务。