Implementing a Robust Deployment Workflow with GitHub Actions

Introduction

This document outlines the steps involved in setting up a static website built with 11ty (Eleventy) and deployed using GitHub Pages and GitHub Actions. It aims to provide a clear and concise guide for developers who want to automate their deployment process and host their 11ty sites on GitHub Pages.

Prerequisites

Before proceeding, ensure you have the following:

Steps

Project Setup (11ty)

If you haven't already, set up your 11ty project with the following file structure:

prespedro-portfolio/
├── src/          (11ty source files)
│   └── index.njk
├── .eleventy.js   (11ty configuration)
├── .gitignore
├── CNAME           (custom domain file)
├── package-lock.json
└── package.json

Configure your .eleventy.js file to use src as the input directory and _site as the output directory:

module.exports = function (eleventyConfig) {
  return {
    dir: {
      input: "src",
      output: "_site",
    },
  };
};

Create a .gitignore file to exclude node_modules and _site from version control:

node_modules
_site

Create a CNAME file in the root directory with your custom domain (e.g., presidentpedro.com).

Install your project dependencies using npm install.

Branching Strategy

Establish a branching strategy with three key branches:

GitHub Actions Workflow

Automate your deployment process using GitHub Actions:

  1. Create a .github/workflows directory in the src branch.
  2. Create a deploy.yml file in the workflows directory with the following content:
name: Deploy to GitHub Pages

on:
  push:
    branches:
      - src

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

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

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

      - name: Install dependencies
        run: npm install

      - name: Build 11ty site
        run: npm run build

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: $
          publish_dir: ./_site
          publish_branch: build
          cname: presidentpedro.com

GitHub Pages Setup

Configure GitHub Pages to serve your site:

Repository Settings

Ensure that GitHub Actions has the necessary permissions:

Deployment Process

Deploy your site by following these steps:

Troubleshooting

Address common issues:

Benefits

This workflow provides several benefits:

This document should serve as a valuable resource for you and anyone else who wants to set up GitHub Pages with GitHub Actions and 11ty. Feel free to add more details or customize it to fit your specific needs.

Sources

The following resources were helpful in developing this workflow:

Integrating GitHub Desktop (Optional)

GitHub Desktop can simplify Git operations:

Install GitHub Desktop

If you don't have it already, download and install GitHub Desktop from desktop.github.com.

Add Your Repository

Open GitHub Desktop and add your repository.

Branch Management

Use GitHub Desktop to easily switch between branches, create new branches, view history, and manage merges.

Using GitHub Desktop with the Workflow

Perform the following steps visually:

Workflow with VS Code

Combine VS Code and GitHub Desktop:

Revised Workflow Steps (with GitHub Desktop)

Here's the workflow using GitHub Desktop:

Repository Restructuring (using GitHub Desktop):

GitHub Actions Workflow (using VS Code and GitHub Desktop):

Monitor GitHub Actions:

Benefits of Using GitHub Desktop:

By using GitHub Desktop, you can focus on code in VS Code while GitHub Desktop handles Git, streamlining your workflow.