0

My Yaml Azure Devops pipeline failed when running a script.

Situation

I have this script in the Tuto-BuildDeploy repository:

trigger:
- none
 
pool:
  vmImage: windows-latest

resources:
  repositories:
    - repository: TutoDeploy
      ref: main
      type: git
      name: Tuto-Deploy

jobs:
 - job: checkout
   steps:
   - checkout: self
   - checkout: TutoDeploy

 - job: Deploy
   dependsOn: 
   - checkout
   steps:
   - task: AzurePowerShell@5
     inputs:
      azureSubscription: 'ToAzureCnx'
      ScriptType: 'FilePath'
      ScriptPath: .\Tuto-Deploy\build.ps1
      azurePowerShellVersion: 'LatestVersion'

This is my build.ps1 file:

param
(
)

$resourceGroup = "RG2"
$location = "westeurope"

New-AzResourceGroup -Name $resourceGroup -Location $location -Force

What happend

I get this error message:

##[error]The term 'D:\a\1\s\Tuto-Deploy\build.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

what I tested

I added :

- script: dir $(Build.SourcesDirectory)\Tuto-Deploy

To check build.ps1 if is downloaded.

I also tried to run it from a pipeline in the project Tuto-Deploy:

trigger:
- main
 
pool:
  vmImage: windows-latest
 
steps:
- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'ToAzureCnx'
    ScriptType: 'FilePath'
    ScriptPath: '$(System.DefaultWorkingDirectory)/build.ps1'
    azurePowerShellVersion: 'LatestVersion'

It works fine.

So I don't think I have a problem with the script.

What I need

I don't understand why it is not working. What ca I do?

thanks

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120

1 Answers1

1

You ran the checkout steps in a separate job. And this caused the problem.

Each job will run in in fresh new agent. See here. So TutoDeploy repo which is downloaded in the first job is not accessiable in the second job. You should combine the checkout job with Deploy job. You can set condtions for the AzurePowershell task if it only needs be executed when the checkout steps are successful. See below:

- job: 
   
   steps:
   - checkout: self
   - checkout: TutoDeploy

   - task: AzurePowerShell@5
     inputs:
      azureSubscription: 'ToAzureCnx'
      ScriptType: 'FilePath'
      ScriptPath: .\Tuto-Deploy\build.ps1
      azurePowerShellVersion: 'LatestVersion'
    condition: succeeded()
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43