grandchild
grandchild

Reputation: 1366

Gitlab-CI: set environment variables for both Linux and Windows

I have a project that needs to be able to be built through a Gitlab-Runner on either Linux or Windows using the same .gitlab-ci.yml file in the Git repository. It's a simple build and I just want to use the shell executor.

The problem is that the syntax for setting environment variables in bash (Linux) is different from cmd (Windows). One uses export the other set.

How can I set up my environment without resorting to setting up a different config.toml on each runner? (Which also seems buggy on Linux when it comes to modifying $PATH, but that's another issue.)

Upvotes: 2

Views: 6280

Answers (1)

grandchild
grandchild

Reputation: 1366

You can make use of the common || syntax for conditional execution in both shells to write commands that will fall through on one OS but not on the other. In the case of setting environment variables the fact that the export does not exist in cmd can be exploited to create a sort-of-dirty OS check:

export PATH="$MYPATH/:$PATH" || set "PATH=%MYPATH%\;%PATH%"

The first half will execute correctly on Linux, but throw an error on Windows. The || syntax is common to both shells and will execute the succeeding statement only when the preceding one fails. So the second half will only execute on Windows, since export will only fail in cmd (provided of course that the syntax is correct on the Linux side).

A complete .gitlab-ci.yml might look like this:

stages:
  - release

before_script:
  # set environment for both Linux (first half) and Windows (second half)
  - export PATH="$MYPATH/:$PATH" || set "PATH=%MYPATH%\;%PATH%"

my-app:
  stage: release
  script:
    - ./gradlew build

Upvotes: 6

Related Questions