Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Batch Script not working in Jenkins Pipeline Job

I have created a powershell script which will transfer(using winscp.dll) the files from Jenkins windows server to Linux server. In Jenkins batch command, I have executed that powershell script and it works fine.

But when i tried the same in Jenkins pipeline job, it calls the powershell script and comes to the next step. Its not waiting for powershell script response.

bat 'powershell.exe -ExecutionPolicy Bypass  "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'

I have tried with another powershell script which will connect to Linux server and execute some commands. It works fine in pipeline job

Kindly guide me to fix this issue.

like image 207
user2439278 Avatar asked Jun 19 '17 05:06

user2439278


People also ask

How do I run a script in Jenkins pipeline?

Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.


1 Answers

Interesting. Your problem doesn't seem to be in your script because you already explained that it works in a batch job.

I don't know how your pipeline is written, but I would suggest taking a look to Stage, Lock and Milestone which is probably what you need.

The stage step is a primary building block in Pipeline, dividing the steps of a Pipeline into explicit units and helping to visualize the progress using the "Stage View" plugin

I guess you could add a stage block like this one in your pipeline:

 stage("Previous Step") {

      // Some previous step
    }

stage("Wait for Script Execution") {

  // Call your script
  bat 'powershell.exe -ExecutionPolicy Bypass  "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
}

 stage("Next Step") {

      // Script already finished its execution
    }

But without your pipeline info is just a guessing. Also to improve your script compatibility avoiding "bat and ExcutionPolicy" and using the PowerShell plugin, with that plugin you could simplify your code like this:

powershell -File your_script.ps1

EDIT: I forgot to mention that you can try a different alternative to powershell and the winscp lib using "scp" direct compatibility between Windows and Linux, I'm talking about Cygwin.

With Cygwin installed (with scp) you can use scp as it was a Linux box and a bash script instead powershell:

D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/file.sh\" 

In this case I'm running a script silently through Cygwin within a Jenkins Project with a "Run Windows Batch" option. In the script you can add shell commands and the scp instructions you want.

It may seems a little bit more complex but adds more flexibility to perform Windows - Linux tasks.

I've detailed more examples in my blog, you may find it useful.

As avvi mentioned you should check if that variables are being loaded.

like image 59
Miguel Ortiz Avatar answered Oct 06 '22 19:10

Miguel Ortiz