docker - 如何有条件地更新 CI/CD 作业映像?
问题描述
我刚刚进入 CI/CD 的(美妙的)世界并拥有工作管道。但是,它们并不是最优的。
该应用程序是一个 dockerized 网站:
- 源代码需要编译
webpack
并最终在dist
- 此
dist
目录被复制到 docker 容器中 - 然后远程构建和部署
我目前的设置非常幼稚(我添加了一些评论来说明为什么我认为需要/有用的各种元素):
# I start with a small image
image: alpine
# before the job I need to have npm and docker
# the problem: I need one in one job, and the second one in the other
# I do not need both on both jobs but do not see how to split them
before_script:
- apk add --update npm
- apk add docker
- npm install
- npm install webpack -g
stages:
- create_dist
- build_container
- stop_container
- deploy_container
# the dist directory is preserved for the other job which will make use of it
create_dist:
stage: create_dist
script: npm run build
artifacts:
paths:
- dist
# the following three jobs are remote and need to be daisy chained
build_container:
stage: build_container
script: docker -H tcp://eu13:51515 build -t widgets-sentinels .
stop_container:
stage: stop_container
script: docker -H tcp://eu13:51515 stop widgets-sentinels
allow_failure: true
deploy_container:
stage: deploy_container
script: docker -H tcp://eu13:51515 run --rm -p 8880:8888 --name widgets-sentinels -d widgets-sentinels
这个设置有点作用npm
,docker
并且安装在两个作业中。这不是必需的,并且会减慢部署速度。有没有办法说明需要为特定工作添加这样和这样的包(而不是全局添加到所有这些包)?
说清楚:这不是一个表演障碍(实际上根本不可能成为问题),但我担心我对这种工作自动化的方法是不正确的。
解决方案
除了为不同的工作引用不同的图像之外,您还可以尝试为工作提供可重用模板的 gitlab 锚点:
.install-npm-template: &npm-template
before_script:
- apk add --update npm
- npm install
- npm install webpack -g
.install-docker-template: &docker-template
before_script:
- apk add docker
create_dist:
<<: *npm-template
stage: create_dist
script: npm run build
...
deploy_container:
<<: *docker-template
stage: deploy_container
...
推荐阅读
- c# - 在前一个元素匹配条件之后获取流的第一个元素
- javascript - 松露测试中的时间延迟
- python - Gmail API ID 不工作,但线程 ID 工作
- ruby-on-rails - config.assets.compile = false - 使 heroku 不在生产中显示我的图像
- c# - 为什么我的身份数据库表没有迁移到 Azure?
- c - 从输入文件中读取字符串/单词和整数
- ruby-on-rails - 将默认日期设置为“所有日期”到 datepicker
- postgresql - 什么 PL/pgSQL 异常与 PostgreSQL 中 Oracle 的 VALUE_ERROR 等效?
- node.js - 通过发送 200 阻止 Express-API 并继续执行?
- maven - 验证代码更改后自述文件中的命令行示例是否正常工作