Saltar al contenido

~ / archivo / stackoverflow / 44622897

Execute Batch Script not working in Jenkins Pipeline Job

StackOverflow · windows, powershell, batch-file, jenkins · ayudé
  1. user2439278 pregunta +6 2017-06-19 05:34

    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.

    • iRon 2017-06-23 11:17 You need to resolve this from within your PowerShell script which is not given in your question. There is an PowerShell/winscp.dll example here: winscp.net/eng/docs/library_powershell. As in this example the $session is cleaned up ($session.Dispose()) when the transfer was successful, you can assume that the transfer process is done.
    • Miguel Ortiz yo 2017-06-27 16:13 Could you share your pipeline configuration?
  2. Miguel Ortiz yo respuesta +3 2017-06-27 04:05

    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.

  3. Avner respuesta 2017-06-28 04:42

    You are not calling the script correctly. You are passing in $Env in the powershell invocation which is a powershell variable.

    You are in batch mode so should be passing in %.

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

← todos los hilos de StackOverflow