Automating Website Deployment

Tutorial 4 of 5

Automating Website Deployment With GitHub Pages

1. Introduction

This tutorial aims to help you automate the process of deploying your website using GitHub Pages. It will guide you through setting up GitHub Actions to automatically build and deploy your site whenever you push to your repository.

You will learn:
- How to setup a GitHub repository for your website
- How to use GitHub Actions for continuous deployment
- How to automate website deployment using GitHub Pages

Prerequisites:
- Basic knowledge of Git and GitHub
- A GitHub account
- Basic understanding of YAML (Yet Another Markup Language)

2. Step-by-Step Guide

Setting Up a GitHub Repository

Start by creating a new repository on GitHub. This will hold all the files for your website.

Creating a GitHub Actions Workflow

  1. In your repository, create a new file in the .github/workflows directory and name it deploy.yml.
  2. In deploy.yml, you will specify the actions GitHub should take every time you push to your repository.

Here is an example of what your deploy.yml file might look like:

name: Build and Deploy
on:
  push:
    branches:
      - main
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Install and Build
        run: |
          npm install
          npm run build
      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@4.1.5
        with:
          branch: gh-pages
          folder: build

Explaining the YAML File

  • The name field is the name of your workflow.
  • The on field specifies when the workflow should be triggered. In this case, it will be triggered every time you push to the main branch of your repository.
  • The jobs field specifies the jobs to be run. In this case, there is only one job, build-and-deploy.
  • runs-on specifies the type of machine to run the job on. ubuntu-latest is a Linux environment.
  • steps are the sequence of tasks that make up a job. Each step is run in the same instance of the virtual environment, allowing the actions in subsequent steps to build on prior steps.

3. Code Examples

Example 1: Deploying a Simple Static Website

name: Deploy
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@4.1.5
        with:
          branch: gh-pages
          folder: .

This simple example is for a static website where no build process is necessary. The folder field specifies the root directory of your project.

Example 2: Deploying a Website with a Build Process

name: Build and Deploy
on:
  push:
    branches:
      - main
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Install and Build
        run: |
          npm install
          npm run build
      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@4.1.5
        with:
          branch: gh-pages
          folder: build

This example is for a website that requires a build step, such as a React or Angular app. The Install and Build step runs npm install to install dependencies and npm run build to create a production-ready version of your site.

4. Summary

In this tutorial, we've explored how to automate website deployment using GitHub Pages and GitHub Actions. We've learned how to set up a GitHub repository, create a GitHub Actions workflow, and write YAML files to define actions.

To further your learning, consider exploring different GitHub Actions that can automate other parts of your workflow, such as testing your code or sending notifications.

5. Practice Exercises

Exercise 1: Create a GitHub repository and write a workflow to deploy a simple static website.

Solution: Follow the steps in the guide above and use the first code example. Replace the folder field with the directory of your website files.

Exercise 2: Modify the workflow to deploy a website that requires a build step.

Solution: Follow the steps in the guide and use the second code example. Replace the folder field with the directory of your build files.

Tip for further practice: Try using a different build tool, such as Gulp or Webpack, and modify the Install and Build step to use that tool.