Search code examples
powershellgitlabyamlpipeline

Make the pipeline fail if a specific sentence appears in the log


The goal of the task is to verify a build process in a GitLab CI/CD pipeline using PowerShell without saving any files on the server. Specifically, the task involves checking for configuration differences between Web.Config and Web.xyz.Config files.

Currently, the issue is that despite running msbuild and capturing its output, the pipeline fails. It should detect a specific error message (No element in the source document matches). And, it does not output Build failed due to missing configuration key..

I'm new to PowerShell, and I would appreciate your assistance.

-  echo "Verifying build…"
- |
  $msbuildOutput = & "C:/thePath/MSBuild.exe" OurProject.sln /p:Configuration=Demo
  if ($msbuildOutput -match "No element in the source document matches")
  {
    echo "Build failed due to missing configuration key."
    exit 1
  }

I also tried Write-Host

Edit: I see, the pipeline failed anyway, not due to exit 1.

the log: the log


Edit 7/19/2024
This was another attempt, and although the pipeline did not fail due to bad PowerShell code, the substring "No element in the source document matches" was not found. I also tried using
-like '*No element in the source document matches*' with the same behavior.
-  echo "Verifying build…"
- |
 $msbuildOutput = & $MSBUILD_PATH "$Our_ASMX_PROJECT_PATH" /p:Configuration=Demo
 if ($msbuildOutput | Select-String -Pattern "No element in the source document matches")
 {
    echo "Build failed due to missing configuration key."
    exit 1
 }

Solution

  • I had to create a log ($log) to try it, replace the variable with yours ($msbuildOutput) and try.

    $log = Get-Content C:\ZZZ\SSSSS.log
    
    $pattern = "No element in the source document matches"
    
    $matches = $log | Select-String -Pattern $pattern | ForEach-Object { $_.Line }
    
    if ($matches) {
     Write-Host "Build failed due to missing configuration key."
     #exit
    }
    else{
     Write-Host "Continue the action"
    }