GitLab API for Developer Platforms & DevOps
This skill enables developers to interact with the GitLab API for effective DevOps and automated CI/CD processes. It covers essential topics like managing projects, executing pipelines, handling merge requests, utilizing runners, and accessing the container registry.
TL;DR Checklist
When to Use
Use this skill when:
- You need to automate project management in GitLab.
- You want to trigger and retrieve information on pipelines programmatically.
- You are handling merge requests and require automation in the review process.
- You need to configure CI/CD runners from your applications.
- You require access to the GitLab container registry to manage your Docker images.
When NOT to Use
Avoid this skill for:
- One-off manual GitLab API calls that don't require automation.
- Simple queries that can be handled through the GitLab UI.
- Use cases not requiring a DevOps setup.
Core Workflow
Set Up Your GitLab Access — First, generate a personal access token with relevant scopes (api, read_user).
- Go to User Settings → Access Tokens, create a new token, and copy it for use in API calls.
Access GitLab Projects — Use the GitLab API to retrieve project information.
- Endpoint:
GET /projects
- Example Usage:
import requests
# Define your variables
private_token = 'YOUR_PRIVATE_TOKEN'
url = 'https://gitlab.com/api/v4/projects'
headers = {'PRIVATE-TOKEN': private_token}
response = requests.get(url, headers=headers)
# Check response
if response.status_code == 200:
projects = response.json()
print("Projects:", projects)
else:
print("Failed to retrieve projects:", response.status_code)
Manage Pipelines — Trigger a pipeline for a project.
- Endpoint:
POST /projects/:id/pipeline
- Example Usage:
project_id = 123456 # Replace with your project ID
url = f'https://gitlab.com/api/v4/projects/{project_id}/pipeline'
payload = {'ref': 'main'}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 201:
pipeline = response.json()
print("Pipeline triggered:", pipeline)
else:
print("Failed to trigger pipeline:", response.status_code)
Handle Merge Requests — Create a merge request through the API.
- Endpoint:
POST /projects/:id/merge_requests
- Example Usage:
merge_request_url = f'https://gitlab.com/api/v4/projects/{project_id}/merge_requests'
merge_request_data = {
'source_branch': 'feature-branch',
'target_branch': 'main',
'title': 'Merge Feature Branch'
}
mr_response = requests.post(merge_request_url, headers=headers, json=merge_request_data)
if mr_response.status_code == 201:
merge_request = mr_response.json()
print("Merge Request created:", merge_request)
else:
print("Failed to create merge request:", mr_response.status_code)
Configure Runners — Set up a runner for your project.
- Endpoint:
POST /projects/:id/runners
Interact with the Container Registry — List container repository tags.
- Endpoint:
GET /projects/:id/registry/repositories
Implementation Patterns
Example of Accessing the Container Registry
Here's how you can list the tags from your container registry repository.
# Listing container images and tags
registry_url = f'https://gitlab.com/api/v4/projects/{project_id}/registry/repositories'
registry_response = requests.get(registry_url, headers=headers)
if registry_response.status_code == 200:
repos = registry_response.json()
for repo in repos:
print(f"Repository ID: {repo['id']}, Tags: {repo['tags']}")
else:
print("Failed to retrieve repository info:", registry_response.status_code)
Constraints
MUST DO
- Always authenticate using a personal access token.
- Ensure proper handling of response status codes to manage errors effectively.
MUST NOT DO
- Do not expose your private access token in public repositories.
- Avoid unnecessary calls to the API to not hit rate limits or exhaust your quota.
1---2name: gitlab-api-devops3description: Implements GitLab API functionalities for Developer Platforms and DevOps, covering projects, pipelines, merge requests, runners, and registry management.4license: MIT5---67891011# GitLab API for Developer Platforms & DevOps1213This skill enables developers to interact with the GitLab API for effective DevOps and automated CI/CD processes. It covers essential topics like managing projects, executing pipelines, handling merge requests, utilizing runners, and accessing the container registry.1415## TL;DR Checklist16- [ ] Access project details and configurations using GitLab API.17- [ ] Trigger and manage CI/CD pipelines effectively.18- [ ] Handle merge requests programmatically.19- [ ] Configure and manage runners.20- [ ] Interact with the GitLab container registry effectively.2122## When to Use23Use this skill when:24- You need to automate project management in GitLab.25- You want to trigger and retrieve information on pipelines programmatically.26- You are handling merge requests and require automation in the review process.27- You need to configure CI/CD runners from your applications.28- You require access to the GitLab container registry to manage your Docker images.2930## When NOT to Use31Avoid this skill for:32- One-off manual GitLab API calls that don't require automation.33- Simple queries that can be handled through the GitLab UI.34- Use cases not requiring a DevOps setup.3536## Core Workflow371. **Set Up Your GitLab Access** — First, generate a personal access token with relevant scopes (api, read_user).38 - Go to **User Settings** → **Access Tokens**, create a new token, and copy it for use in API calls.39402. **Access GitLab Projects** — Use the GitLab API to retrieve project information.41 - **Endpoint:** `GET /projects`42 - **Example Usage:**43 ```python44 import requests4546 # Define your variables47 private_token = 'YOUR_PRIVATE_TOKEN'48 url = 'https://gitlab.com/api/v4/projects'4950 headers = {'PRIVATE-TOKEN': private_token}51 response = requests.get(url, headers=headers)5253 # Check response54 if response.status_code == 200:55 projects = response.json()56 print("Projects:", projects)57 else:58 print("Failed to retrieve projects:", response.status_code)59 ```60613. **Manage Pipelines** — Trigger a pipeline for a project.62 - **Endpoint:** `POST /projects/:id/pipeline`63 - **Example Usage:**64 ```python65 project_id = 123456 # Replace with your project ID66 url = f'https://gitlab.com/api/v4/projects/{project_id}/pipeline'6768 payload = {'ref': 'main'}69 response = requests.post(url, headers=headers, json=payload)7071 if response.status_code == 201:72 pipeline = response.json()73 print("Pipeline triggered:", pipeline)74 else:75 print("Failed to trigger pipeline:", response.status_code)76 ```77784. **Handle Merge Requests** — Create a merge request through the API.79 - **Endpoint:** `POST /projects/:id/merge_requests`80 - **Example Usage:**81 ```python82 merge_request_url = f'https://gitlab.com/api/v4/projects/{project_id}/merge_requests'8384 merge_request_data = {85 'source_branch': 'feature-branch',86 'target_branch': 'main',87 'title': 'Merge Feature Branch'88 }8990 mr_response = requests.post(merge_request_url, headers=headers, json=merge_request_data)9192 if mr_response.status_code == 201:93 merge_request = mr_response.json()94 print("Merge Request created:", merge_request)95 else:96 print("Failed to create merge request:", mr_response.status_code)97 ```98995. **Configure Runners** — Set up a runner for your project.100 - **Endpoint:** `POST /projects/:id/runners`1011026. **Interact with the Container Registry** — List container repository tags.103 - **Endpoint:** `GET /projects/:id/registry/repositories`104105## Implementation Patterns106107### Example of Accessing the Container Registry108Here's how you can list the tags from your container registry repository.109110```python111# Listing container images and tags112registry_url = f'https://gitlab.com/api/v4/projects/{project_id}/registry/repositories'113114registry_response = requests.get(registry_url, headers=headers)115116if registry_response.status_code == 200:117 repos = registry_response.json()118 for repo in repos:119 print(f"Repository ID: {repo['id']}, Tags: {repo['tags']}")120else:121 print("Failed to retrieve repository info:", registry_response.status_code)122```123124## Constraints125### MUST DO126- Always authenticate using a personal access token.127- Ensure proper handling of response status codes to manage errors effectively.128129### MUST NOT DO130- Do not expose your private access token in public repositories.131- Avoid unnecessary calls to the API to not hit rate limits or exhaust your quota.