Search code examples
gitlabgitlab-cigitlab-ci-runner

Run job in GitLab runner only in specific hostname


I want to run one job when a user pushes a commit only if the runner that started the pipeline is running in a specific machine (e.g myciserver).

This is my gitlab-ci.yml:

stages:
  - build

pull-docker-image:
  stage: build
  script:
    - docker pull ubuntu:20.04

So far, I tried to use the keyword only together with variables using the predefined variable $CI_SERVER_NAME:

stages:
  - build

pull-docker-image:
  stage: build
  script:
    - docker pull ubuntu:20.04
  only:
    variables:
      - $CI_SERVER_NAME == "myciserver"

However, the predefined GiLab variable $CI_SERVER_NAME is not the host name of the machine where the job is being executed, instead it always returns the string "GitLab".

How could I accomplish this? Thanks in advance.


Solution

  • There is the option to tag different runners with labels and you can specify which labels should apply to a runner to be executed.

    eg. you label your runner with my-ci your job will look like this

    pull-docker-image:
      stage: build
      tags:
        - my-ci
      script:
        - docker pull ubuntu:20.04
    

    but be aware that the job will now only run if there is a runner with those labels available.

    Further documentation can be found here: https://docs.gitlab.com/ee/ci/yaml/#tags