我在使用自托管 Windows 代理时遇到以下错误。
##[错误]无法找到可执行文件:'bash'。
问题的原因是 Bash 不是 Windows 机器的内置工具。
为了解决这个问题,我们可以安装 git bash 并在代理机器的环境变量 PATH 中添加 bash 命令文件夹。
如何在下面提到的管道中添加任务或阶段来解决此错误
要将所有步骤集成到 Pipeline 中,您可以添加以下任务:
steps:
- task: PowerShell@2
displayName: Install Git And Config PATH ENV
inputs:
targetType: 'inline'
script: |
# Define the URL of the Git installer
$gitInstallerUrl = "https://github.com/git-for-windows/git/releases/download/v2.40.1.windows.1/Git-2.40.1-64-bit.exe"
# Define the path to save the installer
$installerPath = "$env:TEMP\Git-2.40.1-64-bit.exe"
# Download the Git installer
Invoke-WebRequest -Uri $gitInstallerUrl -OutFile $installerPath
# Run the installer silently
Start-Process -FilePath $installerPath -ArgumentList "/SILENT", "/NORESTART" -Wait
# Optionally, remove the installer after installation
Remove-Item -Path $installerPath
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
echo $env:Path
$gitpath = git --exec-path
$bashpath = $gitpath.Replace('mingw64/libexec/git-core','bin')
echo "##vso[task.setvariable variable=PATH]$bashpath;$(PATH)"
- task: Bash@3
inputs:
targetType: 'inline'
script: |
echo "test"
在这种情况下,我们可以使用 Windows 自托管代理中的 Bash 任务。
另一方面,从您的 YAML 示例中,您正在使用 bash 任务来设置 Pipeline 变量。
您也可以考虑改为使用PowerShell任务来实现相同的功能。在这种情况下,您不需要在 Pipeline 中配置 bash 环境变量。
例如:
- powershell: |
echo "##vso[task.setvariable variable=xxxx-outcome;isOutput=true]$(result)"
name: outputForDashboard
condition: always()
更新:
由于 git bash 存在于您的本地计算机上(
C:\Program Files\Git\bin
),您可以添加以下 powershell 任务来设置 env 变量。
steps:
- task: PowerShell@2
displayName: Install Git And Config PATH ENV
inputs:
targetType: 'inline'
script: |
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
echo $env:Path
$bashpath = "C:\Program Files\Git\bin"
echo "##vso[task.setvariable variable=PATH]$bashpath;$(PATH)"
- task: Bash@3
inputs:
targetType: 'inline'
script: |
echo "test"
更新2:
resources:
pipelines:
- pipeline: xxx
source: xxx
project: xxx
trigger:
stages:
- Deploy_xxx
xxx
variables:
- name: dashboardFileDirectory
value: "C:\\Users\\svcAzurexxx\\test-team-xxxfiles"
- name: setJava
value: "false"
stages:
- stage: Test1 #no whitespace
displayName: xxx
jobs:
- job: BuildAndTest
pool:
name: "Test"
displayName: 'Build and Test'
steps:
- task: PowerShell@2
displayName: Config PATH ENV
inputs:
targetType: 'inline'
script: |
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
echo $env:Path
$bashpath = "C:\Program Files\Git\bin"
echo "##vso[task.setvariable variable=PATH]$bashpath;$(PATH)"
- template: templates/test- Automation.yml
parameters:
mvnCommand: mvn clean test Automation"
pipelineArtifactName: Automation-API
- bash: |
echo "##vso[task.setvariable variable= Automation-API-outcome;isOutput=true]$(result)"
name: outputForDashboard
condition: always()
# run the report creation in a new job on microsoft hosted agents
- job: allureReport
pool: "Azure Pipelines"
dependsOn: BuildAndTest
condition: always()
steps:
- template: templates/ Automation.yml
parameters:
pipelineArtifactName: Automation-API
reportName: Automation-Test-Report
- stage: SaveDetailsForDashboard
displayName: Save details for dashboard
pool: "Test Automation"
dependsOn:
- Test1
condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
jobs:
- job: SaveDetails
displayName: Save details
variables:
Automation-API-outcome: $[stageDependencies.Test1.BuildAndTest.outputs['outputForDashboard. Automation-API-outcome']]
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
echo "$( Automation-API-outcome)" | Out-File -FilePath $(dashboardFileDirectory)\ Automation-API-outcome