Jenkins powershell plugin always builds successfully

前端 未结 6 1668
攒了一身酷
攒了一身酷 2021-02-04 00:46

I\'m using Jenkins PowerShell plugin to build a project.

However, I found that Jenkins always considers my build successful no matter what I type inside Windows P

6条回答
  •  佛祖请我去吃肉
    2021-02-04 01:48

    Ultimately, I had to resort to the following configuration in Jenkins as none of the solutions here worked for me. Chris Nelson's answer got me on the right track. We're invoking chef-client remotely so we had to do a little magic to get the remote PS session to talk the local and then pass status on to Jenkins.

    • $res gives the output of chef-client.
    • $lastsuccess is true or false according to PS rules of engagment.

    Of course, you'll have to supply your own evironment variables! :)

     Write-host "Deploying $env:Computer with $env:Databag data bag... "
     $secstr = ConvertTo-SecureString $env:Password -AsPlainText -Force 
     $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $env:User, $secstr
     $s = New-PSSession -ComputerName $env:Computer  -Credential $cred
     $res = Invoke-Command -Session $s -ScriptBlock { try {chef-client} catch {exit 1}}
     $lastsuccess = Invoke-Command -Session $s -ScriptBlock {$?}
     Remove-PSSession $s
     write-host " --- "
     write-host $res
     write-host " --- "
     if($lastsuccess)
     {
      write-host "chef deployment completed"
      exit 0
     }
     write-host "chef deployment had errors"
     exit 1
    

提交回复
热议问题