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)
Start by creating a new repository on GitHub. This will hold all the files for your website.
.github/workflows
directory and name it deploy.yml
.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
name
field is the name of your workflow.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.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.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.
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.
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.
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.