Minimal Vercel serverless API that securely triggers a GitLab CI pipeline.
- Exposes
POST /api/trigger-gitlab - Keeps GitLab secrets on Vercel (server-side only)
- Optionally restricts requests to a single frontend origin
- Validates GitLab OAuth bearer token before triggering CI
api/trigger-gitlab.js- Vercel Function endpointvercel.json- Vercel function/runtime config.env.example- required environment variables
- Install Vercel CLI:
npm i -g vercel
- In this folder, run:
vercel
- In Vercel project settings, add env vars from
.env.example - Redeploy:
vercel --prod
- URL:
https://<your-project>.vercel.app/api/trigger-gitlab - Method:
POST - Headers:
Content-Type: application/jsonAuthorization: Bearer <gitlab_oauth_access_token>
curl -X POST "https://<your-project>.vercel.app/api/trigger-gitlab" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <gitlab_oauth_access_token>" \
-d '{"variables":{"DEPLOY_ENV":"staging"}}'Body fields are optional:
ref- branch/tag to run (defaults toGITLAB_REF)variables- object ofCI/CD variablesto send to pipeline
In your GitLab project:
- Go to
Settings -> CI/CD -> Pipeline triggers - Create a trigger token
- Use:
GITLAB_TRIGGER_TOKEN= trigger tokenGITLAB_PROJECT_ID= numeric project IDGITLAB_REF= default branch (for examplemain)
- Frontend users authenticate with GitLab OAuth PKCE.
- Frontend calls this endpoint with
Authorization: Bearer .... - Endpoint validates token using
GET /api/v4/user. - Endpoint optionally checks:
GITLAB_AUTH_PROJECT_IDaccess (defaults toGITLAB_PROJECT_ID)ALLOWED_GITLAB_USERNAMESallowlist
- Only then it uses server-side
GITLAB_TRIGGER_TOKENto start pipeline.