Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI: How to use the bash shell on a Windows runner

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

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

In my config.toml, I have tried:

[[runners]]
  name = "myTestRunner"
  url = xxxxxxxxxxxxxxxxxxx
  token = xxxxxxxxxxxxxxxxxx
  executor = "shell"
  shell = "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

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

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

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

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

like image 478
Blue7 Avatar asked Apr 03 '18 08:04

Blue7


People also ask

What Shell does GitLab runner use?

PowerShell Desktop Edition is the default shell when a new runner is registered on Windows using GitLab Runner 12.0-13.12. In 14.0 and later, the default is PowerShell Core Edition. PowerShell doesn't support executing the build in context of another user.

Does GitLab CI use bash?

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

How does GitLab connect to runners?

When you register a runner, you are setting up communication between your GitLab instance and the machine where GitLab Runner is installed. Runners usually process jobs on the same machine where you installed GitLab Runner.

How do I run a GitLab command?

You should use user-mode if you are sure this is the mode you want to work with. Otherwise, prefix your command with sudo : $ sudo gitlab-runner run INFO[0000] Starting multi-runner from /etc/gitlab-runner/config.


1 Answers

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

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
like image 189
Carlos Cavero Avatar answered Oct 18 '22 19:10

Carlos Cavero