简体   繁体   English

Gitlab-CI 如何在不同的管道中使用工件

[英]Gitlab-CI how to use artifacts in different pipeline

Currently, I have two main project.目前,我有两个主要项目。

1-) Vue Project which contains (webviews for IOS and Android, websites, and renderer for our Electron ) they are sharing components & API's. 1-) Vue 项目,其中包含(IOS 和 Android 的 webviews、网站和我们 Electron 的渲染器)它们共享组件和 API。

2-) Electron Project which builds desktop app for (windows, darwin, linux) 2-) 为(windows、darwin、linux)构建桌面应用程序的电子项目

i would like to automate our building, releasing process.我想自动化我们的构建、发布过程。 my current setup..我目前的设置..

            before_script: 
                - apt-get update
                - apt-get install zip unzip 
                - rm -rf vue-project
                - git clone vue-project
                - cd vue-project
                - git checkout dev
                - git pull
                - sed -i "/\b\(areaCode\|inline-svg-loader\)\b/d" ./packages/devtool/package.json
                - yarn install
                - ln -s vue-project/packages/desktop/ web
                - npm install

            build_darwin:
                stage: build
                script:
                    - npm run package -- darwin --deploy
                cache:
                    paths:
                        - vue-project/node_modules
                        - node_modules

which basically before bundling electron project it's cloning vue-project install dependencies and bundling electron-renderer then when it's finish.这基本上在捆绑电子项目之前克隆 vue-project 安装依赖项并捆绑电子渲染器然后当它完成时。 i'm running package.我正在运行包。

I would like to separate this two different job from each other.我想把这两个不同的工作分开。 is there anyway i could use artifacts from different project gitlab-CI pipelines ?无论如何我可以使用来自不同项目 gitlab-CI 管道的工件吗?

any help would be an appreciated.任何帮助将不胜感激。

Gitlab has a API for do a lot of tricks. Gitlab 有一个API可以做很多事情。

curl --header "PRIVATE-TOKEN:YOURPRIVATETOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/master/download?job=test"

for download it as a file.下载它作为一个文件。

curl --header "PRIVATE-TOKEN:YOURPRIVATETOKEN" -o artifacts.zip "http://gitlab.example.net/api/v4/projects/<projectnumber>/jobs/artifacts/master/download?job=build_desktop

Gitlab can certainly support this. Gitlab 当然可以支持这一点。 To accomplish this, follow these steps:为此,请按照下列步骤操作:

ARTIFACT GENERATION神器生成

In your Vue Project, modify your job(s) of interest to store artifacts relevant to the Electron project.在您的 Vue 项目中,修改您感兴趣的工作以存储与 Electron 项目相关的工件。 Each job's artifacts are defined using Gitlab Job Artifacts notation, and are uploaded at job completion to Gitlab, and stored associated to your Project , Branch , and Job .每个作业的工件都使用Gitlab Job Artifacts表示法定义,并在作业完成时上传到 Gitlab,并与您的ProjectBranchJob关联存储。

Note: Branch is often overlooked, and matters when you want to retrieve your artifacts, more on this later.注意:分支经常被忽视,当你想要检索你的工件时很重要,稍后会详细介绍。

Illustrating:说明:

Vue Project .gitlab_ci.yml

stages:
- stage1
- ...

vue-job1:
  stage: stage1
  script:
    - echo "vue-job1 artifact1" > ./artifact1
    - echo "vue-job1 artifact2" > ./artifact2
  artifacts:
    when: always
    paths:
    - ./artifact1
    - ./artifact2
    expire_in: 90 days

vue-job2:
  stage: stage1
  script:
    # error, would overwrite job1's artifacts since working
    # directory is a global space shared by all pipeline jobs
    # - echo "vue-job2 artifact1" > ./artifact1
    - echo "vue-job2 artifact1" > ./artifact3
  artifacts:
    when: always
    paths:
    - ./artifact3
    expire_in: 90 days

The artifacts generated above are written to the working directory, which is a clone of your project's repo.上面生成的工件被写入工作目录,该目录是您项目 repo 的克隆。 So be careful with filename conflicts.所以要小心文件名冲突。 To be safe, put your artifacts in a subdirectory (eg: cat "foo" > ./subdir/artifact) and reference them in paths the same way (paths: - ./subdir/artifact).为了安全起见,请将您的工件放在子目录中(例如:cat "foo" > ./subdir/artifact)并以相同的方式在路径中引用它们(路径:-./subdir/artifact)。 You can use 'ls' in your script to view the working directory.您可以在脚本中使用“ls”来查看工作目录。

When your job completes, you can confirm the artifacts stored in Gitlb by using the Gitlab UI.作业完成后,您可以使用 Gitlab UI 确认存储在 Gitlb 中的工件。 View the job output, and use the Browse button under Job Artifacts on the right panel.查看作业输出,然后使用右侧面板上 Job Artifacts 下的 Browse 按钮​​。

ARTIFACT RETRIEVAL文物检索

In your Electron Project, modify your job(s) of interest to retrieve artifacts stored in the Vue Project using the Gitlab Job Artifacts API and curl .在您的 Electron 项目中,使用Gitlab Job Artifacts APIcurl修改您感兴趣的作业以检索存储在 Vue 项目中的工件。 In order to access the Vue artifacts, you will need the Vue Project , Branch , and Job that the artifacts were created under.为了访问 Vue 工件,您将需要创建工件的 Vue ProjectBranchJob

Project: For Project, use the Project ID displayed in the Gitlab UI Project Details screen.项目:对于项目,使用 Gitlab UI 项目详细信息屏幕中显示的项目 ID

Branch: Usually master , but depends on the branch your pipeline executes against.分支:通常是master ,但取决于您的管道执行的分支。 Although this is not relevant for your problem, if you are generating and consuming artifacts across executions of the same pipeline, use the Gitlab variable $CI_COMMIT_BRANCH for the branch.尽管这与您的问题无关,但如果您在同一管道的执行中生成和使用工件,请为分支使用 Gitlab 变量$CI_COMMIT_BRANCH

Job: Generally the Job Name that generated the artifacts for your Project.工作:通常是为您的项目生成工件的工作名称 But if you need artifacts produced by a specific Job, then use the Job Number and the corresponding retrieval API.但是,如果您需要由特定 Job 生成的工件,则使用Job Number和相应的检索 API。

Illustrating:说明:

Electron Project .gitlab_ci.yml

stages:
- stage1
- ...

electron-job1:
  stage: stage1
  script:
  - curl -o ./artifact1 -H "PRIVATE-TOKEN:$TOKEN" https://gitlab.example.com/api/v4/projects/$VUE_PROJECT_ID/jobs/artifacts/$BRANCH/raw/artifact1?job=vue-job1
  - curl -o ./artifact2 -H "PRIVATE-TOKEN:$TOKEN" https://gitlab.example.com/api/v4/projects/$VUE_PROJECT_ID/jobs/artifacts/$BRANCH/raw/artifact2?job=vue-job1
  - curl -o ./artifact3 -H "PRIVATE-TOKEN:$TOKEN" https://gitlab.example.com/api/v4/projects/$VUE_PROJECT_ID/jobs/artifacts/$BRANCH/raw/artifact3?job=vue-job2

This script retrieves artifacts individually to the working directory of your Electron Project.该脚本将工件单独检索到您的 Electron 项目的工作目录中。 There are also options for retrieving all artifacts for your job at once as a zip archive.还有一些选项可以将您的工作的所有工件作为 zip 存档一次检索。

MISCELLANEOUS各种各样的

Although this is not in the problem posed, it is worth noting that you can use artifacts both within the lifespan of a single pipeline execution to pass information between jobs.尽管这不是所提出的问题,但值得注意的是,您可以在单个管道执行的生命周期内使用工件在作业之间传递信息。 You can also use this to pass information across pipeline executions within the same project.您还可以使用它在同一项目中跨管道执行传递信息。

With recent versions of gitlab, this can be achieved simply by using either the multi-project pipeline feature (then starting a pipeline in one project triggers a build from the other project): see the documentation使用最新版本的 gitlab,这可以通过使用多项目管道功能简单地实现(然后在一个项目中启动管道会触发另一个项目的构建):请参阅文档

Or you can also use the "needs:project" mechanism which allows one job to download artifacts from other pipelines (see the documentation Use needs:project to download artifacts from up to five jobs in other pipelines. )或者您也可以使用“needs:project”机制,该机制允许一个作业从其他管道下载工件(请参阅文档Use needs:project to download artifacts from up to five jobs in other pipelines.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM