Jenkins 在 Windows agent 上執行shell 命令,聽起來很有意思,以下方法可以在 Jenkins 中執行一些簡單的 shell 腳本,如果是復雜腳本就交給 Linux agent 去做吧。
1. 安裝工具(二選一)
- Git bash
- Cygwin
由於我用Git, 所以我安裝的是Git for Windows. https://git-scm.com/download/win
2. 加入環境變量PATH
把 Git bash 命令的路徑加入環境變量, 例如:“C:\Program Files\Git\usr\bin”\
備注:如果不想加環境變量,可以到 Jenkins -> Manage -> Configure 中去設置 shell executable
3. 配置Jenkins Job
接下來就可以在 job 中增加 execute shell script, 可以用 freestyle job 也可以用 pipeline 的 sh 'xxx'.
已知問題
在執行一些帶路徑的 shell 命令時,路徑會被解析成 Windows 風格,導致命令出錯。 如:
cp ${WORKSPACE}/test.txt .
${WORKSPACE}/test.txt 會解析成:"C:\Jenkins/test.txt", 導致命令出錯。我的解決方法是 cd 或者 pushd ${WORKSPACE} 再進行相應操作。