This guide will walk you through deploying your Astro website to GitHub Pages using GitHub Actions for automated deployment.
- A GitHub repository containing your Astro project
- GitHub Pages enabled on your repository
- Basic understanding of GitHub Actions
Your astro.config.mjs already has the correct site URL configured:
export default defineConfig({
site: 'https://cv.coderdiaz.com',
// ... other config
});Important: If you're using a different domain or GitHub Pages URL, update the site property to match your GitHub Pages URL format:
- For user/organization pages:
https://username.github.io - For project pages:
https://username.github.io/repository-name - For custom domains:
https://yourdomain.com
If your repository name is not the same as your GitHub Pages URL, add a base path:
export default defineConfig({
site: 'https://username.github.io/repository-name',
base: '/repository-name',
// ... other config
});Create the following directory structure in your repository:
.github/
└── workflows/
└── deploy.yml
Create a file at .github/workflows/deploy.yml with the following content:
name: Deploy to GitHub Pages
on:
# Trigger the workflow every time you push to the `main` branch
# Using a different branch name? Replace `main` with your branch’s name
push:
branches: [main]
# Allows you to run this workflow manually from the Actions tab on GitHub.
workflow_dispatch:
# Allow this job to clone the repo and create a page deployment
permissions:
contents: read
pages: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout your repository using git
uses: actions/checkout@v6
- name: Install, build, and upload your site
uses: withastro/action@v5
# with:
# path: . # The root location of your Astro project inside the repository. (optional)
# node-version: 22 # The specific version of Node that should be used to build your site. Defaults to 22. (optional)
# package-manager: pnpm@latest # The Node package manager that should be used to install dependencies and build your site. Automatically detected based on your lockfile. (optional)
# build-cmd: pnpm run build # The command to run to build your site. Runs the package build script/task by default. (optional)
deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4- Go to your repository on GitHub
- Navigate to Settings → Pages
- Under Source, select GitHub Actions
- Click Save
- Source: GitHub Actions
- Branch: Leave as default (GitHub Actions will handle deployment)
git add .
git commit -m "Add GitHub Actions deployment workflow"
git push origin main- Go to your repository on GitHub
- Click on the Actions tab
- You should see your workflow running
- Wait for the deployment to complete
Once the workflow completes successfully:
- Go to your repository's Settings → Pages
- You should see a green checkmark indicating successful deployment
- Click on the provided URL to view your live website
You can add environment variables to your workflow for different deployment environments:
- name: Install, build, and upload your site
uses: withastro/action@v5
env:
NODE_ENV: production
# Add other environment variables as neededIf you need custom build commands, you can modify the workflow:
- name: Install dependencies
run: npm install
- name: Build site
run: npm run build
- name: Upload to GitHub Pages
uses: actions/upload-pages-artifact@v4
with:
path: ./distTo deploy from different branches or on pull requests:
on:
push:
branches: [main, develop]
pull_request:
branches: [main]-
Build Failures
- Check the Actions tab for error messages
- Ensure all dependencies are properly installed
- Verify your Astro configuration is correct
-
404 Errors
- Verify the
siteURL inastro.config.mjsmatches your GitHub Pages URL - Check if you need to add a
basepath for project pages
- Verify the
-
Permission Errors
- Ensure the workflow has the correct permissions
- Check that GitHub Pages is enabled in repository settings
-
Asset Loading Issues
- Verify all assets are in the
public/directory - Check that image paths are correct
- Verify all assets are in the
-
Check Workflow Logs
- Go to Actions tab in your repository
- Click on the failed workflow run
- Review the detailed logs for error messages
-
Test Locally
npm run build npm run preview
-
Verify Configuration
- Check
astro.config.mjsfor correct site URL - Ensure all integrations are properly configured
- Check
If you're using a custom domain:
- Add your custom domain in repository Settings → Pages
- Update the
siteURL inastro.config.mjs - Add a
CNAMEfile to yourpublic/directory with your domain
Your current configuration includes sitemap generation. Ensure it's working by:
- Checking that
@astrojs/sitemapis in your integrations - Verifying the sitemap is generated in the build output
- Submitting your sitemap to search engines
Consider adding these optimizations:
- Image Optimization: Use Astro's built-in image optimization
- Code Splitting: Ensure your components are properly optimized
- Caching: Configure appropriate cache headers
- Keep Astro and dependencies updated
- Monitor GitHub Actions for any deprecation warnings
- Regularly test your deployment workflow
- Set up notifications for failed deployments
- Monitor your website's performance and uptime
- Keep track of GitHub Pages service status
Note: This documentation is specific to your Astro project configuration. Adjust the site URL and other settings according to your specific deployment requirements.