How to use actions/upload-pages-artifact

GitHub Actions has revolutionized the way developers automate their workflows, making it easier than ever to build, test, and deploy applications directly from GitHub repositories. One particularly useful feature is the ability to upload and store artifacts, especially when deploying static sites using GitHub Pages. The actions/upload-pages-artifact action simplifies this process, allowing you to quickly save and retrieve deployment artifacts in your workflows. If you’re hosting a static website on GitHub Pages, this action is essential.

What is actions/upload-pages-artifact?

actions/upload-pages-artifact is an official GitHub Action designed to upload artifact files intended for GitHub Pages deployment. These artifacts typically include your website’s HTML, CSS, JavaScript, and other assets generated during a build process. When paired with the actions/deploy-pages action, it ensures your content is seamlessly deployed to GitHub Pages.

Why Use actions/upload-pages-artifact?

There are a few key reasons why you might want to use this action in your workflow:

  • Ensures a Smooth Deployment Process – Automatically uploads website files so they can be deployed efficiently.
  • Works with Custom Build Steps – Allows you to generate static files using build tools like Jekyll, Next.js, Hugo, or Gatsby and upload them for deployment.
  • Integrates With GitHub Actions Workflow – Works seamlessly within a CI/CD pipeline, reducing manual steps.

How to Use actions/upload-pages-artifact

To add this action to your GitHub Actions workflow, you’ll need a YAML file under the .github/workflows/ directory in your repository. Let’s walk through a basic setup.

1. Define Your Workflow File

Your workflow should include the following components:

  • A trigger event, such as a push to the main branch.
  • Steps to install dependencies, build the project, and prepare content.
  • The actions/upload-pages-artifact step to upload files.

2. Example Workflow File

Here’s a simple workflow that builds and uploads a static site for GitHub Pages:

name: Deploy to GitHub Pages

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install dependencies
        run: npm install

      - name: Build static site
        run: npm run build

      - name: Upload artifact for GitHub Pages
        uses: actions/upload-pages-artifact@v2
        with:
          path: ./dist

In this example:

  • The workflow runs on every push to the main branch.
  • It installs dependencies and builds the website.
  • The resulting static files from the ./dist folder are uploaded as an artifact.

Configuring the Upload Path

By default, you need to specify the directory where your built files exist. In the example above, the output directory ./dist is uploaded as an artifact. If your project generates its output in a different folder, make sure to update the path value accordingly.

Deploying Your GitHub Pages Site

Once you’ve uploaded the artifact, the next step is to deploy it using actions/deploy-pages. That deployment step might look like this in your workflow:

jobs:
  deploy:
    runs-on: ubuntu-latest
    needs: build
    permissions:
      pages: write
      id-token: write
    steps:
      - name: Deploy to GitHub Pages
        uses: actions/deploy-pages@v2
Process Builder Example of Automation

Adding this job ensures that the artifact uploaded in the previous step is automatically published on GitHub Pages.

Common Pitfalls & Troubleshooting

While setting up the actions/upload-pages-artifact action, you might encounter some common issues:

  • Incorrect Path: Make sure the correct build output directory is specified in the workflow.
  • Missing Build Step: Ensure that your static files are generated before running the upload action.
  • Permission Errors: The deploy step requires repository admin permissions for GitHub Pages.

Conclusion

Using actions/upload-pages-artifact in your GitHub Actions workflow significantly streamlines the process of deploying static sites to GitHub Pages. By following the setup outlined in this guide, you can automate your builds, upload artifacts efficiently, and ensure smooth deployments with minimal effort. Start integrating this action today and take full advantage of GitHub Actions for your static website hosting!

I'm Ava Taylor, a freelance web designer and blogger. Discussing web design trends, CSS tricks, and front-end development is my passion.
Back To Top