ShinChven's Blog

Favorite GitLab API

GitLab, CI/CD2022-08-15 23:07:19

Build and Download Artifact

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

Artifact GitLab Runner Config

compile: # Job name
      - dist # Artifact path
    expire_in: 1 week # Artifact expiration time


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

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'