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