Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mhmyu/d78b5718e61179dc27272a9d09827c09 to your computer and use it in GitHub Desktop.
Save mhmyu/d78b5718e61179dc27272a9d09827c09 to your computer and use it in GitHub Desktop.
Deploy Next.js static to GitLab pages

Deploy Next.js static to GitLab pages

Create a basic GitLab CI config:

$ cat .gitlab-ci.yml
image: node

before_script:
  - npm install

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .next/cache/

pages:
  script:
    - npm run-script build && npm run-script export
    - mv out public
  artifacts:
    paths:
      - public
  only:
    - master

Adapat the assets URL to use the subdirectory of your repo name in the way https://<GITLAB_USER_NAME>.gitlab.com/<GITLAB_PROJECT_NAME> (removing the brackets <>):

$ cat next.config.js 
# ...
module.exports = {
  assetPrefix: process.env.NODE_ENV === 'production' ? '/<GITLAB_PROJECT_NAME>' : '',
  # ...

One we push the changes, GitLab CI will trigger a build and will publish the page under https://<GITLAB_USER_NAME>.gitlab.com/<GITLAB_PROJECT_NAME>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment