简体   繁体   中英

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.

For downloading this artifact I am using GitLab API, which needs jobID for downloading the artifact.

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.

Is there any way to run the curl trigger command after artifacts are uploaded ? 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
  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. But unfortunately, we cannot export variables or override global variables. 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.

Here is my yml skeleton from both the repos

Repository A : contains the development code

Jobs :  unit-test
        build
        deploy

Repository B : contains ui test

Jobs : ui-test

Yml skeleton from Repo A.

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.

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. I'm strongly against such division of projects as developers tend to forget about UI tests and they are useless when not updated.

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. 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.

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. Your yaml would look like this:

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} >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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