# Hosting with GitLab

To host your Fern docs using GitLab, you will need to [add a Fern token to your repository variables](/learn/docs/developer-tools/git-lab#add-a-token-to-gitlab).

<Accordion title="GitLab CI/CD configuration">
  The following GitLab CI/CD workflow will generate a preview link of your docs on merge request and publish your docs when updates are made to `main`.

  To add this to your GitLab Fern project, create a `.gitlab-ci.yml` file in the root of your repository.

  ```yaml .gitlab-ci.yml
  stages: 
    - check
    - preview_docs
    - publish_docs
    - publish_sdks

  before_script:
    - apt-get update -y
    - apt-get install -y curl
    - curl -sL https://deb.nodesource.com/setup_current.x | bash -
    - apt-get install -y nodejs
    - npm install -g fern-api

  check: 
    stage: check
    rules:
      - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      - if: '$CI_COMMIT_BRANCH == "main"'
    script:
      - echo "Checking API is valid"
      - fern check

  preview_docs:
    stage: preview_docs
    rules:
      - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    script:
      - echo "Running fern generate --docs --preview..."
      - |
        OUTPUT=$(fern generate --docs --preview) || true
        echo "$OUTPUT"
        DEMO_URL=$(echo "$OUTPUT" | grep -oP -m1 '(https://[^\s]+-preview-[^\s]+)(?: )')
        echo "Preview URL: $DEMO_URL"
      - |
        if [ -z "$DEMO_URL" ]; then
          echo "No DEMO_URL found"
          exit 1
        fi
        curl --location --request POST \
          --header "PRIVATE-TOKEN: $REPO_TOKEN" \
          --header "Content-Type: application/json" \
          --url "https://gitlab.com/api/v4/projects/$CI_MERGE_REQUEST_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes" \
          --data-raw "{ \"body\": \"🌿 Preview your docs [here]($DEMO_URL)\" }"
      
              
  publish_docs:
    stage: publish_docs
    rules:
      - if: '$CI_COMMIT_BRANCH == "main"'
    script:
      - echo "Publishing Docs"
      - fern generate --docs

  publish_sdks:
    stage: publish_sdks
    rules:
      - if: '$CI_PIPELINE_SOURCE == "web"'
    script: 
        - echo "Publishing SDKs"
        - fern generate --group ts-sdk --version $VERSION --log-level debug
  ```
</Accordion>

## Add a token to GitLab

<Steps>
  ### Log in

  Log into [GitLab](https://gitlab.com/users/sign_in).

  ### Navigate to CI/CD in settings

  Click on the **Settings** tab in your repository. Then, click on **CI/CD**.

  ### Add variable

  Scroll to the **Variables** section and select **Expand** > **Add variable**. Add your key and value, *deselect* **Protect variable**, and then click **Save changes**.
</Steps>

## Preview docs with GitLab

<Steps>
  ### Contact us

  To get set up with a GitLab pipeline to preview your docs automatically, reach out via your dedicated Slack channel or [email](mailto:support@buildwithfern.com).

  ### Log in

  Log into [GitLab](https://gitlab.com/users/sign_in).

  ### Navigate to Access Tokens

  Click on the **Settings** tab in your repository. Then, click on **Access Tokens**.

  ### Generate project access token

  Click on **Add new token**. You then need to:

  * Add your token name
  * Select an expiry date (note: once the token expires, you will need to generate a new one)
  * Set role to **Reporter**
  * Set scope to **api**

  When finished, click **Create project access token**.

  <Note title="Save your token">
    Be sure to save the generated token - it won't be displayed after you leave the page.
  </Note>

  ### Add token to repository variables

  You can do this using [the steps listed above](/learn/docs/developer-tools/git-lab#add-a-token-to-gitlab).
</Steps>