Use GitLab Artifact in Chained Pipelines

Mon Aug 15 2022

Build and Download Artifact

GitLab CI/CD job can archive result as an artifact that can be accessed from API.

Artifact GitLab Runner Config

yaml
|
compile: # Job name artifacts: paths: - dist # Artifact path expire_in: 1 week # Artifact expiration time

Download

bash
|
# Download artifact with job name curl --request GET -sL \ --header "JOB-TOKEN: $CI_JOB_TOKEN" \ --url 'https://<GITLAB_HOST>/api/v4/projects/<PROJECT_ID>/jobs/artifacts/<BRANCH_NAME>/download?job=<JOB_NAME>' \ --output './artifact.zip' # Unzip artifact unzip artifact.zip

Downstream Jobs

GitLab pipeline can be triggered using trigger API and can have downstream jobs when you need a few CI/CD jobs be chained.

bash
|
curl --request POST \ --form token=${CI_JOB_TOKEN} \ --form ref=<BRANCH_NAME> \ --form 'variables[<VAR_NAME>]=<VAR_VALUE>' \ --url 'https://<GITLAB_HOST>/api/v4/projects/<PROJECT_ID>/trigger/pipeline'

Token Access

GitLab repository now requires project scope permission to access artifact in CI Job using CI_JOB_TOKEN.

Go to your source project Settings, CI/CD Settings, in the Token Acess area, add your downstream project to Project with access list.