在构build之间传递文件夹 – GitLab CI与Docker

我想有一个单独的docker容器,构build我的应用程序,当它完成时,它将“dist”目录传递给部署的第二个容器。

我尝试使用工件和“音量”指令,但它似乎不工作。 任何人都没有任何解决办法或解决办法?

.gitlab-ci.yml

stages: - build - push - deploy build_app: stage: build script: - ./deployment/build.sh tags: - shell artifacts: paths: - /dist push_app: stage: push script: - ./deployment/push.sh tags: - shell dependencies: - build_app deploy_app: stage: deploy script: - ./deployment/deploy.sh tags: - shell 

build.sh

 #!/bin/bash set -e echo "Building application" docker build -t build:latest -f "deployment/build.docker" . 

build.docker

 RUN mkdir /app ADD . /app/ WORKDIR /app //code that creates /dist folder VOLUME ["/app/dist"] 

push.sh

 #!/bin/bash set -e docker build -t push:latest -f "deployment/push.docker" . #and other stuff here 

push.docker

 // the first catalog is not there ADD /app/dist /web 

你正在寻找的是caching :

caching用于指定应该在构build之间caching的文件和目录的列表。

所以你可以在你的gitlab-ci.yml根目录下定义这样的东西:

 cache: untracked: true key: "$CI_BUILD_REF_NAME" paths: - dist/ build_app: ... 

dist/将被caching在所有的版本中。

你的问题是你没有在build.docker中正确使用VOLUME命令。 如果启动build:latest image,/ app / dist的内容将被复制到主机文件系统的容器目录中。 这不等于你目前的工作目录。

这是一个固定的版本:

build.sh

 #!/bin/bash set -e echo "Building application" docker build -t build:latest -f "deployment/build.docker" . # Remove old dist directory rm -rf ${PWD}/dist # Here we boot the image, make a directory on the host system ${PWD}/dist and mount it into the container. # After that we copy the files from /app/dist to the host system /dist docker run -i -v ${PWD}/dist:/dist -w /dist -u $(id -u) \ build:latest sh cp /app/dist /dist 

push.docker

 // the first catalog is not there COPY /dist /web