如何从使用不同 docker 镜像的 gitlab 阶段传递构建工件

Lok*_*kii 2 docker gitlab-ci gitlab-ci-runner

您好,Gitlab 管道具有多个阶段。我想在 Nodejs 容器中构建一个 React 项目,然后创建另一个带有 nginx 的 docker 容器以在 AWS 上部署。

这是我的 gitlab-ci.yml 文件:

image:
  name: node:14-alpine

cache:
  key: '$CI_COMMIT_REF_SLUG'
  paths:
    - node_modules/
    - .yarn

stages:
  - install
  - build
  - deploy

job_install:
  stage: install
  script:
    - echo 'yarn-offline-mirror ".yarn-cache/"' >> .yarnrc
    - echo 'yarn-offline-mirror-pruning true' >> .yarnrc
    - yarn install
  cache:
    key:
      files:
        - yarn.lock
    paths:
      - .yarn-cache/
  tags:
    - docker

job_build:
  stage: build
  cache:
    key: '$CI_COMMIT_REF_SLUG'
    paths:
      - node_modules/
      - .yarn
  script:
    - yarn build
  artifacts:
    paths:
      - build
  tags:
    - docker

job_deploy:
  stage: deploy
  image: docker
  services: 
    - docker:dind
  cache:
    key: "$CI_COMMIT_REF_SLUG"
  script:
    - apk update
    - apk upgrade
    - apk add bash
    - chmod +x ./build-scripts/build_wrapper.sh
    - ./build-scripts/build_wrapper.sh
  artifacts:
    paths:
      - BUILDTAG.txt
      - env.sh
      - logs
    when: always
  resource_group: realizeui_deployment
  tags:
    - docker
Run Code Online (Sandbox Code Playgroud)

基本上,build_wrapper.sh 文件将docker build .在代码存储库中执行 Dockerfile。此 Dockerfile 将在其中安装 nginx 和其他工具。

我想将 artifacts: /build 从 stage:build 传递到 stage:deploy 以便在 nginx 容器中使用。

我尝试在文档中寻找解决方案,但没有成功。提前致谢。

Lok*_*kii 5

所以我找到了解决方案。我只需要在部署中添加以前的工作作为依赖项。这使得 gitlab 从依赖项作业中下载所有工件并使它们可用于部署作业。

部署作业如下所示:

job_deploy:
  stage: deploy
  image: docker
  services: 
    - docker:dind
  cache:
    key: "$CI_COMMIT_REF_SLUG"
  dependencies: 
    - job_build
  script:
    - apk update
    - apk upgrade
    - apk add bash
    - chmod +x ./build-scripts/build_wrapper.sh
    - ./build-scripts/build_wrapper.sh
  artifacts:
    paths:
      - BUILDTAG.txt
      - env.sh
      - logs
    when: always
  resource_group: realizeui_deployment
  tags:
    - docker
Run Code Online (Sandbox Code Playgroud)