Azure Devops yaml 管道 - PowerShell 文件移动,然后提交

Azure Devops yaml Pipeline - PowerShell file move, then commit

我能够在我的 Azure DevOps yaml 管道中移动文件(使用 PowerShell)。

我能够通过 Azure DevOps yaml 管道 提交 到我的 git 存储库。 (As found here)

问题是我不能在同一管道中同时执行这两项操作。移动文件工作正常,直到我在嵌套模板中添加以下命令 (gitCalled.yml):

steps:
- checkout: self
  persistCredentials: true

- script: |
   git config --global user.email Continuous.Integrator@yourcompany.com & git config --global user.name "Continuous.Integrator"
  workingDirectory: $(System.DefaultWorkingDirectory)

- script: |
   git checkout -b packaging-test
   echo 'This is a test' > data.txt
   git add -A
   git commit -m "deployment $(Build.BuildNumber)"
   git push --set-upstream origin packaging-test 
  displayName: Add data.txt file
  workingDirectory: $(System.DefaultWorkingDirectory)

具体来说,checkout/persistCredentials 命令 似乎是问题所在。结果,当使用它时,我的 powershell 文件再也找不到了。如果我注释掉该命令,我的 git 命令会失败,但会找到 powerShell 文件并且 运行.

我处于非此即彼的境地,但我两者都想做 ;)

调用模板如下:

parameters:
  - name: aliasPackageName
    type: string
  
stages: 
  - stage: PipelineMoveFiles
    displayName: Pipeline
    jobs:
    - job: MoveFiles
      displayName: MoveFiles
      pool:
        vmImage: ubuntu-latest
      steps:
        
        - pwsh: |
            $numOfForceAppFiles = $(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1 
            Write-Host "##vso[task.setvariable variable=numOfForceAppFiles;isOutput=true]$numOfForceAppFiles"
          name: movesFilesStep
          displayName: Move files to their package folders
        
        - pwsh: |
            Write-Host "Number of files still to move: "$(movesFilesStep.numOfForceAppFiles)
            if ( 0 -eq $numOfForceAppFiles )
            {
                Write-Host "All Files were moved"
            }
            else
            {
              Write-Host "All Files were NOT moved"
              # exit 1;
            } 
          displayName: Validate that all files were moved

        - template: gitCalled.yml

错误:

Starting: Move files to their package folders
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.200.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/33de9584-b260-4422-b6db-b8cef3ea129d.ps1'
/home/vsts/work/1/s/PackageCreation/MoveFiles.ps1: /home/vsts/work/_temp/33de9584-b260-4422-b6db-b8cef3ea129d.ps1:2
Line |
   2 |  … umOfForceAppFiles = /home/vsts/work/1/s/PackageCreation/MoveFiles.ps1
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The term '/home/vsts/work/1/s/PackageCreation/MoveFiles.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]PowerShell exited with code '1'.
Finishing: Move files to their package folders

任何帮助将不胜感激!!

根据您的 YAML 示例,您需要在 PowerShell 内联脚本中执行 Powershell 文件并获取输出。

您可以使用脚本执行ps文件:

$numOfForceAppFiles = & "$(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1" 

参考以下示例:

parameters:
  - name: aliasPackageName
    type: string
  
stages: 
  - stage: PipelineMoveFiles
    displayName: Pipeline
    jobs:
    - job: MoveFiles
      displayName: MoveFiles
      pool:
        vmImage: ubuntu-latest
      steps:
        
        - pwsh: |
       $numOfForceAppFiles = & "$(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1" 
       
       Write-Host "##vso[task.setvariable variable=numOfForceAppFiles;isOutput=true]$numOfForceAppFiles"
          displayName: Move files to their package folders

我决定将它们放在一起,而不是 运行 不同步骤(pwsh 和脚本)中的 powershell 和 git 命令。以下是部分但有效的代码。

steps:
- checkout: self
  persistCredentials: true
  clean: true

- powershell: |
   git --version
   git config user.email Continuous.Integrator@yourcompany.com
   git config user.name "Continuous.Integrator"   
   
   git checkout -b packaging-test
   $numOfForceAppFiles = $(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1
   git add -A
   git commit -m "deployment $(Build.BuildNumber)"
   git push --set-upstream origin packaging-test