Skip to content

Deploy Workshop to GitHub Pages #7

Deploy Workshop to GitHub Pages

Deploy Workshop to GitHub Pages #7

name: Deploy Workshop to GitHub Pages
permissions: write-all
on:
workflow_dispatch:
inputs:
release_type:
description: 'Type of version bump'
required: true
type: choice
options:
- minor
- major
default: minor
jobs:
deploy:
name: Build and deploy workshop
runs-on: ubuntu-24.04
steps:
- name: Checkout sources
uses: actions/checkout@v2
with:
submodules: recursive
fetch-depth: 0
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.145.0'
extended: true
- name: Setup yq
uses: chrisdickinson/setup-yq@latest
with:
yq-version: '4.45.1'
yq-url: 'https://github.com/mikefarah/yq/releases/download/v{version}/yq_{platform}_{arch}'
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Cache go dependencies
uses: actions/cache@v4
with:
path: /tmp/hugo_cache
key: ${{ runner.os }}-hugomod-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-hugomod-
- name: Bump version and create tag
id: bumpversion
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git pull --rebase
TAG=$(bumpversion --list "${{ inputs.release_type }}" | awk -F= '/new_version=/ { print $2 }')
echo "tag_name=${TAG}" >> $GITHUB_OUTPUT
BASE_URL="$(yq .baseURL hugo.yaml)"
echo "base_url=${BASE_URL}" >> $GITHUB_OUTPUT
# Update README with new release link
awk "/Latest versions of the workshop are:/ { print; print \"- [v${TAG}](https://splunk.github.io/observability-workshop/)\";next }1" README.md |
awk "NR==1,/Latest versions of the workshop are:/{c=3} c&&c-- " > README.new.md
mv README.new.md README.md
git fetch --tags origin
git add README.md
git commit --amend -m "Releasing v${TAG}"
git tag -a "v${TAG}" -m "Version ${TAG}"
git push --follow-tags origin main || { echo 'Push failed. git pull --rebase from upstream and attempt another release.'; exit 1; }
- name: Build Hugo site
run: |
hugo --minify --destination "public" --baseURL "${{ steps.bumpversion.outputs.base_url }}observability-workshop" --noChmod --cacheDir /tmp/hugo_cache
- name: Create redirect handler for old URLs
run: |
cat > public/404.html << 'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<script>
// Handle redirects from old versioned URLs to new root URLs
(function() {
var path = window.location.pathname;
// Match /observability-workshop/latest/ or /observability-workshop/v{version}/ patterns
var versionPattern = /^\/observability-workshop\/(latest|v[\d.]+)\/(.*)/;
var match = path.match(versionPattern);
if (match) {
// Extract the path after the version (e.g., "en/index.html")
var newPath = match[2];
// Preserve query string and hash if present
var newUrl = window.location.origin + '/observability-workshop/' + newPath + window.location.search + window.location.hash;
// Redirect to new URL
window.location.replace(newUrl);
} else {
// If no version pattern matched, show a friendly 404 message
document.write('<h1>404 - Page Not Found</h1>');
document.write('<p>The page you are looking for does not exist.</p>');
document.write('<p>Visit the <a href="/observability-workshop/">Splunk Observability Workshop homepage</a>.</p>');
}
})();
</script>
<noscript>
<meta http-equiv="refresh" content="0; url=/observability-workshop/">
</noscript>
</head>
<body>
<p>Redirecting... If you are not redirected, <a href="/observability-workshop/">click here</a>.</p>
</body>
</html>
EOF
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
publish_branch: gh-pages
force_orphan: false
user_name: ${{ github.actor }}
user_email: ${{ github.actor }}@users.noreply.github.com
commit_message: "Deploy v${{ steps.bumpversion.outputs.tag_name }}"
- name: Publish GitHub release
uses: softprops/action-gh-release@v1
with:
name: v${{ steps.bumpversion.outputs.tag_name }}
tag_name: v${{ steps.bumpversion.outputs.tag_name }}