gitlab-runner run os dependent script

Viewed 844

my project builds under windows and linux.I have setup a gitlab-runner on windows and one on a linux machine. Now I want to configure the ".gitlab-ci.yml" for building on both machines. BUT, depending on the operating system, I'd like to call a different build script for the build.

Example ".gitlab-ci.yaml" (not working)

mybuild:
  # on linux
  script:
    - ./build-linux.sh
  # on windows
  script
    - buildwin.bat

How can i achieve this in the .gitlab-ci.yml?

2 Answers

You can't. The way to achieve it is to

  1. give your runners unique tags. e.g. "linux-runner" and "windows-runner"
  2. duplicate the job and run one job only on runners with the tag "linux-runner" and the second job only on runners with the "windows-runner" tag.

    linux build:
      stage: build
      tags:
       - linux-runner
      script:
        - ./build-linux.sh
    
    windows build:
      stage: build
      tags:
       - windows-runner
      script:
        - buildwin.bat
    

See also https://stackoverflow.com/a/49199201/2779972

The solution generally suggested to create two jobs doesn't fit my needs. My need is to be able to use a Windows or on a Linux/MacOS runner, whatever is the one available. My suggested trick is to create a call script in /usr/local/bin so it can mimic the Windows call command:

#/bin/bash
./$*

If you want to invoke Gradle wrapper for example, you can simply write in the gitlab-ci.yml:

script:
- call gradle

it also works with a specific script (for instance "build.bat" for Windows, and "build" for MacOS/Linux):

script:
- call build

I hope that will help someone with the same need as me.

Related