Home > Net >  Azure Devops yaml Pipeline - PowerShell file move, then commit
Azure Devops yaml Pipeline - PowerShell file move, then commit

Time:03-10

I am able to move files (using PowerShell) in my Azure DevOps yaml pipeline.

I am able to commit to my git repo via Azure DevOps yaml pipeline. (As found here)

The problem is that I cannot do both in the same pipeline. Moving files worked fine until I added the following commands in a nested template (gitCalled.yml):

steps:
- checkout: self
  persistCredentials: true

- script: |
   git config --global user.email [email protected] & 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)

Specifically, the checkout/persistCredentials command is what appears to be the issue. The result, when using it, is that my powershell file is no longer able to be found. If I comment out that command, my git commands fail, but the powerShell file is found and ran.

I'm in an either-or situation, but I want to do both ;)

Here is the calling template:

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

ERROR:

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

Any help would be much appreciated!!

CodePudding user response:

Based on your YAML sample, you need to execute the Powershell file in PowerShell Inline script and get the output.

You can use the script to execute the ps file:

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

Refer to the following sample:

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

CodePudding user response:

Instead of running the powershell and git commands in different steps (pwsh and script) I decided to roll them together. The following is a partial, but working code.

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

- powershell: |
   git --version
   git config user.email [email protected]
   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 
  • Related