Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI bundle previews #32

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 0 additions & 76 deletions .github/workflows/bundle.yml

This file was deleted.

157 changes: 157 additions & 0 deletions .github/workflows/deploy-bundle-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
name: Deploy Bundle Preview

on:
pull_request:
branches:
- '*'

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
- name: Configure Git Credentials
uses: de-vri-es/setup-git-credentials@v2
with:
credentials: ${{ secrets.GIT_CREDENTIALS }}

- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16.20.2

- name: Install Dependencies
run: npm ci

- name: Extract Branch Names
shell: bash
run: |
# transform branch names in form of `refs/heads/main` to `main`
draft_branch=$(basename ${{ github.event.pull_request.head.ref }})
echo "draft_branch=$draft_branch" >> $GITHUB_OUTPUT
id: extract_branch

- name: Build UI Bundle Preview
run: |
set -o pipefail
gulp lint |& tee $GITHUB_WORKSPACE/build.log
UI_ROOT_PATH_PREFIX=${{ steps.extract_branch.outputs.draft_branch }} gulp preview:build |& tee $GITHUB_WORKSPACE/build.log

- name: Check Build Result
id: logFail
if: failure()
run: |
MULTILINE_LOG=$(cat $GITHUB_WORKSPACE/build.log)
echo "BUILD_FAILURE<<EOF" >> $GITHUB_ENV
echo $MULTILINE_LOG >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV

- name: Create Build Success Comment
if: ${{ success() && github.event.pull_request.number }}
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body: |
UI bundle preview build successful! :white_check_mark:
Deploying preview to GitHub Pages.
reactions: rocket

- name: Create Build Failure Comment
if: ${{ failure() && github.event.pull_request.number }}
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body: |
UI bundle preview build failure! :x:
> ${{ env.BUILD_FAILURE }}
reactions: confused

- name: Find Comment
if: ${{ success() && github.event.pull_request.number }}
uses: peter-evans/find-comment@v2
id: fc
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'mlr'
body-includes: UI bundle preview build successful!
direction: last

- name: Deploy to GitHub Pages
if: success()
run: |
git clone https://github.com/$GITHUB_REPOSITORY.git pages
cd pages
git checkout gh-pages

# If there was previously a build for the preview, then remove it
# so we get a clean build. This is needed in case a follow up
# build of the same pull request contains content deletions.
rm -rf ${{ steps.extract_branch.outputs.draft_branch }}

mkdir -p ${{ steps.extract_branch.outputs.draft_branch }}
cp -r ../public/* ${{ steps.extract_branch.outputs.draft_branch }}/.

# Records the repository that originally triggered the build so we can post back
# comments upon clean up of a stale draft if it still has an open pull request.
echo "${{ github.event.repository.full_name }}" > ${{ steps.extract_branch.outputs.draft_branch }}/.github_source_repository

git add .
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git commit --allow-empty -m "Auto-deployed from GitHub Actions"
git push -u origin gh-pages

- name: Obtain GitHub Pages build URL
if: success()
run: |
sleep 5 # Allow time for build to initiate
build_url=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.COMMENT_GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" 'https://api.github.com/repos/${{ github.event.repository.full_name }}/pages/builds/latest' \
| jq -r .url)
echo "url=$build_url" >> $GITHUB_OUTPUT
id: ghpages_build

- name: Wait for Github Pages deployment
if: success()
run: |
for i in {1..60}; do
build_status=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.COMMENT_GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" '${{ steps.ghpages_build.outputs.url }}' \
| jq -r .status)

if [ "$build_status" == "built" ]; then echo "Deploy is complete."
exit 0
else
echo "Deploy is not complete. Status: $build_status. Retrying in 10 seconds..."
sleep 10
fi
done
echo "Deploy is still not complete after approximately 10 minutes."
exit 1

- name: Get GitHub Pages Preview URL
if: success()
shell: bash
run: |
echo "url=https://riptano.github.io/${{ github.event.repository.name }}/${{ steps.extract_branch.outputs.draft_branch }}/" >> $GITHUB_OUTPUT
id: draft_url

- name: Update comment
if: ${{ steps.fc.outputs.comment-id != '' }}
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
comment-id: ${{ steps.fc.outputs.comment-id }}
body: |
Deployment successful! [View preview](${{ steps.draft_url.outputs.url }})
reactions: hooray
21 changes: 21 additions & 0 deletions .github/workflows/dispatch-gh-pages-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Dispatch Antora Build and Deploy

on:
pull_request:
branches:
- '*'

jobs:
dispatch-deploy:
runs-on: ubuntu-latest

steps:
- name: Dispatch Antora Build and Deploy
uses: convictional/trigger-workflow-and-wait@v1.6.1
with:
owner: riptano
repo: datastax-docs-site
github_token: ${{ secrets.DISPATCH_GITHUB_TOKEN }}
github_user: mlr
workflow_file_name: gh-pages-build.yml
client_payload: '{ "build_repository": "${{ github.event.repository.full_name }}", "build_branch": "main", "draft_branch": "${{ github.event.pull_request.head.ref }}", "pull_request_number": "${{ github.event.pull_request.number }}", "ui_bundle_repository": "${{ github.event.repository.full_name }}", "ui_bundle_branch": "${{ github.event.pull_request.head.ref }}" }'
125 changes: 125 additions & 0 deletions .github/workflows/gh-pages-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
name: Cleanup GitHub Pages

on:
schedule:
# Runs daily at 1AM UTC which is 6PM pacific
- cron: '0 1 * * *'

workflow_dispatch:
inputs:
stale_threshold:
description: 'Threshold after which the preview build is considered to be stale'
default: '2 weeks'
type: string

jobs:
remove_stale_previews:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: 'gh-pages'
fetch-depth: 0

- name: Determine Previews to Delete
id: find_previews
run: |
# Get the stale threshold from the workflow parameter
# and calculate the timestamp for the threshold
STALE_THRESHOLD="${{ github.event.inputs.stale_threshold || '2 weeks' }}"
THRESHOLD_DATE=$(date --date="$STALE_THRESHOLD ago" +%s)

# Find and collect previews older than the specified threshold, skipping "main" and ".git"
STALE_PREVIEWS=()
for dir in $(find . -type d -mindepth 1 -maxdepth 1); do
# Check if the preview name is "main" or ".git" and skip it
if [ "$(basename "$dir")" == "main" ] || [ "$(basename "$dir")" == ".git" ]; then
continue
fi

DIR_DATE=$(git log -1 --format="%ct" -- "$dir")
if [ -n "$DIR_DATE" ] && [ "$DIR_DATE" -lt "$THRESHOLD_DATE" ]; then
STALE_PREVIEWS+=("$(basename "$dir")")
fi
done

if [ -z "${STALE_PREVIEWS[*]}" ]; then
echo "Did not find any stale previews based on $STALE_THRESHOLD threshold. Done."
else
echo "Previews determined to be stale based on $STALE_THRESHOLD threshold: ${STALE_PREVIEWS[*]}"
fi

# Set the list of previews to be removed as an output
echo "previews_to_delete=${STALE_PREVIEWS[*]}" >> $GITHUB_OUTPUT

- name: Notify Open Pull Requests of Preview Removal
if: ${{ steps.find_previews.outputs.previews_to_delete != '' }}
run: |
# Notify open pull requests of their preview being deleted
# but don't fail on this step for any reason
set +e

STALE_PREVIEWS=(${{ steps.find_previews.outputs.previews_to_delete }})
STALE_THRESHOLD="${{ github.event.inputs.stale_threshold || '2 weeks' }}"

# Iterate through the previews and look for open PRs
for dir in "${STALE_PREVIEWS[@]}"; do
if [ -f "$dir/.github_source_repository" ]; then
# Get the source repository for the preview build
SOURCE_REPO=$(cat "$dir/.github_source_repository")
echo "Source repository for $dir is $SOURCE_REPO"
else
# Without .github_source_repository we don't know where to post the comment so we'll skip
echo "Could not determine source repository for $dir. Skipping."
continue
fi

# Check if there are open pull requests for the source repository
OPEN_PRS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/$SOURCE_REPO/pulls?state=open")

# Check if any open pull requests match the preview branch name
PREVIEW_PRS=$(echo $OPEN_PRS | jq -r --arg dir "$dir" '.[] | select(.head.ref == $dir) | .number' 2>/dev/null)

if [[ -n "$PREVIEW_PRS" ]]; then
# Comment on the open PRs
for pr_number in $PREVIEW_PRS; do
COMMENT_BODY="The preview build for this pull request has been cleaned up due to being stale.\n\nPreview builds that were $STALE_THRESHOLD old or older were automatically removed to maintain a tidy GitHub Pages site.\n\nYou can rebuild the preview at any time by pushing a new commit to this pull request:\n\n\`\`\`\ngit checkout $dir\ngit commit --allow-empty -m 'rebuild preview'\ngit push origin $dir\n\`\`\`"
curl -X POST -d "{\"body\":\"$COMMENT_BODY\"}" -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/$SOURCE_REPO/issues/$pr_number/comments"
done
else
echo "No open pull requests for $dir. No notification needed."
fi
done

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Remove Previews and Push to GitHub Pages
if: ${{ steps.find_previews.outputs.previews_to_delete != '' }}
run: |
# Get the list of directories to delete from the previous step's output
STALE_PREVIEWS=(${{ steps.find_previews.outputs.previews_to_delete }})

# Get the stale threshold from the workflow parameter
# and calculate the timestamp for the threshold
STALE_THRESHOLD="${{ github.event.inputs.stale_threshold || '2 weeks' }}"

echo "Removing stale previews based on $STALE_THRESHOLD threshold: ${STALE_PREVIEWS[*]}"

# Remove the directories and their contents
for dir in "${STALE_PREVIEWS[@]}"; do
rm -rf "$dir"
done

# Commit and push the changes
git add .

if [[ `git status --porcelain` ]]; then
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git commit -m "Removed previews older than $STALE_THRESHOLD via ${{ github.event_name }} by ${{ github.actor }}"
git push origin gh-pages
else
echo "No previews were deleted."
fi
Loading
Loading