为什么Gitlab-CI在下一阶段会删除工件?

时间:2019-01-22 19:07:06

标签: docker gitlab-ci

在指定依赖项之后,Gitlab-CI仍会在进入下一阶段时从工作目录中删除工件。我已经尝试了该代码的所有不同变体,我认为这些变体都可以工作,但是没有成功。此外,我已经阅读了数小时,但仍未成功。

Gitlab 11.6.0

stages:
    - build_app
    - build_container
    - test
    - release
    - deploy

variables:
  IMAGE_TEST_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  IMAGE_RELEASE_NAME: $CI_REGISTRY_IMAGE:latest

build_app:
  stage: build_app
  script:
    - echo "compile the program"
    - zip zipfile.zip helloworld.txt 
    - pwd
    - ls -al
  artifacts:
    paths:
      - /zip

build_container:
  stage: build_container
  before_script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
  dependencies:
    - build_app
  script:
    - pwd
    - ls -al
    - docker build --pull -t $IMAGE_TEST_NAME .
    - docker push $IMAGE_TEST_NAME

test:
  stage: test
  before_script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
  script: 
    - docker pull $IMAGE_TEST_NAME
    - docker run $IMAGE_TEST_NAME yum install unzip -y && unzip /helloworld.zip && cat /helloworld.txt

release:
  stage: release
  before_script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
  script:
    - docker pull $IMAGE_TEST_NAME
    - docker tag $IMAGE_TEST_NAME $IMAGE_RELEASE_NAME
    - docker push $IMAGE_RELEASE_NAME
  only:
    - master

deploy:
  stage: deploy
  script:
    - ./deploy.sh
  only:
    - master
  when: manual

build_container阶段的错误:

Running with gitlab-runner 11.6.1 (8d829975)
  on gitrunner-shell trtHcQTS
Using Shell executor...
Running on gitrunner.example.com...
Fetching changes...
Removing zipfile.zip #################### <------------- this right here
HEAD is now at 07e787e Update .gitlab-ci.yml
Checking out 07e787ec as newFeature...
Skipping Git submodules setup
$ echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
WARNING! Your password will be stored unencrypted in /home/gitlab-runner/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
$ pwd
/home/gitlab-runner/builds/trtHcQTS/0/dhealy/docker-helloworld-test
$ ls -al
total 12
drwxrwxr-x 3 gitlab-runner gitlab-runner   80 Jan 22 12:10 .
drwxrwxr-x 4 gitlab-runner gitlab-runner   70 Jan 21 17:48 ..
-rw-rw-r-- 1 gitlab-runner gitlab-runner   57 Jan 22 12:10 dockerfile
drwxrwxr-x 5 gitlab-runner gitlab-runner  138 Jan 22 12:10 .git
-rw-rw-r-- 1 gitlab-runner gitlab-runner 1450 Jan 22 12:10 .gitlab-ci.yml
-rw-rw-r-- 1 gitlab-runner gitlab-runner   11 Jan 22 12:10 helloworld.txt
$ docker build --pull -t $IMAGE_TEST_NAME .
Sending build context to Docker daemon    151kB

Step 1/3 : FROM centos:7
7: Pulling from library/centos
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Image is up to date for centos:7
 ---> 1e1148e4cc2c
Step 2/3 : COPY zip/helloworld.zip /
COPY failed: stat /var/lib/docker/tmp/docker-builder929937870/zip/helloworld.zip: no such file or directory
ERROR: Job failed: exit status 1

我希望helloworld.zip在下一阶段build_container中存在于工作目录中。

1 个答案:

答案 0 :(得分:0)

删除zip文件是非常正常的,因为在每个作业开始时,源都与当前提交同步。

您的问题来自zip文件的工件路径,该路径在build_app作业中不正确。您应该写:

artifacts:
  paths:
  - zipfile.zip