🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Deploy Astro to GitLab Pages with GitLab CI

Deploy Astro to GitLab Pages: a .gitlab-ci.yml pages job, publishing dist, site and base for project URLs, caching node_modules, and adding a custom domain.

#Astro#GitLab Pages#Deployment#CI

To deploy Astro to GitLab Pages, add a .gitlab-ci.yml with a job named pages that runs npm ci and npm run build, then publishes the dist folder. Set site to your gitlab.io URL and, for a project site, base to the project name. GitLab runs the job on every push to the default branch and serves the result.

Key Takeaways

  • The job must be named pages (or use the pages keyword) for GitLab to deploy it.
  • GitLab Pages traditionally serves a folder named public; point it at dist with pages.publish, or move the folder.
  • Project sites live at https://<namespace>.gitlab.io/<project>/, so set base: '/<project>'.
  • Use a node:22 image; current Astro needs Node.js 22.23.4 or newer.

1. Set site and base

For a project site at https://acme.gitlab.io/website/:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://acme.gitlab.io',
  base: '/website',
});

For a user or group site (a project named acme.gitlab.io) or a custom domain, leave base out. Links you write by hand need the base prefix; our base path tip shows a helper.

2. Add .gitlab-ci.yml

# .gitlab-ci.yml
image: node:22-alpine

pages:
  stage: deploy
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - .npm/
  script:
    - npm ci --cache .npm --prefer-offline
    - npm run build
  pages:
    publish: dist
  artifacts:
    paths:
      - dist
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

pages.publish tells GitLab which folder to serve. Older GitLab versions only served a folder named public and used a top-level publish key; if your instance rejects pages: inside the job, replace those lines with a final script step that swaps the folders:

  script:
    - npm ci --cache .npm --prefer-offline
    - npm run build
    - rm -rf public && mv dist public
  artifacts:
    paths:
      - public

Removing public/ is safe in CI because Astro has already copied its contents into the build. The cache key on package-lock.json reuses the npm cache until dependencies change; our CI build cache tip covers caching Astro’s own build data as well.

3. Push and check

Push to the default branch and open Build and then Pipelines to watch the job. When it passes, Deploy and then Pages shows the site URL. The first deploy can take a few minutes to appear.

If styles are missing, the site is almost always a project site without base set: the HTML asks for /_astro/... at the domain root instead of /website/_astro/....

404 page

GitLab Pages serves 404.html from the published folder for missing paths. Create src/pages/404.astro; see our custom 404 tip.

Custom domain

Under Deploy, Pages, choose New Domain, add your domain and create the DNS records GitLab shows, including the TXT verification record. GitLab can issue a Let’s Encrypt certificate automatically. Then set site to the new domain, remove base, and push. Our custom domain tip and custom domains docs explain DNS in general.

Merge request previews

GitLab supports parallel Pages deployments for merge requests on some tiers, using a path_prefix under the pages keyword. If your plan does not include them, deploy previews to another host; see our preview deployments tip. For the GitHub equivalent, read our GitHub Pages guide. Themes from our Astro theme catalog are static builds that deploy with this file once site and base are set.

FAQ

How do I deploy Astro to GitLab Pages?

Add a pages job to .gitlab-ci.yml that runs npm ci and npm run build and publishes dist. Set site and, for project sites, base in astro.config.mjs.

Why is my Astro site on GitLab Pages missing CSS?

Project sites are served under /<project>/. Set base: '/<project>' in astro.config.mjs so asset URLs include the project path.

Does GitLab Pages need the folder to be called public?

Older versions did. Current GitLab lets you set the folder with pages.publish. If yours does not, rename dist to public as the last step of the job.

Can GitLab Pages run Astro SSR?

No. GitLab Pages serves static files only. Use a static build, or deploy SSR routes to a platform that runs Node.js.