Skip to content

Commit ef2d186

Browse files
authored
docs: add cleanup stage for preview deployments (gitlab) (#5097)
1 parent e240f2c commit ef2d186

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
tags: ["developer-tools"]
3+
---
4+
5+
## Clean up GitLab preview deployments on merge
6+
7+
The updated [GitLab CI/CD pipeline](/learn/docs/developer-tools/gitlab) now includes a `cleanup_preview` stage that deletes a merge request's preview deployment once the MR merges into the default branch, so stale previews don't linger.
8+
9+
To adopt this workflow, replace your `.gitlab-ci.yml` file with the updated version.
10+
11+
<Button intent="none" outlined rightIcon="arrow-right" href="/learn/docs/developer-tools/git-lab#add-the-cicd-pipeline">Read the docs</Button>

fern/products/docs/pages/developer-tools/gitlab.mdx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Set up GitLab CI/CD to automatically publish your Fern docs when ch
44
---
55

66

7-
Use GitLab CI/CD to automatically generate preview links on merge requests and publish your Fern docs when changes are merged to `main`.
7+
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.
88

99
<Info title="Prerequisites">
1010
- Node.js version 18 or higher
@@ -63,17 +63,18 @@ Save the generated token immediately — it won't be displayed after you leave t
6363

6464
## Add the CI/CD pipeline
6565

66-
Create a `.gitlab-ci.yml` file in the root of your repository. This pipeline validates your API definition, posts preview links on merge requests, and publishes your docs when changes are merged to `main`.
66+
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.
6767

6868
```yaml .gitlab-ci.yml
6969
stages:
7070
- check
7171
- preview_docs
7272
- publish_docs
73+
- cleanup_preview
7374

7475
before_script:
7576
- apt-get update -y
76-
- apt-get install -y curl
77+
- apt-get install -y curl jq
7778
- curl -sL https://deb.nodesource.com/setup_current.x | bash -
7879
- apt-get install -y nodejs
7980
- npm install -g fern-api
@@ -82,7 +83,7 @@ check:
8283
stage: check
8384
rules:
8485
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
85-
- if: '$CI_COMMIT_BRANCH == "main"'
86+
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
8687
script:
8788
- echo "Checking API is valid"
8889
- fern check
@@ -92,11 +93,11 @@ preview_docs:
9293
rules:
9394
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
9495
script:
95-
- echo "Generating preview..."
96+
- echo "Generating preview for branch $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME..."
9697
- |
97-
OUTPUT=$(fern generate --docs --preview) || true
98+
OUTPUT=$(fern generate --docs --preview --id "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" --force 2>&1) || true
9899
echo "$OUTPUT"
99-
DEMO_URL=$(echo "$OUTPUT" | grep -oP -m1 '(https://[^\s]+-preview-[^\s]+)(?: )')
100+
DEMO_URL=$(echo "$OUTPUT" | grep -oE -m1 '(https://[^[:space:]]+-preview-[^[:space:]]+) ' | tr -d ' ')
100101
echo "Preview URL: $DEMO_URL"
101102
- |
102103
if [ -z "$DEMO_URL" ]; then
@@ -112,10 +113,32 @@ preview_docs:
112113
publish_docs:
113114
stage: publish_docs
114115
rules:
115-
- if: '$CI_COMMIT_BRANCH == "main"'
116+
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
116117
script:
117118
- echo "Publishing Docs"
118119
- fern generate --docs
120+
121+
cleanup_preview:
122+
stage: cleanup_preview
123+
rules:
124+
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
125+
script:
126+
- echo "Looking up merged MR for commit $CI_COMMIT_SHA..."
127+
- |
128+
MR_INFO=$(curl -sf --header "PRIVATE-TOKEN: $REPO_TOKEN" \
129+
"https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/repository/commits/$CI_COMMIT_SHA/merge_requests") || {
130+
echo "Failed to query MRs for commit — skipping cleanup"
131+
exit 0
132+
}
133+
SOURCE_BRANCH=$(echo "$MR_INFO" | jq -r 'map(select(.state == "merged")) | .[0].source_branch // empty')
134+
135+
if [ -z "$SOURCE_BRANCH" ]; then
136+
echo "No merged MR found for this commit (likely a direct push to main) — skipping cleanup"
137+
exit 0
138+
fi
139+
140+
echo "Deleting preview for branch: $SOURCE_BRANCH"
141+
fern docs preview delete --id "$SOURCE_BRANCH" || echo "Preview deletion returned non-zero — it may already be gone"
119142
```
120143
121-
Commit and push the `.gitlab-ci.yml` file to your repository. The pipeline runs automatically when changes are merged to `main`.
144+
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.

0 commit comments

Comments
 (0)