GitHub Actions Workflow for Publishing GitHub Pages
This document provides an overview of a GitHub Actions workflow designed to automate the deployment of a Jekyll-based static site to GitHub Pages. The workflow is triggered by specific events and includes two main jobs: build_pages and deploy.
Workflow Triggers
The workflow is triggered under the following conditions:
- Push Events:
- Activates when changes are pushed to the
mainbranch. - Only triggers if changes are made within the
pages/**directory.
- Activates when changes are pushed to the
- Manual Trigger (
workflow_dispatch):- Allows manual execution of the workflow via the GitHub UI.
Jobs in the Workflow
1. Build Pages Job (build_pages)
Conditions:
- Runs only if the trigger is from the
mainbranch (github.ref == 'refs/heads/main').
Environment:
- Runs on:
ubuntu-latest.
Steps:
- Checkout Repository:
- Uses the
actions/checkout@v4action to clone the repository into the workspace.
- Uses the
- Setup Pages:
- Configures the environment for GitHub Pages using
actions/configure-pages@v5.
- Configures the environment for GitHub Pages using
- Install Ruby and Bundler:
- Sets up Ruby version
3.0and installs Bundler with caching enabled usingruby/setup-ruby@v1.
- Sets up Ruby version
- Install Dependencies:
- Navigates to the
pagesdirectory and installs dependencies usingbundle install.
- Navigates to the
- Build with Jekyll:
- Builds the Jekyll site using
actions/jekyll-build-pages@v1. - Specifies the source directory (
./pages) and the destination directory (./output_dir).
- Builds the Jekyll site using
- Upload Artifact:
- Uploads the built site (located in
./output_dir) as a GitHub artifact nameddeploy_artifactusingactions/upload-pages-artifact@v3.
- Uploads the built site (located in
2. Deployment Job (deploy)
Conditions:
- Runs only if the trigger is from the
mainbranch (github.ref == 'refs/heads/main'). - Depends on the successful completion of the
build_pagesjob.
Permissions:
- Grants the necessary permissions for deploying to GitHub Pages:
contents: readpages: writeid-token: write
Environment:
- Specifies the
github-pagesenvironment. - Sets the URL of the deployed site to
$.
Steps:
- Deploy to GitHub Pages:
- Deploys the previously uploaded artifact (
deploy_artifact) to GitHub Pages usingactions/deploy-pages@v4.
- Deploys the previously uploaded artifact (
Key Features
- Selective Triggering:
- The workflow ensures that it only runs when relevant changes are made (e.g., within the
pages/**directory).
- The workflow ensures that it only runs when relevant changes are made (e.g., within the
- Dependency Management:
- Uses Bundler to manage and cache dependencies, ensuring efficient builds.
- Artifact Sharing:
- The
build_pagesjob generates an artifact (deploy_artifact) that is shared with thedeployjob for publishing.
- The
- Environment Configuration:
- Configures the
github-pagesenvironment with appropriate permissions and outputs the deployed site’s URL.
- Configures the
Example Use Case
This workflow is ideal for automating the deployment of Jekyll-based static sites to GitHub Pages. By separating the build and deployment processes into distinct jobs, it ensures modularity and reusability while maintaining clarity in the workflow structure.