GitHub工作流中调用同目录PS1脚本失败的技术求助
问题场景
在GitHub多阶段流水线中,调用.ps1脚本时能正常找到该脚本,但它调用同一文件夹下的另一个.ps1脚本时,出现“脚本或cmdlet未找到”异常。本地执行脚本一切正常,怀疑与脚本的上下文目录设置有关。
相关代码片段
GitHub工作流
上传工件阶段(供后续阶段使用)
create-pipeline-artifacts: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Upload bicep files as pipeline artifact uses: actions/upload-artifact@v3 with: name: bicep path: deployment/bicep - name: Upload powershell scripts as pipeline artifact uses: actions/upload-artifact@v3 with: name: powershell path: deployment/powershell - name: Upload integrations as pipeline artifact uses: actions/upload-artifact@v3 with: name: integrations path: integrations
下载工件并执行脚本阶段
Deploy-Integrations: runs-on: ubuntu-latest if: always() environment: ${{ inputs.environment }} steps: - name: Azure Login uses: azure/login@v1 with: creds: '${{ secrets.AZURE_CREDENTIALS }}' enable-AzPSSession: true - name: Download powershell files from pipeline artifact uses: actions/download-artifact@v3 with: name: powershell path: ./artifacts/powershell - name: Download integration files uses: actions/download-artifact@v3 with: name: integrations path: ./artifacts/integrations - name: Deploy integrations uses: azure/powershell@v1 with: inlineScript: ./artifacts/powershell/deploy-logicapp-folder.ps1 -environmentAcronym ${{ inputs.environmentAcronym }} -location ${{ inputs.region }} -rootFolder './artifacts/integrations/' azPSVersion: "latest"
PowerShell脚本
deploy-logicapp-folder.ps1
param ($rootFolder, $location, $environmentAcronym) $locationAcronym = 'weu' Get-ChildItem -Path $rootFolder -Recurse -Filter *.definition.json | ForEach-Object { # 异常发生位置 & ./deploy-logicapp.ps1 -logicAppDefinitionFile $_.FullName -location $location -locationAcronym $locationAcronym -environmentAcronym $environmentAcronym }
deploy-logicapp.ps1
param ($logicAppDefinitionFile, $location, $environmentAcronym, $locationAcronym) ### 设置逻辑应用名称 $logicAppName = Split-Path $logicAppDefinitionFile -leaf $logicAppName = $logicAppName.Replace('.definition.json', '') $logicAppName = "$locationAcronym-$environmentAcronym-int-la-$logicAppName" # 其余代码省略
实际错误信息
Run ./artifacts/powershell/deploy-logicapp-folder.ps1 -environmentAcronym dev -location westeurope -rootFolder './artifacts/integrations/' ForEach-Object: /home/runner/work/backend/backend/artifacts/powershell/deploy-logicapp-folder.ps1:21 Line | 21 | ForEach-Object *** | ~~~~~~~~~~~~~~~~ | The term './deploy-logicapp.ps1' is not recognized as a name of a | cmdlet, function, script file, or executable program. Check the spelling | of the name, or if a path was included, verify that the path is correct | and try again. Error: Process completed with exit code 1.
解决方案
问题根源是PowerShell执行脚本时的工作目录并非脚本所在目录。在GitHub Actions环境中,执行deploy-logicapp-folder.ps1时,工作目录是仓库根目录(/home/runner/work/backend/backend/),而非脚本所在的./artifacts/powershell/,所以用相对路径./deploy-logicapp.ps1会找不到文件。
有两种可靠修复方式:
方式一:使用脚本内置目录变量调用
修改deploy-logicapp-folder.ps1,利用PowerShell内置变量$PSScriptRoot获取当前脚本所在目录,再拼接目标脚本路径:
param ($rootFolder, $location, $environmentAcronym) $locationAcronym = 'weu' # 获取当前脚本所在的绝对目录 $scriptDir = $PSScriptRoot Get-ChildItem -Path $rootFolder -Recurse -Filter *.definition.json | ForEach-Object { # 用脚本目录拼接目标脚本的绝对路径 & "$scriptDir/deploy-logicapp.ps1" -logicAppDefinitionFile $_.FullName -location $location -locationAcronym $locationAcronym -environmentAcronym $environmentAcronym }
$PSScriptRoot不受当前工作目录影响,始终指向脚本文件所在的目录,是最稳妥的方式。
方式二:先切换工作目录再执行脚本
在GitHub Actions的inlineScript中,先切换到脚本所在目录,再执行主脚本,注意同步调整其他路径的相对引用:
- name: Deploy integrations uses: azure/powershell@v1 with: inlineScript: | cd ./artifacts/powershell ./deploy-logicapp-folder.ps1 -environmentAcronym ${{ inputs.environmentAcronym }} -location ${{ inputs.region }} -rootFolder '../../artifacts/integrations/' azPSVersion: "latest"
如果担心相对路径出错,也可以直接使用绝对路径指定rootFolder。
内容的提问来源于stack exchange,提问作者Sam Vanhoutte
相关产品推荐
相关产品推荐

