2016-08-30 34 views
0

我想要一個單獨的Docker容器構建我的應用程序,當它完成時,它會將'dist'目錄傳遞到部署的第二個容器。構建之間傳遞文件夾 - GitLab CI與Docker

我嘗試使用工件和「音量」指令,但它似乎不工作。任何人都沒有任何變通或解決方案?

.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 

回答

0

你的問題是你沒有正確使用VOLUME命令build.docker。如果啓動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 
+0

'SH:0:無法打開CP ERROR:構建失敗:退出狀態1 '什麼是錯 – VanDavv

1

你所尋找的是caching

cache is used to specify a list of files and directories which should be cached between builds.

所以,你會定義這樣的事情在你的gitlab-ci.yml

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

build_app: ... 

dist/隨後將緩存中的所有版本。

+0

'3步:添加/距離/網絡 LSTAT距離:沒有這樣的文件或目錄 錯誤:構建失敗:退出狀態1'不幸的是,它不工作,是的,這是我正在尋找 – VanDavv

+0

我正在使用緩存Java(mvn)和NodeJs項目,我真的不能說爲什麼它不適合你。嘗試將您的CI腳本剝離至少兩個階段:一個階段填滿緩存('mkdir dist && touch dist/test.txt'),另一個階段檢查緩存是否存在('cat dist/test.txt' )。 –