Defining different `before_script` for each Gitlab runner executor type

I’m effectively cross-posting this here from the issue tracker, as I was told this is probably the more appropriate venue for this.

I’m currently trying to run some Gitlab scripts on my local development environment using gitlab-runner. I’m trying to switch between the docker and shell executor type for a Gitlab script that’s originally written for containerised execution. The before_script is currently defined with the assumption that it runs on fresh GNU/Linux environment every time so when I run the same script using shell executor on my macOS environment, I get errors. Is there a way to specify multiple versions of the before_script (or event the after_script) based on the executor? I know for standard job scripts, you can define tags so that jobs only run for certain types of runner. However, based on #15062, it doesn’t seem to be currently possible for before_script's. Then again, even if tagging was supported in before_script, my local Gitlab runner after being registered with appropriate tags, can still switch between executor types.

What’s the best way to approach this?

Hi,

can you provide the content of your .gitlab-ci.yml to better allow us to understand what you’re describing?

Cheers,
Michael

It could be as simple as the following:

image: ubuntu:latest

a_job:
  script:
    - run_this_job.sh

before_script:
  - setup_env.sh

So now this manifest works ok if I ran this in containerised environment (Docker or Kubernetes executor). However, if I try to run this in my local environment through the shell (macOS), it fails because there are scripts in setup_env.sh that assumes the job is running on an Ubuntu environment. I want to be able to alter before_script depending on the type of executor I run the job in.

Hi,

ah, ok, thanks. I would actually recommend to hide the OS detection logic inside the script, and go with different script invocation in there. That way the CI config stays more generic.

In terms of detecting whether this is Ubuntu or not, you can e.g. use

cat /etc/os-release

On macOS you can run or check for the existence of sw_vers.

Then you go further with running sub scripts for each environment.

Cheers,
Michael