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 API key to GitLab
Generate a Fern API key
Run fern token in your terminal from the directory containing your fern folder. This generates an organization-scoped API key that authenticates the Fern CLI in CI/CD.
Copy the API key output — you’ll add it to GitLab in the next step.
Add the Fern API key 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 API key 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.