Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the sh step on Windows

TL;DR I want to use the sh step even though Jenkins is running on Windows. I do not want to use the bat step, unless you can show me how to easily reproduce what I need done using bat

I've been converting some old Jenkins jobs over to 2.x Pipeline script. One of my jobs uses the "Publish over SSH plugin" to:

  • Send artifacts to a remote server
  • Exec a set of commands on the remote server

For instance:

Publish over SSH plugin

I wanted to replicate this in Pipeline Script so I've done the following:

   stage('Deploy') {
    withCredentials([[$class: 'FileBinding', credentialsId: 'bitbucket-key-file', variable: 'SSHKEY']]) {
        sh '''
            scp -i "$SSHKEY" dsub.tar.gz [email protected]:dsubdeploy
            scp -i "$SSHKEY" deployDsubUi.sh [email protected]:dsubdeploy
            ssh -i "$SSHKEY" -o StrictHostKeyChecking=no 192.168.220.57 <<- EOF
                DEPLOY_DIR=/home/tprmbbuild/dsubdeploy
                echo '*** dos2unix using sed'
                sed -e 's/\r$//' $DEPLOY_DIR/deployDsubUi.sh > $DEPLOY_DIR/deployDsubUi-new.sh
                mv $DEPLOY_DIR/deployDsubUi-new.sh $DEPLOY_DIR/deployDsubUi.sh
                chmod 755 $DEPLOY_DIR/deployDsubUi.sh
                echo '*** Deploying Dsub UI'
                $DEPLOY_DIR/deployDsubUi.sh $DEPLOY_DIR/dsub.tar.gz
            EOF'''
    }
   }

Problem is, I get this stack trace when executing my build:

[Pipeline] sh
[E:\Jenkins\jenkins_home\workspace\tpr-ereg-ui-deploy@2] Running shell script
      1 [main] sh 3588 E:\Jenkins\tools\Git_2.10.1\usr\bin\sh.exe: *** fatal error - add_item ("\??\E:\Jenkins\tools\Git_2.10.1", "/", ...) failed, errno 1
Stack trace:
Frame        Function    Args
000FFFF9BB0  0018005C24E (0018023F612, 0018021CC39, 000FFFF9BB0, 000FFFF8B30)
000FFFF9BB0  001800464B9 (000FFFFABEE, 000FFFF9BB0, 1D2345683BEC046, 00000000000)
000FFFF9BB0  001800464F2 (000FFFF9BB0, 00000000001, 000FFFF9BB0, 4A5C3A455C3F3F5C)
000FFFF9BB0  001800CAA8B (00000000000, 000FFFFCE00, 001800BA558, 1D234568CAFA549)
000FFFFCC00  00180118745 (00000000000, 00000000000, 001800B2C5E, 00000000000)
000FFFFCCC0  00180046AE5 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  00180045753 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0  00180045804 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace
like image 991
ThaDon Avatar asked Nov 01 '16 15:11

ThaDon


People also ask

How do I run a Jenkins shell script in Windows?

The sh.exe which is needed to run shell scripts is in the "bin" sub-directory, i.e. C:\cygwin64\bin. 2 - Tell Jenkins where sh.exe is located. Jenkins web console > Manage Jenkins > Configure System > Under shell, set the "Shell executable" = C:\cygwin64\bin\sh.exe > Click apply & also click save.


2 Answers

Agreed with "it is my belief it is failing to spawn the shell". It is trying to run "E:\Jenkins\tools\Git_2.10.1\usr\bin\sh.exe" (using Windows backslash syntax). Unless we have a shell executable configured (sh.exe) in the mentioned directory, it will fail.

Powershell (or Cmd Shell):
If you are oepn to use batch files, you would have to install/configure 3 binaries (ssh, scp, ssh). Everything else falls in place (I see that you are channeling commands to a remote machine using ssh. I assume that the remote server is linux/unix based).

Alternatives:
You can use cygwin or run linux on virtualbox (or any software that emulates linux on windows). But, running just 3 commands may not be worth the trouble (It will definitely be fruitful if you have plans to convert/write more shell scripts in future).

like image 101
blackpen Avatar answered Oct 12 '22 01:10

blackpen


You can use "bat" instead of "sh" in windows. Also use 2 backslashes to escape the path string correctly. See example below

  node {
      currentBuild.result = "SUCCESS"
      try {

       stage('Checkout'){

          checkout scm
       }

       stage('Convert to Binary RPD'){
         bat "D:\\oracle\\Middleware\\user_projects\\domains\\bi\\bitools\\bin\\biserverxmlexec -D .\\RPD -P Gl081Reporting -O .\\GLOBI.rpd"
       }


       stage('Notify'){

         echo 'sending email'

         // send to email
           emailext ( 
               subject: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", 
               body: """$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS:
                    Check console output at $BUILD_URL to view the results.""",
               to:"[email protected] [email protected]"
            )
       }

    }
    catch (err) {

        currentBuild.result = "FAILURE"

        throw err
    }

}
like image 40
codemonkey Avatar answered Oct 12 '22 00:10

codemonkey