首页 > 解决方案 > 如何有条件地更新 CI/CD 作业映像?

问题描述

我刚刚进入 CI/CD 的(美妙的)世界并拥有工作管道。但是,它们并不是最优的。

该应用程序是一个 dockerized 网站:

我目前的设置非常幼稚(我添加了一些评论来说明为什么我认为需要/有用的各种元素):

# 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

这个设置有点作用npmdocker并且安装在两个作业中。这不是必需的,并且会减慢部署速度。有没有办法说明需要为特定工作添加这样和这样的包(而不是全局添加到所有这些包)?

说清楚:这不是一个表演障碍(实际上根本不可能成为问题),但我担心我对这种工作自动化的方法是不正确的。

标签: dockergitlabgitlab-ci

解决方案


除了为不同的工作引用不同的图像之外,您还可以尝试为工作提供可重用模板的 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
...


推荐阅读