
To deploy GitHub Pages with GitHub Actions, you'll need to create a new workflow file in your repository. This file should be named `.github/workflows/deploy.yml` and contain the necessary configuration to automate the deployment process.
The `deploy.yml` file should have a `name` field that specifies the workflow's name, such as "Deploy GitHub Pages". This helps identify the workflow in the GitHub Actions interface.
GitHub Actions will automatically run the workflow whenever code is pushed to the repository's main branch, allowing you to automate the deployment of your GitHub Pages site.
Prerequisites
Before we dive into deploying your GitHub Pages, you'll need to make sure you have the following prerequisites in place.
Node and npm are required, and I'll be using version 16.13.2 of Node and 8.1.2 of npm in this tutorial.
Git is also necessary, and I'll be working with version 2.29.1.windows.1.
A GitHub account is a must-have for deploying your pages.
To get started, you'll need to have these tools installed and ready to go.
A fresh viewpoint: Github Gh Pages
Install and Configure
To deploy your GitHub Pages, you'll need to configure a few things. First, your React app needs to be accessible to anyone who visits the homepage URL you specified, which is typically at a URL like https://gitname.github.io/react-gh-pages.
To do this, you'll need to create a new file in your project at .github/workflows/deploy.yml and paste in the YAML below. This will trigger a GitHub Action that will deploy your site automatically whenever you push changes to the `main` branch.
The GitHub Action will clone your repository, create a page deployment, and then deploy your site to GitHub Pages. You can customize the action to use a different branch name or Node version if needed. Here's a summary of the YAML configuration:
After setting up the GitHub Action, commit the new workflow file and push it to GitHub. Your site should now be published and automatically deployed whenever you push changes to your repository.
Install NPM Package
To install the gh-pages npm package, you'll need to run the command npm install gh-pages --save-dev in your terminal. This will install the package on your computer and document the React app's dependence on it in the package.json file.
The gh-pages npm package is a development dependency, which means it's not required for the app to run, but it's necessary for deployment. You can verify that it's installed by checking the package.json file.
Now that the package is installed, you can proceed with the next step, which is to add deployment scripts to the package.json file.
A fresh viewpoint: How to Deploy Mern App on Netlify
Add Homepage to package.json
To add a homepage to your package.json file, you'll need to open the file in a text editor. Use any text editor you want, such as Visual Studio Code.
Add a homepage property in this format: https://{username}.github.io/{repo-name}. For example, if your GitHub repository is named "react-gh-pages", your homepage property would be "https://gitname.github.io/react-gh-pages".
Readers also liked: How to Add People to Facebook Page
The homepage property should be added to the package.json file, which is where your project's metadata is stored. This is where you can also find the "name", "version", and "private" properties.
Here's an example of what your package.json file might look like after adding the homepage property:
Make sure to save the changes to your package.json file, and you're all set!
Configure Astro
To configure Astro, you'll need to set the site and base options in astro.config.mjs. This is where you define the core settings for your Astro project.
You can then use these settings to customize your Astro site. For example, you might want to set the site title or description.
In some cases, you might need to make additional configuration changes to get your Astro site up and running.
On a similar theme: How to Host Site on Github
A fresh viewpoint: Astro Github Pages
Repository Configuration
To configure your repository, start by adding a "remote" that points to the GitHub repository you created earlier. This will allow you to sync your local repository with the remote one.
You'll need to navigate to your local repository's settings and add a new remote with the URL of your GitHub repository. This is a crucial step in deploying your GitHub Pages.
Here's a summary of the steps to add a remote:
- Navigate to your local repository's settings
- Add a new remote with the URL of your GitHub repository
Once you've added the remote, you can configure GitHub Pages to deploy your React app. This will make your app accessible to anyone who visits the homepage URL you specified.
Configure
Configure your repository to deploy your site automatically with GitHub Actions. GitHub Actions is a powerful tool that allows you to automate tasks, including deploying your site.
To use GitHub Actions, create a new file in your project at .github/workflows/deploy.yml and paste in the YAML below. This will trigger the workflow every time you push to the main branch.
Here is a step-by-step guide to creating the deploy.yml file:
- Create a new file in your project at .github/workflows/deploy.yml and paste in the YAML below.
- Commit the new workflow file and push it to GitHub.
- On GitHub, go to your repository’s Settings tab and find the Pages section of the settings.
- Choose GitHub Actions as the Source of your site.
This will allow you to run the workflow manually from the Actions tab on GitHub, and the GitHub Action will automatically deploy your site when you push changes to your repository.
Base

You need to set the base option in Astro to configure your website properly. This is especially important if your repository name is not the same as the root of your website.
A value for base is required to tell Astro that your repository name is the root of your website. For example, if your repository is named /my-repo, you would set the base value to /my-repo.
In fact, the base value should start with a forward slash, like /my-blog. This tells Astro that your website's root is /my-repo, rather than the default /.
For another approach, see: Deploy Website for Free
GitHub Pages Configuration
To configure GitHub Pages, you'll need to create a repository named username.github.io, where username is your GitHub username. This repository will serve as the source for your GitHub Pages site.
First, create a new repository on GitHub by navigating to the GitHub website and clicking on the "+" button in the top right corner. Fill in the repository name as username.github.io and click on the "Create repository" button. If you already have a repository, you can rename it to username.github.io.
Curious to learn more? Check out: How to Create Web Page Layout
Next, push the files of your Hexo folder to the default branch of your repository. The default branch is usually main, but older repositories may use the master branch. To push the main branch to GitHub, use the command $ git push -u origin main.
The public/ folder should not be uploaded by default, so make sure the .gitignore file contains the line public/. This will prevent the public/ folder from being pushed to GitHub.
To configure GitHub Actions for deployment, you'll need to create a new file named .github/workflows/pages.yml in your repository. This file will contain the configuration for the GitHub Actions workflow.
Here's an example of what the .github/workflows/pages.yml file might look like:
```
name: Pages
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
- name: Cache NPM dependencies
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
- name: Deploy to GitHub Pages
uses: actions/deploy-pages@v4
```
This configuration will deploy your site to GitHub Pages using the GitHub Actions workflow. You can customize this configuration to suit your needs.
Once you've created the .github/workflows/pages.yml file, you'll need to commit and push the changes to your repository. After the deployment is finished, you can check the webpage at username.github.io to see your site in action.
Deployment
To deploy your GitHub Pages site, you can use GitHub Actions to automate the process.
Astro maintains an official GitHub Action, withastro/action, that simplifies the deployment process with minimal configuration required.
You'll need to create a .yml file in the .github/workflows directory, which we'll call deploy.yml, to set up the workflow.
The workflow will run a job called Build and Deploy whenever a commit is pushed to the main branch, using git to checkout the current branch and compile your production-ready build.
To configure the deployment, you'll need to add a step to use the github-pages-deploy-action action, specifying the name of the folder where your compiled code gets pushed into and the branch you'd like to push it to.
You can customize the deployment step to suit your needs, including deploying to a different repository or specifying a target folder within a branch.
Deploy Action
Deploying your Astro site to GitHub Pages is a straightforward process that can be automated using GitHub Actions. This tool allows you to trigger a workflow on any event that GitHub supports.
To get started, you'll need to create a `.yml` file in the `.github/workflows` directory, which we'll call `deploy.yml`. This file will contain the necessary steps to build and deploy your site.
You can configure the deployment step in multiple ways to suit your needs. For example, you can deploy to a different repository by leveraging the `repository-name` field.
Here are the basic steps to deploy your Astro site to GitHub Pages:
- Create a new file in your project at `.github/workflows/deploy.yml` and paste in the YAML below.
- Configure the workflow to trigger on every push to the `main` branch.
- Add steps to install, build, and upload your site.
- Add a step to deploy to GitHub Pages.
- Commit the new workflow file and push it to GitHub.
Note that you can customize the workflow to fit your specific needs, and you can also use the official Astro action to deploy your project with minimal configuration.
GitHub and Astro
Deploying GitHub Pages requires some configuration, but don't worry, it's straightforward.
To configure Astro for GitHub Pages, you need to set the site and base options in astro.config.mjs. This is a crucial step in getting everything set up correctly.
You can then use GitHub Pages to host your Astro site, which is a great way to get your project online quickly and easily.
By following these simple steps, you'll be able to deploy your GitHub Pages site in no time.
Featured Images: pexels.com


