gitlab-ci.yml deployment on multiple hosts

扶醉桌前 提交于 2020-01-16 13:10:08

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/59546865/gitlab-ci-yml-deployment-on-multiple-hosts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!