Integrating GitLab API with Website
GitLab API is used for CI/CD integrations, displaying pipeline status, managing tasks from external systems, and authorization via GitLab OAuth. Particularly relevant for DevOps tools and corporate portals.
Authentication
import gitlab
# Personal Access Token
gl = gitlab.Gitlab('https://gitlab.com', private_token=GITLAB_TOKEN)
# OAuth2 for user applications
gl = gitlab.Gitlab('https://gitlab.com', oauth_token=oauth_token)
Common Scenarios
CI/CD Pipeline Status on Website:
def get_pipeline_status(project_id: int, ref: str = 'main') -> dict:
project = gl.projects.get(project_id)
pipelines = project.pipelines.list(ref=ref, per_page=1)
if not pipelines:
return {'status': 'unknown'}
pipeline = pipelines[0]
return {
'status': pipeline.status, # success/failed/running/pending
'ref': pipeline.ref,
'sha': pipeline.sha[:8],
'started_at': pipeline.started_at,
'duration': pipeline.duration,
'url': pipeline.web_url,
}
Trigger Pipeline from Admin Panel:
public function triggerDeploy(Request $request): JsonResponse
{
$resp = Http::withToken(config('services.gitlab.token'))
->post("https://gitlab.com/api/v4/projects/{$projectId}/pipeline", [
'ref' => 'main',
'variables' => [
['key' => 'DEPLOY_ENV', 'value' => $request->environment],
],
]);
return response()->json(['pipeline_id' => $resp->json('id')]);
}
GitLab OAuth2
Route::get('/auth/gitlab/redirect', function () {
return redirect('https://gitlab.com/oauth/authorize?' . http_build_query([
'client_id' => config('services.gitlab.client_id'),
'redirect_uri' => route('auth.gitlab.callback'),
'response_type' => 'code',
'scope' => 'read_user read_api',
]));
});
Webhooks
GitLab supports Push Events, Pipeline Events, Merge Request Events. Verification via X-Gitlab-Token header (secret token set during webhook creation).
Timeline
Displaying pipeline status + OAuth: 2–3 business days. Full integration with management: 4–5 days.







