Deploying Vite project to GitHub Pages

Vite is a tool that helps in faster and more efficient web development. A project created using the Vite tool is known as a Vite project. When we make a Vite project, we set up all the necessary configurations and files.

GitHub Pages is a feature on GitHub that helps in the creation of static websites using our GitHub repository. These websites are open and accessible by all users on GitHub.

In this Answer, we will discuss how to deploy our Vite project to GitHub pages.

Setting up git on our Vite project

We run the following commands to set up git on our Vite project using our terminal:

  • We use the following command to set up Git infrastructure on our project:

git init
Setting up Git infrastructure
  • We use the following command to add all files and changes in our current directory to our Git repository.

git add .
Adding files to Git repository
  • We use the following command to create a new commit in our Git repository with a descriptive message enclosed in the double quotations.

git commit -m "add: initial files"
Creating new commit
  • We use the following command to rename our default branch as main.

git branch -M main
Renaming our branch
  • We use the following command to add a remote repository with the URL provided. This remote repository acts as a main focus for sharing our project.

git remote add origin https://github.com/username/projectname.git
Adding a remote repository

Here username is the user name of our Git profile while projectname is the name we have defined for our Vite project.

  • Lastly, we use the following command to push all the local commits to our remote repository. Now a connection is established between our local and remote repository.

git push -u origin main
Pushing all commits to remote repository

Deployment setup

After transferring our project to the GitHub repository we move to the deployment setup process.

  • We set the base URL of our project in our vite.config.ts file. Here projectname is the name of our repository.

vite.config.ts
vite.config.ts
  • We create a create a .github folder. In this folder, we make a folder named as workflows which keeps all our workflows to be implemented on our GitHub repository. This folder contains our yml file named as deploy.yml. This allows us to deploy our projects on GitHub automatically. We add the following code to the deploy.yml to set all the steps used by GitHub to setup our workflow.

name: Deploy
on:
push:
branches:
- main
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
- name: Install dependencies
uses: bahmutov/npm-install@v1
- name: Build project
run: npm run build
- name: Upload production-ready build files
uses: actions/upload-artifact@v3
with:
name: production-files
path: ./dist
deploy:
name: Deploy
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: production-files
path: ./dist
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
deploy.yml
  • We use the following command to add all modified and new files in our current directory to the Git staging area.

git add .
Adding all modified and new files
  • By running the following command, we create a commit with a descriptive message in double quotations.

git commit -m "add: deploy workflow"
Creating a commit
  • We push our changes to remote repository using this command.

git push
Pushing our changes to remote repository

Setup on GitHub

After running all the commands above, we can see our directory files on our GitHub.

  • Now we have to ensure that our workflows permissions are set. We go to Settings > Actions > General > and scroll to the Workflow permissions options and adjust the following setting:

Setting workflow permissions setting
Setting workflow permissions setting
  • Then we go to the Actions setting and check if our workflows have executed successfully. If not, we re-run the job. Our succeeded workflow will look like this:

Succeeded workflow
Succeeded workflow
  • Next, we go to Settings > Pages, and in the branch option, we set our repository branch to gh-pages.

Setting branch
Setting branch
  • We go the Settings > Pages where can find our site link.

Site link
Site link
  • We click on the site, and we get the following output:

Output
Output

To study further the project we have created, we can explore the following project link on GitHub:

Conclusion

Deploying a Vite project to GitHub Pages offers a convenient way to showcase and share our web application or static site publicly. By following a few simple steps, we can easily configure and automate the deployment process, ensuring our project is accessible to users on the GitHub Pages platform.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved