GitLab CI/CD 我可以在运行器中获取工件真实路径然后使用 scp 发送文件吗?

GitLab CI/CD Could I get artifacts real path in runner then send files with scp?

我正在学习 GitLab CI/CD,我想在完成构建后在工件中发送文件,这个想法可行吗?

image: maven:3.8.1-jdk-11

stages:
  - build
  - deploy

build:
  stage: build
  script:
    - mvn clean install
  artifacts:
    paths:
      - "*/target/*.jar"

deploy:
  stage: deploy
  script:
    - scp -r <artifacts_path> root@test.com:~/Deploy

Could I get artifacts real path in runner then send files with scp?

一般来说,没有。您必须依赖工件恢复过程。请记住,(1) 工件通常不存储在运行器上,并且 (2) docker 运行器在 docker 容器内执行作业,通常无法访问运行器主机上的文件,即使如果工件存储在那里。

作业开始时,先前阶段的工件将恢复到工作区中。

因此,作为替代解决方案,您可以简单地从一个空的工作区开始(不要检出存储库),然后上传工作区中的所有文件,假设没有 file-based变量。

deploy:
  variables:  # prevent checkout of repository
    GIT_STRATEGY: none 
  stage: deploy
  script:
    - ls -laht  # list files, which should be just restored artifacts
    - scp -r ./ root@test.com:~/Deploy

另一种方法可能是仅使用 artifacts:paths: 中使用的相同 glob 模式来查找文件并上传它们。

variables:
  ARTIFACTS_PATTERN: "*/target/*.jar"

build:
  # ...
  artifacts:
    paths:
      - $ARTIFACTS_PATTERN

deploy:
  script: # something like this. Not sure if scp supports glob patterns
    - rsync -a -m --include="$ARTIFACTS_PATTERN" user@remote:~/Deploy