Azure DevOps pwsh 和 PowerShell@2 在 Docker 镜像内无法执行。

huangapple go评论74阅读模式
英文:

Azure DevOps pwsh and PowerShell@2 fail to execute inside docker image

问题

在我们的组织中,我们正在使用Azure虚拟机规模集(VMSS),如下图所示的规格,用作我们DevOps流水线的自托管代理。我们正在在拉入VMSS的Docker容器中运行所有DevOps流水线作业。这些作业正在正常执行,除了那些包含PowerShell任务的作业,无论是作为pwsh还是PowerShell@2,它们都会失败并显示以下错误:

##[error]无法找到可执行文件:'pwsh'。请验证文件路径是否存在,或者文件是否可以在PATH环境变量指定的目录中找到。还请检查文件模式以验证文件是否可执行。

VMSS的规格如下:

Azure DevOps pwsh 和 PowerShell@2 在 Docker 镜像内无法执行。

以下是错误的屏幕截图:

Azure DevOps pwsh 和 PowerShell@2 在 Docker 镜像内无法执行。

然而,我用于运行作业的Docker镜像中已经安装了PowerShell。以下是Docker镜像文件:

FROM python:3.10-slim
ENV TERRAFORM_VERSION=1.0.4
# 其他环境变量...

USER 0
HEALTHCHECK NONE

# 安装软件包...
# 安装PowerShell...
# 安装其他工具...

################################
# 安装Azure CLI
################################
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash

正如您在Docker镜像中看到的,PowerShell已安装,但作业仍然引发错误。以下是导致失败的DevOps流水线作业:

pool: my-VMSS-devops-pool
resources:
  containers:
  - container: base
    image: mycontainerregistry.azurecr.io/black/base-image:1.1.0
    endpoint: 'service-connection-endpoint-to-ACR'

trigger:
  branches:
    include:
      - "main"
      - "feature/*"
## 一些变量在这里
stages:
  - stage: Build
    displayName: 'Build Stage'

    jobs:
    - job: SomeJob
      steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            pwsh: true
            script:
              Write-Host "$(deployToDevPipelineLvl) - ${{variables.deployToDevPipelineLvl}}"
              Write-Host "$(deployToQAPipelineLvl) - ${{variables.deployToQAPipelineLvl}}"
              Write-Host "$(deployToProdPipelineLvl) - ${{variables.deployToProdPipelineLvl}}"
          displayName: pwsh check var values

    - job: build_publish_whlPackageName
      steps:
        - task: UsePythonVersion@0
          displayName: Use Python $(pythonVersion)
          inputs:
            versionSpec: $(pythonVersion)

## 流水线的其余部分

task: PowerShell@2是引发错误的部分。请注意,我尝试过pwsh,但它也引发了相同的错误。

- pwsh: |
    ## 代码在此处

简而言之,问题是如果问题出现在Docker镜像方面,我应该如何更改Docker镜像以解决错误,并能够在自托管VMSS中的Docker容器中执行Azure DevOps PowerShell任务?

英文:

Background Setup:In our organization we are using Azure virtual machine scale sets (VMSS), specification is shown in image below: to use as self hosted agents for our DevOps pipelines. We are running all our DevOps pipeline jobs in docker containers pulled into the VMSS. The jobs are executing properly, except for the ones which have powershell task, either as pwsh or PowerShell@2. Both of them are failing with the following error:

##[error]Unable to locate executable file: 'pwsh'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.

Spec of the VMSS:

Azure DevOps pwsh 和 PowerShell@2 在 Docker 镜像内无法执行。

Here is the screen shot of the error:

Azure DevOps pwsh 和 PowerShell@2 在 Docker 镜像内无法执行。

The docker image I am using to run the job inside however, has powershell installed. Below is the docker image file:

FROM python:3.10-slim
ENV TERRAFORM_VERSION=1.0.4
ENV KUBECTL_VERSION=v1.25.2
ENV HELM_VERSION=v3.10.0
ENV PIP_VERSION=22.3
ENV CHECKOV_VERSION=2.1.270
ENV YQ_VERSION=3.1.0
ENV TZ=UTC
ENV DEBIAN_FRONTEND=noninteractive

USER 0
HEALTHCHECK NONE

# install packages
RUN apt-get update && apt-get install -yq --no-install-recommends \
    git \
    curl \
    unzip \
    jq \
    wget \
# install versioned terraform, kubectl and helm binary
    && curl -sLO https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
    && unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
    && mv terraform /usr/local/bin/ \
    && chmod +x /usr/local/bin/terraform \
    && curl -sLO https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl \
    &&  chmod +x ./kubectl \
    && mv ./kubectl /usr/local/bin \
    && curl -sLO https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz \
    && tar -xzf helm-${HELM_VERSION}-linux-amd64.tar.gz \
    && mv linux-amd64/helm /bin/helm && rm -rf linux-amd64 \
## install powershell    
    && wget -q https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb  \
    && apt-get update && apt-get install -y powershell \
#     pwsh Register-PSRepository \
# install checkov and yq via pip
    && pip3 install --no-cache-dir checkov==${CHECKOV_VERSION} \
    && pip3 install --no-cache-dir yq==${YQ_VERSION} \
# cleanup
    && apt-get autoremove --purge -y \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf *.zip *.gz


################################
# Install Azure CLI
################################
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash

As you see in docker image powershell is installed, but still the job is causing error. Below I show the DevOps pipeline job that is causing failure:

pool: my-VMSS-devops-pool
resources:
  containers:
  - container: base
    image: mycontainerregistry.azurecr.io/black/base-image:1.1.0
    endpoint: 'service-connection-endpoint-to-ACR'

trigger:
  branches:
    include:
      - "main"
      - "feature/*"
## Some variables here
stages:
  - stage: Build
    displayName: 'Build Stage'

    jobs:
    - job: SomeJobe
      steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            pwsh: true
            script:
              Write-Host "$(deployToDevPipelineLvl) - ${{variables.deployToDevPipelineLvl}}"
              Write-Host "$(deployToQAPipelineLvl) - ${{variables.deployToQAPipelineLvl}}"
              Write-Host "$(deployToProdPipelineLvl) - ${{variables.deployToProdPipelineLvl}}"
          displayName: pwsh check var values

    - job: build_publish_whlPackageName
      steps:
        - task: UsePythonVersion@0
          displayName: Use Python $(pythonVersion)
          inputs:
            versionSpec: $(pythonVersion)

## The rest of the pipeline

The task: PowerShell@2 is the part that is causing the error. Please note that I tried "pwsh" but it is also raising the same error.

- pwsh: |
    ## Code goes here

In short the question is how should I make changes to the docker image, if the problem is from the docker image side, to resolve the error and be able to execute Azure DevOps powershell tasks inside a docker container in a self-hosted VMSS?

答案1

得分: 1

看起来你在作业部分缺少容器定义:

jobs:
    - job: SomeJobe
      container:
        image: mycontainerregistry.azurecr.io/black/base-image:1.1.0
        endpoint: 'service-connection-endpoint-to-ACR'
      steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            pwsh: true
            script:
              Write-Host "$(deployToDevPipelineLvl) - ${{variables.deployToDevPipelineLvl}}"
              Write-Host "$(deployToQAPipelineLvl) - ${{variables.deployToQAPipelineLvl}}"
              Write-Host "$(deployToProdPipelineLvl) - ${{variables.deployToProdPipelineLvl}}"
          displayName: pwsh check var values

jobs:
  - job: Run_Prod
    displayName: Build_Prod
    container:
      image: ${{ variables.buildContainerUrl }}
      endpoint: ${{ variables.containerRegistryServiceConnection }}
      options: --user 0:0
英文:

It seems you are missing the container definition in the jobs section:

jobs:
    - job: SomeJobe
      container:
        image: mycontainerregistry.azurecr.io/black/base-image:1.1.0
        endpoint: 'service-connection-endpoint-to-ACR'
      steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            pwsh: true
            script:
              Write-Host "$(deployToDevPipelineLvl) - ${{variables.deployToDevPipelineLvl}}"
              Write-Host "$(deployToQAPipelineLvl) - ${{variables.deployToQAPipelineLvl}}"
              Write-Host "$(deployToProdPipelineLvl) - ${{variables.deployToProdPipelineLvl}}"
          displayName: pwsh check var values

jobs:  
  - job: Run_Prod
    displayName: Build_Prod
    container:
      image: ${{ variables.buildContainerUrl }}
      endpoint: ${{ variables.containerRegistryServiceConnection }}
      options: --user 0:0

huangapple
  • 本文由 发表于 2023年5月24日 18:55:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定