Deploy to Vercel from a Selfhosted Gitlab instance

Deploy your Application to Vercel directly from a self-hosted GitLab instance with this step-by-step guide. Learn how to configure GitLab CI/CD pipelines, securely manage your Vercel API token, and automate deployments for your frontend apps

In this guide, we are deploying a Next.js application to Vercel from a local GitLab instance using CI/CD and a custom runner.

Prerequisites

  • A GitLab instance running with Docker & Portainer (in the Cloud or on a local network)
  • Node.js and npm installed locally on your PC
  • A Vercel account
  • A Next.js project

Prepare Vercel for CLI Deployments

  1. Install Vercel Cli on your Computer
 npm i -g vercel
  1. Login to your Vercel Account
vercel login

choose your authentication provider and click enter

  • This should open your browser, where you have to login to your Vercel account.
  • Once logged in, you will get a success message. You can return to your console.
  1. Create a Vercel project by running this command
vercel 
  • you can continue with the basic options, except for 'Link to your existing project?' enter 'n' for no.
  1. open the .vercel directory on your project on the machine, there should be your projectId and orgId.

Create a GitLab Project and Push Your Code

0:00
/0:17
  • to create a GitLab project open your GitLab's dashboard
  • Click on the "New project' button
  • Set the same project name as your Next.js project.
  • Set the access level for your Project and remove the checkmark for initialize the repository with a README.

Setup your CI / CD pipeline

  1. Head to Vercel and create a new account token
0:00
/0:21
  • Set a custom name for the token so you can recognise it later.
  • Select the scopes you want to grant the token
  • set an expiration day.
  • copy your token and paste it somewhere safe. For security reasons this token cannot be shown a second time.
  1. Set your environment variables in GitLab
  • Head back to Gitlab, open your project and navigate to your projects settings > CI/CD > Variables
  • create a variable for your Token: enter VERCEL_TOKEN under Key and under Value paste your Token
  • create a variable for your Project ID: enter VERCEL_PROJECT_ID under Key and under Value paste your projectId from the project.json file
  • create a variable for your Org ID: enter VERCEL_ORG_ID under Key and under Value paste your projectId from the project.json file
The end Result should look like this

Set Up a GitLab Runner

  1. Login to your portainer web console - if you havent installed Portainer yet, check out my other guide - i promise, its quick
  2. Open your Docker environment
  3. Click on Containers in the sidebar, then click Add container in the top right
  4. Specify a name for the runner, in my case i called it testrunner, because i will delete it after the guide.
  5. Enter the GitLab Runner image
gitlab/gitlab-runner:latest
  1. on the bottom open Volumes and map additional volume:
  • under container paste this: /var/run/docker.sock and click on bind
  • under host also paste this: /var/run/docker.sock
  1. select your GitLab's network in the Network tab
  2. in the Restart policy tab select 'always'
  3. Deploy the container
  4. Click on Console and connect as root
  5. Paste this snippet replace YOURGITLABDOMAIN.COM with your domain like gitlab.clickdavid.com
openssl s_client -showcerts -connect YOURGITLABDOMAIN.COM:443 -servername YOURGITLABDOMAIN.COM < /dev/null 2>/dev/null | openssl x509 -outform PEM > /etc/gitlab-runner/certs/YOURGITLABDOMAIN.COM.crt
  1. Return back to your GitLab project, click settings > CI/CD
  2. Go to Runners > Create a Runner
  3. configure your runner: tags: docker, enable Run untagged jobs, set a runner description and click on Create runner
  4. Copy the Snippet shown in Step 1
  5. Paste this snippet in the portainer console
  6. Click enter, when it asks your GitLab instance url
  7. Enter the name for the runner
  8. Set the executor to docker
  9. Enter following default image: node:22.14.0
  10. Start the runner with:
gitlab-runner run

Create the Ci file in your Projects root directory

  1. Create the .gitlab-ci.yml file in your projects root with following contents:
stages:
  - build
  - deploy

.standard-rules:
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

frontend-build-job:
  stage: build
  image: node:22.14.0
  script:
    - npm install
    - npm run build

frontend-deploy-job:
  stage: deploy
  image: node:22.14.0
  script:
    - npx vercel --prod --token $VERCEL_TOKEN --yes
  1. Commit & Push the changes to your main branch
git add .
git commit -m "testing deployment"
git push --set-upstream https://YOURGITLABINSTANCE.COM/david/testproject.git master
🥳
Done, thats it! Not easy, but doable

If you found this guide useful, please consider subscribing or leaving a tip. Your support helps me keep sharing these kind guides!