-
Notifications
You must be signed in to change notification settings - Fork 970
162 lines (135 loc) · 5.68 KB
/
preview-link.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
name: Vercel deployment preview link generator
on:
pull_request:
types: [opened, synchronize]
paths:
- 'website/docs/docs/**'
- 'website/docs/best-practices/**'
- 'website/docs/guides/**'
- 'website/docs/faqs/**'
- 'website/docs/reference/**'
permissions:
contents: write
pull-requests: write
jobs:
update-pr-description:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install necessary tools
run: |
sudo apt-get update
sudo apt-get install -y jq curl
- name: Generate Vercel deployment URL
id: vercel_url
run: |
# Get the branch name
BRANCH_NAME="${{ github.head_ref }}"
# Convert to lowercase and sanitize branch name
BRANCH_NAME_SANITIZED=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g')
# Construct the deployment URL
DEPLOYMENT_URL="https://docs-getdbt-com-git-${BRANCH_NAME_SANITIZED}-dbt-labs.vercel.app"
echo "deployment_url=$DEPLOYMENT_URL" >> $GITHUB_OUTPUT
- name: Wait for deployment to be accessible
id: wait_for_deployment
run: |
DEPLOYMENT_URL="${{ steps.vercel_url.outputs.deployment_url }}"
echo "Waiting for deployment at $DEPLOYMENT_URL to become accessible..."
MAX_ATTEMPTS=60 # Wait up to 10 minutes
SLEEP_TIME=10 # Check every 10 seconds
ATTEMPTS=0
while [ $ATTEMPTS -lt $MAX_ATTEMPTS ]; do
STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$DEPLOYMENT_URL")
if [ "$STATUS_CODE" -eq 200 ]; then
echo "Deployment is accessible."
break
else
echo "Deployment not yet accessible (status code: $STATUS_CODE). Waiting..."
sleep $SLEEP_TIME
ATTEMPTS=$((ATTEMPTS + 1))
fi
done
if [ $ATTEMPTS -eq $MAX_ATTEMPTS ]; then
echo "Deployment did not become accessible within the expected time."
exit 1
fi
- name: Get changed files
id: files
run: |
# Get the list of changed files in the specified directories
CHANGED_FILES=$(git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.sha }}" | grep -E '^website/docs/(docs|best-practices|guides|faqs|reference)/.*\.md$' || true)
echo "Changed files:"
echo "$CHANGED_FILES"
# Check if any files were changed
if [ -z "$CHANGED_FILES" ]; then
echo "No documentation files were changed."
echo "changed_files=" >> $GITHUB_OUTPUT
else
# Convert line-separated files to space-separated for easier handling
CHANGED_FILES="$(echo "$CHANGED_FILES" | tr '\n' ' ')"
echo "changed_files=$CHANGED_FILES" >> $GITHUB_OUTPUT
fi
- name: Generate file preview links
id: links
run: |
DEPLOYMENT_URL="${{ steps.vercel_url.outputs.deployment_url }}"
CHANGED_FILES="${{ steps.files.outputs.changed_files }}"
if [ -z "$CHANGED_FILES" ]; then
echo "No changed files found in the specified directories."
LINKS="- No documentation files were changed."
else
LINKS=""
# Convert CHANGED_FILES back to newline-separated for processing
echo "$CHANGED_FILES" | tr ' ' '\n' > files.txt
while IFS= read -r FILE; do
# Remove 'website/docs/' prefix
FILE_PATH="${FILE#website/docs/}"
# Remove the .md extension
FILE_PATH="${FILE_PATH%.md}"
# Construct the full URL
FULL_URL="$DEPLOYMENT_URL/$FILE_PATH"
LINKS="$LINKS\n- [$FILE_PATH]($FULL_URL)"
done < files.txt
rm files.txt
fi
# Properly set the multi-line output
echo "links<<EOF" >> $GITHUB_OUTPUT
echo -e "$LINKS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Update PR description with deployment links
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = context.issue.number;
// Fetch the current PR description
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
let body = pullRequest.body || '';
// Define the markers
const startMarker = '<!-- vercel-deployment-preview -->';
const endMarker = '<!-- end-vercel-deployment-preview -->';
// Build the deployment content
const deploymentContent = `${startMarker}
🚀 **Deployment preview available!**
**Deployment URLs:** [${{ steps.links.outputs.links }}](${{ steps.links.outputs.links }})
**Direct links to the updated files:**
${{ steps.links.outputs.links }}
---
${endMarker}`;
// Remove existing deployment content between markers
const regex = new RegExp(`${startMarker}[\\s\\S]*?${endMarker}`, 'g');
body = body.replace(regex, '').trim();
// Prepend the new deployment content
body = `${deploymentContent}\n\n${body}`;
// Update the PR description
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
body: body,
});