Search code examples
gitlabgitlab-cigitlab-ci-runner

GitlabCi deploy on multiple servers


I use Gitlab runner and works fine for a single server. The gitlab-ci.yml is simple:

stages:
  - test
  - deploy

test:
  stage: test
  image: php
  tags:
      - docker
  script:
      - echo "Run tests..."
deploy:
    stage: deploy
    tags:
      - shell
    script:
      - sh deploy.sh

As i said this is fine for a single server but to deploy same app on another server? I tried with same gitlab-runner config (same conf.toml) but then it was only updating one of them randomly.

Is there somehow gitlab Ci to be triggered by more than 1 runner and deploy all of them according gitlab-ci.yml?


Solution

  • You can register several runners (e.g. tagged serverA and serverB) from different servers and have multiple deployment jobs, each of them performed by a different runner. This is because you can set more than one tag in a job and only a runner having all the tags will be used.

    stages:
      - test
      - deploy
    
    test:
      stage: test
      image: php
      tags:
          - docker
      script:
          - echo "Run tests..."
    
    deployA:
        stage: deploy
        tags:
          - shell
          - serverA
        script:
          - sh deploy.sh
    
    deployB:
        stage: deploy
        tags:
          - shell
          - serverB
        script:
          - sh deploy.sh
    

    However, take into account a situation when one of the deployment jobs fails - this would end up in you having two different versions of the code on the servers. Depending on your situation this might or might not be a problem.