Search code examples
azure-devopspipeline

Azure Devops Pipeline Stage success even that it fails to do command


I have stage in pipeline which is meant to delete files and folders older than 7 days. If that script does not find files that are older, it exited with error code 1 and stage was failed. I didnt want that, so i tried to edit stage in the way that error with no older files should be success for pipeline, but every other error (such as permission) should be considered as failed stage. But right now, with edited script, every error is considered as success for stage.

My base code was:

# Step 7: Delete Backups Older Than 7 Days
- script: |
    setlocal enabledelayedexpansion

    set DUMP_DIR=%DUMP_DIR:"=%
    echo Step 7 start
    echo Checking DUMP_DIR: "%DUMP_DIR%"
    if exist %DUMP_DIR% (
        echo DUMP_DIR exists: %DUMP_DIR%
        echo Searching for backups older than 7 days...

        REM Mazání souborů starších než 7 dní
        forfiles /P "%DUMP_DIR%" /S /D -7 /C "cmd /c echo Found and deleting old file: @path & del /F /Q @path"

        REM Mazání složek starších než 7 dní
        forfiles /P "%DUMP_DIR%" /D -7 /C "cmd /c echo Found and deleting old folder: @path & rmdir /S /Q @path"

    ) else (
        echo DUMP_DIR does not exist. Skipping cleanup.
    )
  displayName: 'Delete old backups and folders'

Then i edited it:

# Step 7: Delete Backups Older Than 7 Days
- script: |
    @echo off
    setlocal enabledelayedexpansion

    set DUMP_DIR=%DUMP_DIR:"=%

    echo Step 7 start
    echo Checking DUMP_DIR: "%DUMP_DIR%"

    if exist "%DUMP_DIR%" (
        echo DUMP_DIR exists: %DUMP_DIR%
        echo Searching for backups older than 7 days...

        set ERROR_FOUND=0

        REM Delete files older than 7 days
        for /f "tokens=*" %%F in ('forfiles /P "%DUMP_DIR%" /S /D -7 /C "cmd /c echo @path"') do (
            echo Found and deleting old file: %%F
            del /F /Q "%%F"
            if !ERRORLEVEL! NEQ 0 (
                echo Error deleting file: %%F
                exit 1
            )
        )
        if !ERRORLEVEL! EQU 1 (
            echo No old files found.
        )

        REM Delete folders older than 7 days
        for /f "tokens=*" %%F in ('forfiles /P "%DUMP_DIR%" /D -7 /C "cmd /c echo @path"') do (
            echo Found and deleting old folder: %%F
            rmdir /S /Q "%%F"
            if !ERRORLEVEL! NEQ 0 (
                echo Error deleting folder: %%F
                exit 1
            )
        )
        if !ERRORLEVEL! EQU 1 (
            echo No old folders found.
        )

    ) else (
        exit 1
    )


  displayName: 'Delete old backups and folders'

And in this state, every run is considered succes, even that i get permission or bad name errors:

Step 7 start
Checking DUMP_DIR: "C:\MongoBackup"
DUMP_DIR exists: C:\MongoBackup
Searching for backups older than 7 days...
Found and deleting old file: "C:\MongoBackup\rewrite_amd64_en-US.msi"
C:\MongoBackup\rewrite_amd64_en-US.msi
Přístup byl odepřen. #Access denied 
Found and deleting old folder: "C:\MongoBackup\rewrite_amd64_en-US.msi"
Název adresáře je neplatný. #Name of folder is invalid
Finishing: Delete old backups and folders

I tried changing the code, adding goto :error, exit /b 1, but none of this worked.


Solution

  • I did some change below and it works for me:

    1. check files and folders:

    for files: forfiles /P "%DUMP_DIR%" /S /D -7 /C "cmd /c if @isdir==FALSE echo @path"

    for folder: forfiles /P "%DUMP_DIR%" /S /D -7 /C "cmd /c if @isdir==TRUE echo @path"

    1. Check if the file/folder still exist after deletion, instead of checking !ERRORLEVEL! code.
          del /F /Q "%%F"
          if exist "%%F" (
              echo Error deleting file: %%F
              exit 1
          )
    
    1. Change the logic to validate if the older files or folders exist or not.

    The new script(change set DUMP_DIR="C:\MongoBackup" if needed):

        - task: CmdLine@2
          displayName: 'Delete old backups and folders'
          inputs:
            script: |
              @echo off
              setlocal enabledelayedexpansion
              set DUMP_DIR="C:\MongoBackup"
              set DUMP_DIR=%DUMP_DIR:"=%
              
              echo Step 7 start
              echo Checking DUMP_DIR: "%DUMP_DIR%"
              
              if exist "%DUMP_DIR%" (
                  echo DUMP_DIR exists: %DUMP_DIR%
                  echo Searching for backups older than 7 days...
              
                  REM Delete files older than 7 days
                  set FILE_FOUND=0
                  forfiles /P "%DUMP_DIR%" /S /D -7 /C "cmd /c if @isdir==FALSE echo @path" > temp.txt
                  for /f "tokens=*" %%F in (temp.txt) do (
                      set FILE_FOUND=1
                      echo Found and deleting old file: %%F
                      del /F /Q "%%F"
                      if exist "%%F" (
                          echo Error deleting file: %%F
                          exit 1
                      )
                  )
                  del temp.txt
                  if !FILE_FOUND! EQU 0 (
                      echo No old files found.
                  )
              
                  REM Delete folders older than 7 days
                  set FOLDER_FOUND=0
                  forfiles /P "%DUMP_DIR%" /S /D -7 /C "cmd /c if @isdir==TRUE echo @path" > temp.txt
                  for /f "tokens=*" %%F in (temp.txt) do (
                      set FOLDER_FOUND=1
                      echo Found and deleting old folder: %%F
                      rmdir /S /Q "%%F"
                      if exist "%%F" (
                          echo Error deleting folder: %%F
                          exit 1
                      )
                  )
                  del temp.txt
                  if !FOLDER_FOUND! EQU 0 (
                      echo No old folders found.
                  )
              ) else (
                  echo DUMP_DIR doesn't exist
                  exit 1
              )
    

    Fail to delete files:

    enter image description here

    Fail to delete folder: enter image description here

    No file or folder found: enter image description here