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

Posted

技术标签:

【中文标题】如何在 gitlab 中的作业之间移动 jar 文件?【英文标题】:How to move jar files between jobs in gitlab? 【发布时间】:2021-01-11 02:44:56 【问题描述】:

我正在尝试构建一个 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,但它在同一目录中......在构建阶段日志的末尾显示为警告,但下一个作业失败说它可以't find the file ...它如何找到一个文件而不是另一个文件?

第 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  

【问题讨论】:

【参考方案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

【讨论】:

以上是关于如何在 gitlab 中的作业之间移动 jar 文件?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 gitlab 中触发特定作业

如何在 Gitlab 中以分离模式运行作业?

如何在运行作业之前使用用户界面向 gitlab 作业添加选项?

gitlab ci:手动或仅在 master 时运行构建作业

我如何收集gitlab管道中所有作业的日志?

在 Gitlab 中合并请求后运行 Jenkins 作业