用于Angular的Gitlab CI管道:构建和发布docker映像/未找到dst文件夹



我想在Gitlab CI中为Angular应用程序设置一个管道。

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

variables:
CLI_VERSION: 9.1.4
stages:
- install_dependencies
- build
- test
- build-image-frontend
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- ./frontend/node_modules
- ./frontend/.npm
buildFrontend:
stage: build
image: trion/ng-cli
before_script:
- cd frontend
- npm ci --cache .npm --prefer-offline
script:
- ng build --prod
- mv ./dist ${CI_PROJECT_DIR}
- echo "after build structure in frontend folder:"
- ls 
artifacts:
expire_in: 1 day
paths:
- ./dist
tags:
- docker

build-image-frontend:
stage: build-image-frontend
image: docker
services:
- docker:19.03.12-dind
before_script:
- echo "folder before cd:"
- ls
- cd frontend
script:
- docker build -t frontendproduction -f Dockerfile.ci .
- docker push frontendproduction
tags:
- docker

在阶段"strong"中;构建前端"我构建应用程序并在中创建一个工件/dist文件夹。这是我想在"阶段中用于docker构建的文件夹;构建图像前端">。码头文件";Dockerfile.ci";我在这个阶段使用的是以下内容:

FROM nginx:1.14.1-alpine
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
RUN rm -rf /usr/share/nginx/html/*
COPY ./dist /usr/share/nginx/html

我面临的问题发生在docker文件的最后一行,我想在那里复制以前创建的/dist文件夹。我得到以下错误:

Step 4/4 : COPY ./dist /usr/share/nginx/html
COPY failed: stat /var/lib/docker/tmp/docker-builder820618559/dist: no such file or directory

因此,我猜在执行构建docker时,会在容器中查找dist文件夹,但实际上dist文件夹位于${CI_PROJECT_DIR}目录中。

以下是构建前ls的输出(buildFrontend阶段中的before_script(:

folder before cd:
$ ls
README.md
backend
dist
docker-compose.yml
frontend
package-lock.json

如何复制作为工件生成并存储在buildFrontend阶段的dist文件夹?

您的build-image-frontend作业没有来自buildFrontend作业的工件。您必须将dependenciesneeds添加到build-image-frontend作业中才能获得工件:

build-image-frontend:
needs: ["buildFrontend"]
stage: build-image-frontend
image: docker
services:
- docker:19.03.12-dind
before_script:
- echo "folder before cd:"
- ls
- cd frontend
script:
- docker build -t frontendproduction -f Dockerfile.ci .
- docker push frontendproduction
tags:
- docker

build-image-frontend:
dependencies:
- buildFrontend
stage: build-image-frontend
image: docker
services:
- docker:19.03.12-dind
before_script:
- echo "folder before cd:"
- ls
- cd frontend
script:
- docker build -t frontendproduction -f Dockerfile.ci .
- docker push frontendproduction
tags:
- docker

感谢您的回复,我找到了问题。问题是,我必须将dist文件夹添加到我的docker容器中,该容器在构建上下文之外。

因为构建发生在前端文件夹中,docker不知道构建上下文之外的dist文件夹(因为前端文件夹和dist文件夹在同一级别(。为了解决这个问题,我不得不启动像这样的docker构建命令

docker build -t frontendproduction -f frontend/Dockerfile.ci .

而之前在前端目录中没有更改。因此,我可以使用add命令在构建上下文之外添加dist文件夹。

参考:如何包含Docker构建上下文之外的文件?

只需使用ADD而不是COPY

像这样的堆栈中有很多引用如何从Dockerfile将文件夹复制到docker镜像?

最新更新