This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: read | |
pull-requests: write | |
checks: write # Added permission for creating check runs | |
jobs: | |
comment-deployment-link: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- 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 | |
BRANCH_NAME_LOWER=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]') | |
# Replace non-alphanumeric characters with hyphens | |
BRANCH_NAME_SANITIZED=$(echo "$BRANCH_NAME_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 # Adjust as needed (e.g., 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 | |
CHANGED_FILES=$(echo "$CHANGED_FILES" | tr ' ' '\n') | |
for FILE in $CHANGED_FILES; do | |
# Remove 'website/docs/' prefix | |
FILE_PATH="${FILE#website/docs/}" | |
# Remove the .md extension as per your desired output | |
FILE_PATH="${FILE_PATH%.md}" | |
# Construct the full URL | |
FULL_URL="$DEPLOYMENT_URL/$FILE_PATH" | |
LINKS="$LINKS\n- [$FILE_PATH]($FULL_URL)" | |
done | |
fi | |
# Properly set the multi-line output | |
echo "links<<EOF" >> $GITHUB_OUTPUT | |
echo -e "$LINKS" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
- name: Post deployment links in a check run | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const deploymentUrl = `🚀 Deployment available at: [${{ steps.vercel_url.outputs.deployment_url }}](${{ steps.vercel_url.outputs.deployment_url }})`; | |
const links = `Here are the direct links to the updated files:\n${{ steps.links.outputs.links }}`; | |
const summary = `${deploymentUrl}\n\n${links}`; | |
// Access octokit.rest instead of github | |
const checkRuns = await octokit.rest.checks.listForRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: context.payload.pull_request.head.sha, | |
}); | |
const existingCheckRun = checkRuns.data.check_runs.find( | |
(run) => run.name === 'Vercel Deployment Preview' | |
); | |
if (existingCheckRun) { | |
// Update the existing check run | |
await octokit.rest.checks.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
check_run_id: existingCheckRun.id, | |
output: { | |
title: 'Vercel Deployment Preview', | |
summary: summary, | |
}, | |
conclusion: 'success', | |
}); | |
} else { | |
// Create a new check run | |
await octokit.rest.checks.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
name: 'Vercel Deployment Preview', | |
head_sha: context.payload.pull_request.head.sha, | |
status: 'completed', | |
conclusion: 'success', | |
output: { | |
title: 'Vercel Deployment Preview', | |
summary: summary, | |
}, | |
}); | |
} | |