【问题标题】:Docker/CI: Get access to nodeJS app, which is created in another stageDocker/CI:获取对 nodeJS 应用程序的访问权限,该应用程序是在另一个阶段创建的
【发布时间】:2017-04-25 21:10:32
【问题描述】:

在我的 CI 管道 (gitlab) 中有一个 build 和一个 end2end-testing 阶段。在构建阶段,将创建应用程序的文件。然后我想将生成的文件复制到 e2e_testing 容器中,以便对这个应用程序进行一些测试。

如何将生成的文件(/opt/project/build/core/bundle)复制到镜像中?

对于 e2e 测试,我想使用 nightwatchJS - 请参阅下面的 e2e docker 图像。也许可以在 e2e 映像中使用构建映像?

我需要做的是 nightwatchJS e2e 测试生成的 nodeJS 应用程序


我的尝试

使用docker cp 命令将生成的文件复制到e2e_testing 容器中。

build:
  stage: build
  before_script:
    - meteor build /opt/project/build/core --directory
  script:
    - cd /opt/jaqua/build/core/bundle
    - docker build -t $CI_REGISTRY_IMAGE:latest .
  after_script:
    - docker cp /opt/project/build/core/bundle e2e_testing:/opt/project/build/core/

但这不起作用,因为下一阶段 (e2e) 将从 e2e:latest 图像创建一个容器。所以在这个容器中不存在 bundle 文件夹,所以这个示例脚本失败了。

e2e:
  image: e2e:latest
  stage: e2e
  before_script:
    - cd /opt/project/build/core/bundle && ls -la
  script:
    # - run nightwatchJS to do some e2e testing with the build bundle

e2e:最新镜像 Dockerfile

FROM java:8-jre

## Node.js setup
RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -
RUN apt-get install -y nodejs

## Nightwatch
RUN npm install -g nightwatch

一个名为e2e_testing 的容器是从这个镜像创建的,它一直在运行。所以在 CI 管道运行时,容器已经存在。

但当时,创建此映像时应用程序文件不存在,因为它们是在构建阶段生成的。所以我不能使用 Dockerfile 将这些文件放入 docker 映像中。

那么如何才能在 e2e 阶段访问 build 阶段生成的文件呢?

或者是否可以在 nightwatch 映像 (e2e) 中使用构建映像 ($CI_REGISTRY_IMAGE:latest)

【问题讨论】:

  • @johnharris85 我没看到,你想给我看哪一部分... :-(
  • 什么应用程序正在解析这些 yml 文件? IE。你用的是什么 CI 工具?
  • 这两句话似乎有冲突,我可能误读了:“但这不起作用,因为下一阶段 (e2e) 将从 e2e:latest 图像创建一个容器。”和“一个名为 e2e_testing 的容器是从此映像创建的,它一直在运行。”容器是一直在运行,还是每次运行都重新生成镜像和重新创建容器?
  • @BMitch 我正在使用 gitlab CI。现在,每次运行都会重新创建容器。不知道如何正确设置它以运行构建测试。

标签: node.js docker gitlab-ci


【解决方案1】:

使用artifacts怎么样?

基本上在构建后将 bundle 文件夹移动到存储库的根目录,并将 bundle 文件夹定义为工件。然后,从 e2e 作业中,捆绑文件夹将从构建阶段的工件中下载,您将能够使用其内容。以下是如何执行此操作的示例:

build:
  stage: build
  before_script:
    - meteor build /opt/project/build/core --directory
  script:
    # Copy the contents of the bundle folder to ./bundle
    - cp -r /opt/project/build/core/bundle ./bundle
    - cd /opt/jaqua/build/core/bundle
    - docker build -t $CI_REGISTRY_IMAGE:latest .
  artifacts:
    paths:
      - bundle

e2e:
  image: e2e:latest
  stage: e2e
  dependencies:
    - build
  script:
    - mkdir -p /opt/project/build/core/bundle
    - cp -r ./bundle /opt/project/build/core/bundle
    # - run nightwatchJS to do some e2e testing with the build bundle

我不知道你是否还需要 docker build 部分,所以我把它留在了那里,以防你想把那个容器推到某个地方。

【讨论】:

  • 为什么我必须将文件夹复制到根目录?不可以用原始路径设置工件吗?
  • 如果您查看答案中链接的工件文档,您会看到:“您只能使用项目工作区中的路径。”
猜你喜欢
  • 2016-06-10
  • 2021-04-19
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 2021-08-08
  • 2018-09-23
  • 1970-01-01
  • 2022-11-29
相关资源
最近更新 更多