Table of Contents
Continuous deployment is a vital practice for developers who want to automate the process of publishing their websites. GitHub Pages offers a simple way to host static sites, and integrating it with CI/CD tools can streamline updates and ensure your site is always current. This guide will walk you through setting up continuous deployment for GitHub Pages using popular CI/CD tools like GitHub Actions.
Prerequisites
- A GitHub account with access to your repository
- A repository containing your static website files
- Basic knowledge of Git and GitHub workflows
- Optional: a custom domain for your GitHub Pages site
Setting Up GitHub Actions
GitHub Actions is a powerful CI/CD tool integrated into GitHub. To set it up, create a new workflow file in your repository.
Create the Workflow File
In your repository, navigate to the .github/workflows directory. If it doesn’t exist, create it. Then, add a new file named deploy.yml.
Open deploy.yml and add the following configuration:
name: Deploy to GitHub Pages
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Build project
run: npm run build
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
This workflow triggers on every push to the main branch. It checks out your code, installs dependencies, builds the project, and deploys the build directory to GitHub Pages.
Configuring Your Repository
Ensure your project has a build script defined in package.json. For example:
{
"scripts": {
"build": "your-build-command"
}
}
Set the GitHub Pages source to the gh-pages branch in your repository settings. The workflow will automatically push the built site to this branch.
Testing and Deployment
Push changes to your main branch. GitHub Actions will automatically run the workflow, build your site, and deploy it to GitHub Pages. Visit your site to verify the update.
Additional Tips
- Use custom domains by configuring DNS settings in repository settings.
- Secure your deployment with secrets if using private dependencies or custom tokens.
- Monitor your workflows in the Actions tab for troubleshooting.
By automating deployment with CI/CD tools, you ensure your GitHub Pages site is always up-to-date with minimal manual effort. Happy deploying!