Search code examples
azureazure-devopsyamlcicd

Devops pipeline - path cant be found


Im trying to run the pipeline but it fails with the error that it cant find my files.

I had the below script which was running perfectly, without any issue. It searchs the delete.ini file inside the configuration folder.

name: Delete-Resources

trigger:
  branches:
    include:
      - main
  paths:
    include:
      - configuration/delete.ini

variables:
  - template: common/Common-Variables.yml

pool:
  vmImage: 'windows-latest'

steps:
- script: |
    @echo off
    setlocal

    REM Read delete.ini file
    set "INI_FILE=configuration\delete.ini"
    echo INI_FILE=%INI_FILE%
    type "%INI_FILE%"

    REM Check delete flag using PowerShell
    powershell -Command "$content = Get-Content -Path '%INI_FILE%'; if ($content -match 'delete\s*=\s*true') { echo 'Delete flag is set to true. Running Main_clean.ps1...'; exit 0 } else { echo 'Delete flag is not set to true. Skipping Main_clean.ps1 execution.'; exit 1 }"

    REM Check the exit code of the PowerShell command
    if %errorlevel% neq 0 (
        echo Error: PowerShell command to check delete flag failed.
        exit /b %errorlevel%
    )
  displayName: 'Check delete.ini for delete flag'

- task: AzurePowerShell@5
  inputs:
    azureSubscription: '$(serviceConnectionName)'
    ScriptType: 'FilePath'
    ScriptPath: 'infrastructure/Main_clean.ps1'
    azurePowerShellVersion: 'LatestVersion'
  condition: and(succeeded(), ne(variables['ShouldSkipMainClean'], 'true'))

but now Im trying to target an environment from a deployment job. So I created the env called acsub and modified my yaml code as below.

name: Delete-Resources

trigger:
  branches:
    include:
      - main
  paths:
    include:
      - configuration/delete.ini

variables:
  - template: common/Common-Variables.yml

pool:
  vmImage: 'windows-latest'

jobs:
- deployment: DeleteResourcesDeployment
  displayName: 'Delete Resources Deployment'
  environment: 
    name: 'acsub'
  pool:
    vmImage: 'windows-latest'
  strategy:
    runOnce:
      deploy:
        steps:
        - script: |
            @echo off
            setlocal

            REM Set path to delete.ini file using agent build working directory
            set "INI_FILE=$(Agent.BuildDirectory)\configuration\delete.ini"
            echo INI_FILE=%INI_FILE%
            type "%INI_FILE%"

            REM Check delete flag using PowerShell
            powershell -Command "$content = Get-Content -Path '%INI_FILE%'; if ($content -match 'delete\s*=\s*true') { echo 'Delete flag is set to true. Running Main_clean.ps1...'; exit 0 } else { echo 'Delete flag is not set to true. Skipping Main_clean.ps1 execution.'; exit 1 }"

            REM Check the exit code of the PowerShell command
            if %errorlevel% neq 0 (
                echo Error: PowerShell command to check delete flag failed.
                exit /b %errorlevel%
            )
          displayName: 'Check delete.ini for delete flag'

        - task: AzurePowerShell@5
          inputs:
            azureSubscription: '$(serviceConnectionName)'
            ScriptType: 'FilePath'
            ScriptPath: 'infrastructure/Main_clean.ps1'
            azurePowerShellVersion: 'LatestVersion'
          condition: and(succeeded(), ne(variables['ShouldSkipMainClean'], 'true'))

but it hasthe problem to find the repository its folder and file,

error is this

INI_FILE=D:\a\1\configuration\delete.ini The system cannot find the path specified. Get-Content : Cannot find path 'D:\a\1\configuration\delete.ini' because it does not exist.

how can I fix this?


Solution

  • It appears that the issue lies in how the path to the delete.ini file is being constructed within the deployment job. Since the deployment job runs in a different context compared to the previous script, the way you reference the file path needs to be adjusted. In your original script, you were using a relative path to locate the delete.ini file, which worked fine in that context. However, within the deployment job, you need to ensure that you are referencing the correct path based on the working directory of the agent. You can modify your script and set path to delete.ini file using $(Build.SourcesDirectory). Something like this:

    set "INI_FILE=$(Build.SourcesDirectory)\configuration\delete.ini"
    

    In order to use the files from your repository in the deployment job, you need to ensure that the repository is properly checked out before the deployment job runs.

    steps:
    - checkout: self
    - script: |
        @echo off
        setlocal
    
        REM Read delete.ini file
        set "INI_FILE=$(Build.SourcesDirectory)\configuration\delete.ini"
        echo INI_FILE=%INI_FILE%
        type "%INI_FILE%"
    
        REM Check delete flag using PowerShell
        powershell -Command "$content = Get-Content -Path '%INI_FILE%'; if ($content -match 'delete\s*=\s*true') { echo 'Delete flag is set to true. Running Main_clean.ps1...'; exit 0 } else { echo 'Delete flag is not set to true. Skipping Main_clean.ps1 execution.'; exit 1 }"
    
        REM Check the exit code of the PowerShell command
        if %errorlevel% neq 0 (
            echo Error: PowerShell command to check delete flag failed.
            exit /b %errorlevel%
        )
      displayName: 'Check delete.ini for delete flag'
    

    The checkout: self step checks out the repository associated with the pipeline. This ensures that the repository files are available for subsequent steps.