GitLab CI and Git Bash

GitLab CI and Git Bash

A while back GitLab CI deprecated the batch executor for Windows runners. That meant that you could no longer directly call batch scripts from a Gitlab Runner. They had switched over to Powershell. There were workarounds. You could still write a batch script and call it from PowerShell using cmd.exe but it was kind of a pain. I played around with PowerShell. I really tried to like it, but it turns out to be just like every other Microsoft product. It was clunky. I had to jump through a bunch of hoops to get it to even run a script. Passing return values was non-trivial. I’m generally all for learning new things, but this time I decided to try and see if I could use Git Bash instead.

Why Git Bash?

I’ve been doing a lot of Linux stuff lately, so I’ve gotten pretty good at writing bash scripts. When you install Git for Windows you automatically get Git Bash anyway, so I wondered if there was a way to get a GitLabRunner to run a bash script. It would have the added benefit of making scripts easier to run locally. While programming I almost always have a Git Bash terminal open so having a bash script would be a lot easier than having to switch to Powershell in order to run scripts locally.

GitLab runner running a bash script using Git Bash.

How to make it work

I stumbled upon this Stack Overflow post that had all the information I needed. You simply set up GitLab Runner as normal (install and run it as a user, not a system account) and register it. Make sure you select Shell executor. Then take 2 additional steps.

First, we need to help GitLab Runner find GitBash. Add a bash.cmd file into your user directory (the user that runs the GitLab Runner) with the following contents:

@"C:\Program Files\Git\usr\bin\bash.exe" -l

Next, we have to make sure that our starting paths are set up correctly with Linux-style formatting. In your config.toml file for your GitLab runner (it’s in the directory where you installed GitLab Runner), change the shell to bash and add Linux style paths for the builds_dir and cache_dir. That’s it. See the example below.

[[runners]]
  name = "windows"
  url = "https://your.server.name"
  token = "YOUR_SECRET_TOKEN"
  executor = "shell"
  shell = "bash"
  builds_dir="/c/gitlab-runner/builds/"
  cache_dir="/c/gitlab-runner/cache/"

Calling G-CLI from Git Bash

The next problem you might run into is that you want to use G-CLI to call LabVIEW from your CI script. Git Bash doesn’t know where to find it. The solution is simple. Add it to your path inside Git Bash. You can add it to your ~/.bashrc file by adding the line:

export PATH=$PATH:/c/Program\ Files/G-CLI

For good measure, you could add a similar line to the top of your CI script without the export statement. You do have to be careful with your paths. Even though you are running in a linux shell, you need to pass paths to G-CLI that are Windows formatted. So make sure you use \ if you use a / it won’t work. See the example below.

g-cli --lv-ver $LV_VER --timeout $TIMEOUT  "GCLI\Test.vi" 

Benefits

I now have one simple bash script that installs dependencies, runs my tests and VI Analyzer, generates my documentation and builds my code. This allows me to run the same script locally on my machine or via GitLab Runner on my build machine. I can do it from the same Git Bash shell that I already have open for using Git. And I don’t have to bother learning Powershell.

Help

If you would like how we can help you implement Continuous Integration for your project, let’s talk.