如何在 win10 上设置 jenkins 以使用 cygwin bash

how to setup jenkins on win10 to use cygwin bash

是否可以 运行 使用 Cygwin 在 jenkins 中执行 sh 步骤?我试过的都不行。

为了让 sh 与 cygwin 一起工作,我使用了配置屏幕 http://<jenkins>:8080/configure 并将 Shell 可执行文件设置为 运行 C:\cygwin64\bin\bash.exe 但是当 运行设置测试管道

node {
    stage('1'){
        bat "dir C:\cygwin64\bin\bash.exe"
        sh 'ls ..'
    }
}

这是结果。

Running on Jenkins in J:\workspace\try_shellx
[Pipeline] {
[Pipeline] stage
[Pipeline] { (1)
[Pipeline] bat

J:\workspace\try_shellx>dir C:\cygwin64\bin\bash.exe 
 Volume in drive C has no label.
 Volume Serial Number is 9CDC-D180

 Directory of C:\cygwin64\bin

01/27/2017  01:13 PM           739,859 bash.exe
               1 File(s)        739,859 bytes
               0 Dir(s)  294,634,586,112 bytes free
[Pipeline] sh
sh: C:\cygwin64\bin\bash.exe: command not found
[Pipeline] }
[Pipeline] // stage

因此 sh "ls .." 无法执行,因为无法找到 bash 但 dir C:\cygwin64\bin\bash.exe 是通过 bat 步骤

找到的

感谢任何帮助

终于想通了。它由于正斜杠和反斜杠而令人困惑,并且由于未知原因而没有 bat 文件将无法工作。第一步是通常在 c:\cygwin64\ 目录

中复制 Cygwin.bat
copy C:\cygwin64\Cygwin.bat C:\cygwin64\CygwinForJenkins.bat

编辑 C:\cygwin64\CygwinForJenkins.bat 使其看起来像这样。

@echo off

c:\cygwin64\bin\bash %*

然后在 "Configure System" 管理屏幕中设置 Shell executable 设置以指向新的 bat 文件并忽略可能显示给您的所有错误。 注意非 DOS 正斜杠。

现在,当您执行 sh "find ." 或其他 linux 命令时,该命令由 jenkins 包装在临时脚本中,并以这种方式调用。

C:/cygwin64/CygwinForJenkins.bat -xe J:/workspace/try_shellx@tmp/durable-aa951084/script.sh

CygwinForJenkins.bat 然后使用 DOS arg glob %*

像这样将临时脚本传递给 cygwin bash.exe
c:\cygwin64\bin\bash -xe J:/workspace/try_shellx@tmp/durable-aa951084/script.sh

此外,您还需要 windows 系统路径中的 C:\cygwin64\bin(可在“系统”下的控制面板下找到,或使用如下所示的搜索)。这是因为 Jenkins 还添加了仅在该路径中可用的 nohup 命令。

如果您在使用此设置时遇到问题,您可以使用 set 在 bat 文件中显示您的 windows env。设置 @echo on 将显示 DOS 环境中的所有命令,这就是我得出解决方案的方式。

我的 jenkins 在 J:\workspace 下,我按照 this 解决方案来做到这一点

Windows Linux (WSL) 的子系统无法使用它,因为它不理解 Jenkins 通过 cygwin bash 的路径中的正斜杠符号。持久任务插件执行此 scriptPath.replace("\", "/") seen about here in the source。因此,在将它传递给 WSL 之前,您必须在 bat 脚本中撤消此替换,并且看起来像这样。

C:\Windows\System32\bash.exe -xe J:\workspace\try_shellx@tmp\durable-aa951084\script.sh

您可能可以使用类似于 Windows 的 sed 命令来做到这一点。我只是懒得去那么远。