问题描述
我需要在多台服务器上部署我的应用程序.
I need to deploy my application on multiple servers.
我在 gitlab-ci 上托管了我的源代码.我已经设置了环境变量和 .gitlab-ci.yml 文件
I have hosted my source code on gitlab-ci.I have setup envrionnement variables and .gitlab-ci.yml file
它适用于单个服务器:我可以构建 docker 映像并将这些映像推送到注册表.然后我在 kubernetes 基础设施上部署这些图像.所有操作都在 .gitlab-ci.yml 中描述
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
我需要做的是为每个服务器重复" .gitlab-ci.yml 步骤.我需要为每个服务器设置一组不同的环境变量.(对于我的应用程序的每次升级,我都需要为每台服务器提供一个 docker 映像).
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).
有没有办法用 gitlab-ci 做到这一点?
Is there a way to do this with gitlab-ci ?
谢谢
** 编辑 **
这是我的 .gitlab-ci.yml:
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
推荐答案
为了使用不同的环境变量运行相同的作业,您可以使用 Yaml 锚.
In order to run same job with different environment variables you can use Yaml Anchors.
例如:
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
这样在 deploy
阶段 3 个作业将运行(并行).
您可以将变量保存在 Settings >CI/CD >变量
,如果它们包含敏感数据.如果没有,只需将它们写入您的 .gitlab-ci.yml
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
这篇关于多个主机上的 gitlab-ci.yml 部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!