Skip to content

Merge pull request #20 from RedGuides/auto/submodule-updates #55

Merge pull request #20 from RedGuides/auto/submodule-updates

Merge pull request #20 from RedGuides/auto/submodule-updates #55

Workflow file for this run

name: Build ReadGuides Documentation & Deploy via SSH
on:
push:
branches: [ "master" ]
workflow_dispatch:
permissions:
contents: read
concurrency:
group: readguides-${{ github.ref }}
cancel-in-progress: true
jobs:
build-and-deploy:
runs-on: ubuntu-latest
# Only run on the main repository, not on forks
if: github.repository == 'RedGuides/readguides'
env:
# Environment variables for deployment and auth
TARGET_HOST: ${{ secrets.TARGET_HOST }}
TARGET_USER: ${{ secrets.TARGET_USER }}
TARGET_DIR: ${{ secrets.TARGET_DIR }}
TARGET_OWNER: ${{ secrets.TARGET_OWNER }}
# Environment variables for discussion mapper
REDGUIDES_HOST: ${{ secrets.REDGUIDES_HOST }}
REDGUIDES_USER: ${{ secrets.REDGUIDES_USER }}
REDGUIDES_DB_USER: ${{ secrets.REDGUIDES_DB_USER }}
REDGUIDES_DB_PASS: ${{ secrets.REDGUIDES_DB_PASS }}
REDGUIDES_DB_NAME: ${{ secrets.REDGUIDES_DB_NAME }}
steps:
# Checkout source WITHOUT submodules first
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false
# Setup SSH agent with keys for submodules and private dependencies
- name: Setup SSH agent
uses: webfactory/ssh-agent@v0.9.1
with:
ssh-private-key: |
${{ secrets.SUBMODULES_SSH_KEY }}
${{ secrets.READGUIDES_SSH_KEY }}
# Configure Git to use SSH for GitHub and GitLab submodules
- name: Configure Git for SSH submodules
run: |
git config --global url."git@github.com:".insteadOf "https://github.com/"
git config --global url."git@gitlab.com:".insteadOf "https://gitlab.com/"
# Add known hosts
ssh-keyscan github.com >> ~/.ssh/known_hosts
ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
# Initialize submodules using SSH
- name: Initialize submodules
run: |
git submodule sync --recursive
git submodule update --init --recursive --depth=1
# Prepare Python
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
# (optional) Cache pip packages to speed-up subsequent runs
- name: Cache pip packages
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
# Install MkDocs & plugins
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Generate documentation pages
- name: Generate documentation pages
run: python gen_pages.py
# Add database host to known_hosts for discussion mapper
- name: Add database host to known_hosts
run: |
ssh-keyscan -H ${{ env.REDGUIDES_HOST }} >> ~/.ssh/known_hosts
# Generate discussion map (requires SSH access to database)
- name: Generate discussion map
run: python automation/generate_discussion_map.py
# Build the static site
- name: Build readguides documentation
run: mkdocs build --clean
# Create a tar.gz archive of the build
- name: Compress artefact
run: |
tar -czf readguides_docs.tar.gz -C site .
####################################################################
# SSH section – copy archive & unpack remotely
####################################################################
# (optional) Pre-accept host key to avoid the prompt
- name: Add host to known_hosts
run: |
ssh-keyscan -H ${{ env.TARGET_HOST }} >> ~/.ssh/known_hosts
# Copy the archive over
- name: Copy archive via scp
run: |
scp -o StrictHostKeyChecking=no readguides_docs.tar.gz \
${{ env.TARGET_USER }}@${{ env.TARGET_HOST }}:/tmp/
# Run remote commands: unpack, chown, atomic swap
- name: Extract and swap on server
run: |
# Run the commands via SSH
ssh ${{ env.TARGET_USER }}@${{ env.TARGET_HOST }} << 'EOF'
set -e
# Use the variables defined in the workflow
TARGET_DIR="${{ env.TARGET_DIR }}"
TARGET_OWNER="${{ env.TARGET_OWNER }}"
# Check if variables are empty
if [ -z "$TARGET_DIR" ] || [ -z "$TARGET_OWNER" ]; then
echo "Error: TARGET_DIR or TARGET_OWNER is empty." >&2
exit 1
fi
echo "Deploying readguides documentation to $TARGET_DIR"
# Deployment commands
sudo rm -rf "${TARGET_DIR}.new"
sudo mkdir -p "${TARGET_DIR}.new"
sudo tar -xzf /tmp/readguides_docs.tar.gz -C "${TARGET_DIR}.new"
sudo chown -R "${TARGET_OWNER}" "${TARGET_DIR}.new"
if [ -d "${TARGET_DIR}" ]; then
sudo mv "${TARGET_DIR}" "${TARGET_DIR}.old"
fi
sudo mv "${TARGET_DIR}.new" "${TARGET_DIR}"
sudo rm -rf "${TARGET_DIR}.old"
sudo rm -f /tmp/readguides_docs.tar.gz
echo "✅ ReadGuides documentation deploy complete"
EOF
# Purge Cloudflare cache for /docs only
- name: Purge Cloudflare cache
# Only run if the previous steps were successful
if: success()
uses: NathanVaughn/actions-cloudflare-purge@master
with:
# Map existing secrets to the expected input names for this action
cf_zone: ${{ secrets.CLOUDFLARE_ZONE_ID }}
cf_auth: ${{ secrets.CLOUDFLARE_API_TOKEN }}
# Purge only the /docs directory
prefixes: "www.redguides.com/docs/"