是否可以在管道之间传输缓存/工件?

时间:2019-04-26 15:53:21

标签: gitlab pipeline

在Gitlab中,是否可以在管道之间传输缓存或工件? 我正在一个管道中构建一个库,而我想在另一个管道中构建具有该库的应用程序。

1 个答案:

答案 0 :(得分:0)

是的,有可能。有两种方法可以实现此目的:


使用Job API和GitLab Premium

第一种选择是使用Job API来获取工件。仅当您具有 GitLab Premium 时,此方法才可用。在此选项中,您可以在Job API中使用CI_JOB_TOKEN从另一个管道中获取工件。了解更多here

这是您要放入应用管道配置中的工作的简单示例:

build_application:
  image: debian
  stage: build
  script:
  - apt update && apt install -y unzip
  - curl --location --output artifacts.zip "https://gitlab.example.com/api/v4/projects/${PROJECT_ID}/jobs/artifacts/master/download?job=build&job_token=$CI_JOB_TOKEN"
  - unzip artifacts.zip

使用S3

第二个选项是使用某些第三方中间存储,例如AWS S3。要传递工件,请遵循以下示例。

在您的管道配置中,创建以下作业:

variables:
  TARGET_PROJECT_TOKEN: [get token from Settings -> CI/CD -> Triggers]
  TARGET_PROJECT_ID: [get project id from project main page]

publish-artifact:
  image: "python:latest"
  stage: publish
  before_script:
    - pip install awscli
  script:
    - aws s3 cp output/artifact.zip s3://your-s3-bucket-name/artifact.zip.${CI_JOB_ID}
    - "curl -X POST -F token=${TARGET_PROJECT_TOKEN} -F ref=master -F variables[ARTIFACT_ID]=${CI_JOB_ID} https://gitlab.com/api/v4/projects/${TARGET_PROJECT_ID}/trigger/pipeline"

然后在您的 application 管道配置中,从s3存储桶中检索工件:

fetch-artifact-from-s3:
  image: "python:latest"
  stage: prepare
  artifacts:
    paths:
      - artifact/
  before_script:
    - pip install awscli
  script:
    - mkdir artifact
    - aws s3 cp s3://your-s3-bucket-name/artifact.zip.${ARTIFACT_ID} artifact/artifact.zip
  only:
    variables:
      - $ARTIFACT_ID

fetch-artifact-from-s3作业完成后,您的工件将在artifact/目录中可用。现在可以在 application 管道中的其他作业中使用它。