简体   繁体   English

Gitlab CI:如何在同一作业中上传工件后运行 curl 脚本?

[英]Gitlab CI : How to run a curl script after uploading the artifacts in the same Job?

I want to trigger a job that is configured in a Repository B which needs an artifact (exe file) from repository A.我想触发一个在存储库 B 中配置的作业,它需要来自存储库 A 的工件(exe 文件)。

For downloading this artifact I am using GitLab API, which needs jobID for downloading the artifact.为了下载这个工件,我使用 GitLab API,它需要 jobID 来下载工件。

Problem : Since I am using curl command (with JOB ID)to trigger the ui_test job in Repo B, ui_test is started even before artifacts finish uploading from build job in repo A.问题:由于我使用 curl 命令(带有作业 ID)来触发回购 B 中的ui_test作业, ui_test甚至在工件完成从回购 A 中的build作业上传之前, ui_test启动。

Is there any way to run the curl trigger command after artifacts are uploaded ?上传工件后有什么方法可以运行 curl trigger 命令吗? or if there is any other way to accomplish this?或者是否还有其他方法可以实现这一目标?

What I've tried so far到目前为止我尝试过的

  1. I've tried moving curl to after_script : but unfortunately, it runs before artifacts are uploaded我试过将 curl 移动到after_script :但不幸的是,它在工件上传之前运行
  2. I configured one more job in Repo A which runs after build job, my idea was to save the JOB_ID of build job in a global variable and pass it to the next job.我在build作业之后运行的 Repo A 中配置了另一个作业,我的想法是将build作业的JOB_ID保存在全局变量中并将其传递给下一个作业。 But unfortunately, we cannot export variables or override global variables.但不幸的是,我们无法导出变量或覆盖全局变量。 https://gitlab.com/gitlab-org/gitlab/-/issues/16765 https://gitlab.com/gitlab-org/gitlab/-/issues/16765
  3. I've tried using when with delay for delaying the start of job, this is not reliable since uploading artifacts can take more than 4 minutes also.我试过使用when with delay来延迟工作的开始,这不可靠,因为上传工件也可能需要 4 分钟以上。

Here is my yml skeleton from both the repos这是我的两个 repos 的 yml 骨架

Repository A : contains the development code Repository A : 包含开发代码

Jobs :  unit-test
        build
        deploy

Repository B : contains ui test存储库 B :包含 ui 测试

Jobs : ui-test

Yml skeleton from Repo A.来自 Repo A 的 Yml 骨架。

stages:
  - test
  - build
  - deploy

# Unit tests on the branch
unit-test:
  tags:
    - docker
  stage: test
  script:
    - echo test 

# This jobs creates the exe file 
build:
  stage: build
  tags:
    - docker
  script:
    - some build commands
    - curl -X POST -F token=<triggertoken> -F ref=master -F variables[JOB_ID]=${CI_JOB_ID}  https://gitlab.com/api/####/projects/<repoBprojectid>/trigger/pipeline
  artifacts:
    paths:
      - /build/demo/test.exe

Yml skeleton from Repo B.来自 Repo B 的 Yml 骨架。

ui_test:
  stage: test
  when: delayed
  start_in: 4 minutes
  tags:
    - selenium
  before_script:
    - echo ${JOB_ID}
    - 'curl --location --output ../resources/exe/test.exe --header "PRIVATE-TOKEN: $token" "https://gitlab.com/api/#/projects/<repoAprojectid>/jobs/$JOB_ID/artifacts//build/demo/test.exe"'
  script:
    - java loginTest.java

I wonder why so many project have selenium tests in different repositories.我想知道为什么这么多项目在不同的存储库中进行 selenium 测试。 I'm strongly against such division of projects as developers tend to forget about UI tests and they are useless when not updated.我强烈反对这样的项目划分,因为开发人员往往会忘记 UI 测试,而且如果不更新它们就毫无用处。

Selenium tests don't take much space as this is most of the time only source code for test classes so it would be reasonable to keep them together with main project code. Selenium 测试不占用太多空间,因为大多数情况下这只是测试类的源代码,因此将它们与主项目代码放在一起是合理的。 Other thing is you don't have to worry about passing artifacts and varsioning tests accordingly to main product.另一件事是您不必担心根据主要产品传递工件和变体测试。

So my advice would be to get rid of redundant project and keep UI tests together with main project unless you have good reason to keep them apart.所以我的建议是摆脱冗余项目并将 UI 测试与主项目放在一起,除非你有充分的理由将它们分开。

As you pointed out there is currently no option to pass variable between pipeline stages.正如您所指出的,目前没有在管道阶段之间传递变量的选项。 Fortunately many users found workaround in passing text file with variables as job artifact .幸运的是,许多用户找到了将带有变量的文本文件作为作业工件传递的解决方法。

So if you want to trigger UI tests I would go with solution you proposed to have one more job which triggers external job after build step.因此,如果您想触发 UI 测试,我会采用您提议的解决方案,该解决方案在构建步骤后触发外部作业。 Your yaml would look like this:您的 yaml 将如下所示:

stages:
  - test
  - build
  - deploy

...

# This jobs creates the exe file 
build:
  stage: build
  tags:
    - docker
  script:
    - some build commands
    - echo "export JOB_ID=$CI_JOB_ID;" >> variables
  artifacts:
    paths:
      - /build/demo/test.exe
      - variables

trigger_ui_test:
  stage: deploy
  script:
    - source variables
    - curl <your curl command with -F variables[JOB_ID]=${JOB_ID} >

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

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