Hosting with GitLab
Use GitLab CI/CD to automatically generate preview links on merge requests, publish your Fern docs when changes are merged to main, and delete preview links after merge.
Prerequisites
- Node.js version 22 or higher
- The Fern CLI installed locally
- A Fern project with a
fernfolder (quickstart)
Add a Fern token to GitLab
Generate a Fern token
Run fern token in your terminal from the directory containing your fern folder. This generates an organization-scoped token that authenticates the Fern CLI in CI/CD.
Copy the token output — you’ll add it to GitLab in the next step.
Add the Fern token as a CI/CD variable
- Log in to GitLab and navigate to your Fern docs repository.
- Go to Settings > CI/CD.
- Scroll to the Variables section, select Expand, then click Add variable.
- Set the key to
FERN_TOKEN, paste the token you generated in the previous step as the value, deselect Protect variable, and click Save changes.
Add a project access token to GitLab
To post preview links on merge requests, you need a GitLab project access token.
Create a project access token
- In your GitLab repository, go to Settings > Access Tokens.
- Click Add new token and configure the following:
- Token name: a descriptive name (e.g.,
fern-preview) - Expiration date: set as needed (you’ll need to regenerate once it expires)
- Role: Reporter
- Scopes: api
- Token name: a descriptive name (e.g.,
- Click Create project access token and copy the token.
Save your token
Save the generated token immediately — it won’t be displayed after you leave the page.
Add the CI/CD pipeline
Create a .gitlab-ci.yml file in the root of your repository. This pipeline validates your API definition, posts a per-branch preview link on each merge request, publishes your docs when changes are merged to main, and deletes the merged branch’s preview deployment.
Commit and push the .gitlab-ci.yml file to your repository. The pipeline runs automatically on merge requests and when changes are merged to your default branch.