在 Windows 上使用 sh 步骤

Using the sh step on Windows

TL;DR 我想使用 sh 步骤,即使 Jenkins 在 Windows 上 运行。我不想使用 bat 步骤,除非你能告诉我如何使用 bat

轻松重现我需要完成的操作

我一直在将一些旧的 Jenkins 作业转换为 2.x 管道脚本。我的一项工作使用 "Publish over SSH plugin" 来:

例如:

我想在管道脚本中复制它,所以我做了以下操作:

   stage('Deploy') {
    withCredentials([[$class: 'FileBinding', credentialsId: 'bitbucket-key-file', variable: 'SSHKEY']]) {
        sh '''
            scp -i "$SSHKEY" dsub.tar.gz tprmbbuild@192.168.220.57:dsubdeploy
            scp -i "$SSHKEY" deployDsubUi.sh tprmbbuild@192.168.220.57: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'''
    }
   }

问题是,我在执行构建时得到了这个堆栈跟踪:

[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

同意"it is my belief it is failing to spawn the shell"。它试图 运行 "E:\Jenkins\tools\Git_2.10.1\usr\bin\sh.exe" (使用 Windows 反斜杠语法)。除非我们在上述目录中配置了 shell 可执行文件 (sh.exe),否则它将失败。

电源shell(或命令Shell):
如果您要使用批处理文件,则必须 install/configure 3 个二进制文件(ssh、scp、ssh)。其他一切就绪(我看到您正在使用 ssh 将命令传送到远程机器。我假设远程服务器是基于 linux/unix 的)。

备选方案:
您可以在 virtualbox 上使用 cygwin 或 运行 linux(或在 windows 上模拟 linux 的任何软件)。但是,运行仅 3 个命令可能不值得麻烦(如果您计划在将来 convert/write 更多 shell 脚本,那肯定会富有成果)。

您应该能够直接在批处理脚本中从 git 安装 运行 scp.exe。 据我所知,没有用于批处理的 "here" 文档,但您可以将服务器上 运行 的脚本放入一个单独的文件中。

(未测试)

stage('Deploy') {
withCredentials([[$class: 'FileBinding', credentialsId: 'bitbucket-key-file', variable: 'SSHKEY']]) {
    bat '''
        E:\Jenkins\tools\Git_2.10.1\usr\bin\scp.exe -i "${SSHKEY}" dsub.tar.gz tprmbbuild@192.168.220.57:dsubdeploy
        E:\Jenkins\tools\Git_2.10.1\usr\bin\scp.exe -i "${SSHKEY}" deployDsubUi.sh tprmbbuild@192.168.220.57:dsubdeploy
        E:\Jenkins\tools\Git_2.10.1\usr\bin\scp.exe -i "${SSHKEY}" -o StrictHostKeyChecking=no 192.168.220.57 < server_script.sh
}

}

您可以在 windows 中使用 "bat" 而不是 "sh"。 还使用 2 个反斜杠正确转义路径字符串。请参阅下面的示例

  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:"girish.lakshmanan@abc.co.uk girish.la@gmail.com"
            )
       }

    }
    catch (err) {

        currentBuild.result = "FAILURE"

        throw err
    }

}