代码之家  ›  专栏  ›  技术社区  ›  WoJ

如何有条件地更新ci/cd作业映像?

  •  0
  • WoJ  · 技术社区  · 5 年前

    我刚进入ci/cd的(美妙的)世界,有了工作管道。不过,它们并不是最优的。

    该应用程序是一个停靠的网站:

    • 源代码需要由 webpack 最后 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 安装在两个作业中。这是不需要的,会减慢部署速度。 是否有办法说明需要为特定作业添加此类包(而不是对所有作业添加全局包)?

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

    0 回复  |  直到 5 年前
        1
  •  1
  •   rflume    5 年前

    你不必对所有的工作都使用相同的图像。让我给你看看我们的一个管道(部分)做了类似的事情,只是 composer 对于php而不是 npm :

    cache:
      paths:
        - vendor/
    
    build:composer:
      image: registry.example.com/base-images/php-composer:latest # use our custom base image where only composer is installed on to build the dependencies)
      stage: build dependencies
      script:
        - php composer.phar install --no-scripts
      artifacts:
        paths:
          - vendor/
      only:
        changes:
          - composer.{json,lock,phar}  # build vendor folder only, when relevant files change, otherwise use cached folder form s3 bucket (configured in runner config)
    
    build:api:
      image: docker:18  # use docker image to build the actual application image
      stage: build api
      dependencies:
        - build:composer # reference dependency dir
      script:
        - docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY"
        - docker build -t $CI_REGISTRY_IMAGE:latest.
        - docker push $CI_REGISTRY_IMAGE:latest
    

    composer基本映像包含运行composer所需的所有包,因此在您的示例中,您将为 NPM :

    FROM alpine:latest 
    
    RUN apk add --update npm
    

    然后,在你的 create_dist 舞台和使用 image: docker:latest 作为其他阶段的形象。

        2
  •  1
  •   Anna Slastnikova    5 年前

    除了为不同的作业引用不同的图像外,您还可以尝试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
    ...
    
    
        3
  •  0
  •   Akash Sharma    5 年前

    尝试多阶段构建,你可以中间临时图像和复制生成的内容最终docker图像。另外,NPM应该是Docker映像的一部分,创建一个NPM映像,并在最终Docker映像中用作构建器映像。