Gitlabs artifact of one project used in other projects

Question

  • What is the best way to transfer artifacts (jar, class, war) among projects when using docker containers in the CI phase.

Let me explain my problem in detail, please do not stop reading ... =)

Gitlabs1 Project

  • unit tests
  • etc...
  • package

Gitlabs2 Project

  • unit test
  • etc...
  • assembly (failure)
    • here i need one artifact (jar) generated in project1

Current scenario / comments

  • I use dockers, so in each .gitlab-ci.yml I will have independent containers.
  • Everything works fine in the project1
  • If I use "shell" instead of dockers in my .gitlab-ci.yml, I can save the jar file from project1 to disk and use when project2 starts building
  • Today my trigger for calling project2 when project1 ends is working fine
  • My artifact is not RPM, so I will not add to my repo

Possible solutions

+8
source share
4 answers

Hello, you should take a look at a script called get-last-successful-build-artifact.sh developed by morph027 .

https://gitlab.com/morph027/gitlab-ci-helpers

This script allows you to download an artifact and unzip it to the root of the project. It uses the Gitlab API to retrieve the last successful build and load the corresponding artifact. You can combine several artifacts and unzip wherever you want by simply updating the script a little.

I am also currently running the PHP library to handle build artifacts, but it is at a very early stage and is currently associated with laravel.

There is currently no easy way to manage the use of artifacts between projects, you must create your own using these tools.

I think that using the shell executor is not the right solution, it is very dangerous because you cannot check the file on the server that was used during the build!

Hope this helps :)

+5
source

Cool, found my passage here;)

Is it possible to use get -last-successful-build-artifact.sh without private-token (in the readable world of the repository)? for example, to share an artifact upload command without showing your token

Yes, just add it as a secret variable in project settings -> pipelines -> secret variables.

+1
source

At the time of writing, artifacts cannot be shared between projects only inside the pipeline. See https://docs.gitlab.com/ee/ci/yaml/README.html#artifacts

However, there is an open function to enable this object, which has not yet been implemented. https://gitlab.com/gitlab-org/gitlab-ce/issues/14728

0
source

GitLab silver and premium have $ CI_JOB_TOKEN available, which allows the following .gitlab-ci.yaml snippet:

 build_submodule: image: debian stage: test script: - apt update && apt install -y unzip - curl --location --output artifacts.zip "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/master/download?job=test&job_token=$CI_JOB_TOKEN" - unzip artifacts.zip only: - tags 

However, if you do not have Silver or higher gitlab subscriptions, but you rely on free tiers, you can also use the APIs and pipeline triggers.

Suppose we have a project app.jar that project B needs .

First, you need an API token. Go to settings to create it, then save it as a variable in project B. In my example, this is GITLAB_API_TOKEN .

In the CI / CD settings of Project B, add a new trigger, for example, "Project A built." This will give you a token that you can copy. Open the A .gitlab-ci.yaml project and copy the trigger_build: section from the CI / CD settings launch section of project B.

Project A :

 trigger_build: stage: deploy script: - "curl -X POST -F token=TOKEN -F ref=REF_NAME https://gitlab.example.com/api/v4/projects/${PROJECT_B_ID}/trigger/pipeline" 

Replace TOKEN with this token (better, save it as a variable in project A - then you will need to make it token=${TRIGGER_TOKEN_PROJECT_B} or something else), and replace REF_NAME with your branch (for example, master ).

Then in project B we can write a section that is based only on triggers and extracts artifacts.

Project B :

 download: stage: deploy only: - triggers script: - "curl -O --header 'PRIVATE-TOKEN: ${GITLAB_API_TOKEN}' https://gitlab.example.com/api/v4/projects/${PROJECT_A_ID}/jobs/${REMOTE_JOB_ID}/artifacts/${REMOTE_FILENAME}" 

If you know the path to the artifact, you can replace it with ${REMOTE_FILENAME} , for example build/app.jar . The project ID can be found in the CI / CD settings.

I expanded the script in project A to pass the remaining information as described in the trigger settings section:

Add variables[VARIABLE]=VALUE to the API request. Variable values โ€‹โ€‹can be used to distinguish between running pipelines and regular pipelines.

So the trigger passes REMOTE_JOB_ID and REMOTE_FILENAME, but of course you can change this as needed:

 curl -X POST \ -F token=TOKEN \ -F ref=REF_NAME \ -F "variables[REMOTE_FILENAME]=build/app.jar" \ -F "variables[REMOTE_JOB_ID]=${CI_JOB_ID}" \ https://gitlab.example.com/api/v4/projects/${PROJECT_B_ID}/trigger/pipeline 
0
source

All Articles