Gitlab CI:如何在 Windows 运行器上使用 bash shell

问题描述:

来自 GitLab CI 文档,Windows 支持 bash shell.

From the GitLab CI documentation the bash shell is supported on Windows.

Supported systems by different shells:
Shells  Bash    Windows Batch   PowerShell
Windows     ✓   ✓ (default)     ✓

在我的 config.toml 中,我尝试过:

In my config.toml, I have tried:

[[runners]]
  name = "myTestRunner"
  url = xxxxxxxxxxxxxxxxxxx
  token = xxxxxxxxxxxxxxxxxx
  executor = "shell"
  shell = "bash"

但是如果我的 .gitlab-ci.yml 尝试执行 bash 脚本,例如

But if my .gitlab-ci.yml attempts to execute bash script, for example

stages:
  - Stage1 
testJob:
  stage: Stage1
  when: always
  script:
    - echo $PWD  
  tags:
    - myTestRunner

然后从包含 GitLab 多运行器的文件夹中,我右键单击并选择git bash here",然后键入:

And then from the folder containing the GitLab multi runner I right-click and select 'git bash here' and then type:

gitlab-runner.exe exec shell testJob

它无法解析 $PWD,证明它实际上没有使用 bash 执行器.(Git bash 通常可以在 Windows 上正确打印出 $PWD.)

It cannot resolve $PWD, proving it is not actually using a bash executor. (Git bash can usually correctly print out $PWD on Windows.)

Running with gitlab-runner 10.6.0 (a3543a27)
Using Shell executor...
Running on G0329...
Cloning repository...
Cloning into 'C:/GIT/CI_dev_project/builds/0/project-0'...
done.
Checking out 8cc3343d as bashFromBat...
Skipping Git submodules setup
$ echo $PWD
$PWD
Job succeeded

如果我推送提交,也会发生同样的事情,基于 Web 的 GitLab CI 终端会自动运行 .gitlab-ci 脚本.

The same thing happens if I push a commit, and the web based GitLab CI terminal automatically runs the .gitlab-ci script.

如何在 Windows 上的 GitLab CI 中正确使用 Bash 终端?

How do I correctly use the Bash terminal in GitLab CI on Windows?

首先,我的猜测是它无法正常工作(请参阅问题下方的评论).我找到了一个解决方法,也许它不是您需要的,但它有效.出于某种原因,命令echo $PWD"在 bash 命令之后连接,然后在 Windows cmd 中执行.这就是结果是$PWD"的原因.要复制它,请在 CMD 控制台中执行以下操作(仅 bash 处于打开状态):

Firstly my guess is that it is not working as it should (see the comment below your question). I found a workaround, maybe it is not what you need but it works. For some reason the command "echo $PWD" is concatenated after bash command and then it is executed in a Windows cmd. That is why the result is "$PWD". To replicate it execute the following in a CMD console (only bash is open):

bash && echo $PWD

解决方案是在 bash 中使用选项 -c 执行命令(这不是理想的解决方案,但它有效)..gitlab-ci.yml 应该是:

The solution is to execute the command inside bash with option -c (it is not the ideal solution but it works). The .gitlab-ci.yml should be:

stages:
  - Stage1 
testJob:
  stage: Stage1
  when: always
  script:
    - bash -c "echo $PWD"  
  tags:
    - myTestRunner