1

I need to deploy my application on multiple servers.

I have hosted my source code on gitlab-ci. I have setup envrionnement variables and .gitlab-ci.yml file

It works great for a single server: I can build docker images and push this images to a registry. Then i am deploying this images on a kubernetes infrastructure. All operations are described in .gitlab-ci.yml

What i need to do is to "repeat" .gitlab-ci.yml steps for each server. I need a different set of envrionment variables for each server. (I will need one docker image for each server, for each upgrade of my application).

Is there a way to do this with gitlab-ci ?

Thanks

** EDIT **

Here is my .gitlab-ci.yml:

stages:
  - build
  - deploy

build:
  stage: build
  script:
  - docker image build -t my_ci_registry_url/myimagename .
  - docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" "${CI_REGISTRY}"
  - docker push my_ci_registry_url/myimagename

deploy:
  stage: deploy
  environment: production
  script:
  - kubectl delete --ignore-not-found=true secret mysecret
  - kubectl create secret docker-registry mysecret --docker-server=$CI_REGISTRY --docker-username=$CI_REGISTRY_USER --docker-password=$CI_REGISTRY_PASSWORD
  - kubectl apply -f myapp.yml
  - kubectl rollout restart deployment/myapp-deployment
Bob5421
  • 7,757
  • 14
  • 81
  • 175

1 Answers1

3

In order to run same job with different environment variables you can use Yaml Anchors.

For example:

stages:
  - build
  - deploy

.deploy: &deploy
  stage: deploy
  environment: production
  script:
  - some use of $SPECIAL_ENV    # from `variables` defined in each job
  - some use of $OTHER_SPECIAL_ENV   # from `variables` defined in each job

build:
  stage: build
  script:
  - ...

deploy env 1:
  variables:
    SPECIAL_ENV: $SPECIAL_ENV_1   # from `CI/CD > Variable`
    OTHER_SPECIAL_ENV: $OTHER_SPECIAL_ENV-1    # from `CI/CD > Variable`
  <<: *deploy

deploy env 2:
  variables:
    SPECIAL_ENV: $SPECIAL_ENV_2   # from `CI/CD > Variable`
    OTHER_SPECIAL_ENV: $OTHER_SPECIAL_ENV_2   # from `CI/CD > Variable`
  <<: *deploy

deploy env 3:
  variables:
    SPECIAL_ENV: $SPECIAL_ENV_3   # from `CI/CD > Variable`
    OTHER_SPECIAL_ENV: $OTHER_SPECIAL_ENV_3   # from `CI/CD > Variable`
  <<: *deploy

That way on deploy stage the 3 jobs will run (parallel).
You can save the variables in Settings > CI/CD > Variable if they contain sensitive data. If not, just write them in your .gitlab-ci.yml

Aviad Levy
  • 750
  • 4
  • 13
  • Thanks but i would like to store variables in Settings >CI/CD > Variable but there are no way to create arrays of variables... Thanks – Bob5421 Jan 02 '20 at 19:11
  • Why you need to use array? just define variable with special name for each environment you have, and then use them in your job by defining common name you'll use in the anchor. – Aviad Levy Jan 03 '20 at 08:05
  • What if you don't know the number of servers beforehand? what if they production servers are in autoscaling configuration? – Shiplu Mokaddim Mar 17 '21 at 09:44