如何在 gitlab 中的作业之间移动 jar 文件?

How to move jar files between jobs in gitlab?

我正在尝试构建一个 gitlab 管道以从 java 应用程序构建一个 docker 图像。我有两个工件,我想在构建阶段之间传递...发现一个文件 (build_result.txt) 已上传,但找不到另一个 (chat.jar)。但是,这两个文件都在同一个目录中。将文件作为工件上传是否有大小或扩展名限制?


stages:

  - app-build
  - con-build

image: docker:latest
  
services:
  - docker:dind

variables:
  #Global variables
  GIT_SSL_NO_VERIFY: "true"
  DOCKER_DRIVER: overlay2

 #Application variables 
  APP_NAME: web
  APP_TAG: test

before_script:
  - echo "..."

after_script:
  - echo "..."

app-build:
  stage: app-build
  artifacts:
    when: always
    paths:
      - build_result.txt 
      - chat.jar
    expire_in: 2 days
  tags: 
    - dind
  script:
    - echo "BEFORE..."
    - apk --no-cache --update add maven git
    - apk --no-cache --update add openjdk11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community

    - echo "DO..."
    - echo "building app..."| tee -a build_result.txt 
    - git clone https://github.com/callicoder/spring-boot-websocket-chat-demo.git /app
    - cd /app
    - mvn package | tee -a build_result.txt
    - cp target/websocket-demo-0.0.1-SNAPSHOT.jar /app/chat.jar
    - ls / 
    - ls /app 
    - ls /app/target

con-build:
  stage: con-build
  dependencies:
    - app-build
  tags: 
    - dind
  artifacts:
    when: always
    paths:
      - docker_build.txt
    expire_in: 2 days

  script:
    - echo "Place artifact for docker consumption"
    - mkdir -p /docker/files/app
    - cp chat.jar /docker/files/app/chat.jar
    - cd docker
    - docker build -t "$APP_NAME:$APP_TAG" . | tee -a docker_build.txt

我得到的输出是找不到 chat.jar,但它在同一目录中......在构建阶段日志的末尾显示为警告,但下一个作业没有说它找不到文件......它怎么能找到一个文件而不是另一个?

步骤 1 - 日志。

[INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time:  18.574 s
 [INFO] Finished at: 2020-09-24T18:15:56Z
 [INFO] ------------------------------------------------------------------------
 $ cp target/websocket-demo-0.0.1-SNAPSHOT.jar /app/chat.jar
 $ ls /
 app
 bin
 builds
 certs
 dev
 etc
 home
 lib
 media
 mnt
 opt
 proc
 root
 run
 sbin
 srv
 sys
 tmp
 usr
 var
 $ ls /app
 Dockerfile
 Readme.md
 build_result.txt
 chat.jar
 k8s-deployment.yaml
 mvnw
 mvnw.cmd
 pom.xml
 screenshot.png
 src
 target
 $ ls /app/target
 classes
 generated-sources
 generated-test-sources
 maven-archiver
 maven-status
 surefire-reports
 test-classes
 websocket-demo-0.0.1-SNAPSHOT.jar
 websocket-demo-0.0.1-SNAPSHOT.jar.original
Running after_script
00:00
 Running after script...
 $ echo "do stuff after every stage..."
 do stuff after every stage...
Uploading artifacts for successful job
00:00
 Uploading artifacts...
 build_result.txt: found 1 matching files and directories 
 WARNING: chat.jar: no matching files               
 Uploading artifacts as "archive" to coordinator... ok  id=797950 responseStatus=201 Created token=nVg8i-GF
 Job succeeded

第 2 步 - 日志:

 $ cp chat.jar /docker/files/app/chat.jar
 cp: can't stat 'chat.jar': No such file or directory
Running after_script
00:00
 Running after script...
 $ echo "do stuff after every stage..."
 do stuff after every stage...
Uploading artifacts for failed job
00:00
 Uploading artifacts...
 WARNING: docker_build.txt: no matching files       
 ERROR: No files to upload                          
 ERROR: Job failed: command terminated with exit code 1  

您正在创建一个 docker_build.txt 文件,其内容为
echo "building app..."| tee -a build_result.txt 但随后您将目录更改为 /app,然后创建另一个 docker_build.txt,最后构建您的工件。

但是你看到你的第一个 docker_build.txt 不是你的 ls 命令,因为你的起始目录不是 / 而是 CI_PROJECT_DIR predefined variable

此外,所有工件定义也都与此路径相关,因此在移动您的 docker_build.txt 命令后,这应该可以工作:

  artifacts:
    when: always
    paths:
      - app/build_result.txt 
      - app/chat.jar