API¶
Authentication¶
Export GITLAB_TOKEN (already exported in when entering i.e. ~/oas/openappstack/)
and API url:
Alias curl cmd:
Authentication scope¶
Show which user the token belongs to:
Show which groups a token has access to (and find out group IDs matching group names):
curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v4/groups" | jq '.[] | .full_path, .id'
What scopes do I need for which action ?
- Git (pull/push):
read_repository, write_repository
Projects¶
https://docs.gitlab.com/api/projects.html
Extensive list of projects:
Simple list of projects:
Show only projects that the current user is member of:
GITLAB_CURL "${GITLAB_API_URL}/projects?simple=truei&membership=true" | jq .
GITLAB_CURL "${GITLAB_API_URL}/projects?simple=true&membership=true" | jq '.[] | { id:.id, name:.name_with_namespace}'
Find out project id by project name:
GITLAB_CURL "${GITLAB_API_URL}/projects?simple=true&membership=true&search=infrastructure" | jq '.[] | { id:.id, name:.name_with_namespace}'
Artifacts¶
https://docs.gitlab.com/ee/api/job_artifacts.html
Get list of jobs:
Then use job id to download the artifact zip:
Or download artifact zip from latest successful pipeline (beware, if the last pipeline doesn't contain the job this will fail, even if an earlier successful pipeline succeeded with this job):
Jobs¶
https://docs.gitlab.com/ee/api/jobs.html
Get a log file:
List pipeline bridges / child pipelines:
$ GITLAB_CURL "${GITLAB_API_URL}/projects/6/pipelines/14147/bridges" | \
jq '.[] | select(.name=="install-stackspin") | .downstream_pipeline.id'
14149
GITLAB_CURL "${GITLAB_API_URL}/projects/6/pipelines/14149/jobs"
GITLAB_CURL "${GITLAB_API_URL}/projects/6/pipelines/14149/jobs" | jq 'map({stage: .stage, name:.name})'
GITLAB_CURL "${GITLAB_API_URL}/projects/6/pipelines/14149/jobs" | jq '.[].name'
Issues¶
https://docs.gitlab.com/api/issues.html
Beware of pagination, max. 100 issues can get queried per page !
Get all issues (max. 100):
Get all open issues (max. 100):
CI variables¶
https://docs.gitlab.com/api/project_level_variables.html
List variables:
Create variable:
GITLAB_CURL --request POST "${GITLAB_API_URL}/projects/6/variables" \
--form "key=zzztest" --form "value=new value" --form "masked=true"
URLs¶
Get single project (id):
i.e. leap_cli: 241
Paginate:
show builds for project
Cleaning up¶
https://docs.gitlab.com/api/branches.html#delete-merged-branches
Repository files API¶
curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
'https://gitlab.com/api/v4/projects/1234/repository/files/gitlab%2Fci-configs%2Frepolinter.yaml/raw?ref=main'
or
curl "https://gitlab.com/api/v4/projects/1234/repository/files/gitlab%2Fci-configs%2Frepolinter.yaml/raw?ref=main&private_token=$GITLAB_TOKEN"
In CI use CI_JOB_TOKEN:
curl --header "JOB-TOKEN: $CI_JOB_TOKEN" \
'https://gitlab.com/api/v4/projects/1234/repository/files/gitlab%2Fci-configs%2Frepolinter.yaml/raw?ref=main'