|
| 1 | +# Deploy your Astro Website to GitHub Pages using GitHub Actions |
| 2 | + |
| 3 | +This guide will walk you through deploying your Astro website to GitHub Pages using GitHub Actions for automated deployment. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- A GitHub repository containing your Astro project |
| 8 | +- GitHub Pages enabled on your repository |
| 9 | +- Basic understanding of GitHub Actions |
| 10 | + |
| 11 | +## Step 1: Configure Astro for GitHub Pages |
| 12 | + |
| 13 | +### 1.1 Update Astro Configuration |
| 14 | + |
| 15 | +Your `astro.config.mjs` already has the correct site URL configured: |
| 16 | + |
| 17 | +```javascript |
| 18 | +export default defineConfig({ |
| 19 | + site: 'https://cv.coderdiaz.com', |
| 20 | + // ... other config |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +**Important**: If you're using a different domain or GitHub Pages URL, update the `site` property to match your GitHub Pages URL format: |
| 25 | + |
| 26 | +- For user/organization pages: `https://username.github.io` |
| 27 | +- For project pages: `https://username.github.io/repository-name` |
| 28 | +- For custom domains: `https://yourdomain.com` |
| 29 | + |
| 30 | +### 1.2 Add Base Path (if needed) |
| 31 | + |
| 32 | +If your repository name is not the same as your GitHub Pages URL, add a base path: |
| 33 | + |
| 34 | +```javascript |
| 35 | +export default defineConfig({ |
| 36 | + site: 'https://username.github.io/repository-name', |
| 37 | + base: '/repository-name', |
| 38 | + // ... other config |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +## Step 2: Create GitHub Actions Workflow |
| 43 | + |
| 44 | +### 2.1 Create Workflow Directory |
| 45 | + |
| 46 | +Create the following directory structure in your repository: |
| 47 | + |
| 48 | +``` |
| 49 | +.github/ |
| 50 | +└── workflows/ |
| 51 | + └── deploy.yml |
| 52 | +``` |
| 53 | + |
| 54 | +### 2.2 Create the Deployment Workflow |
| 55 | + |
| 56 | +Create a file at `.github/workflows/deploy.yml` with the following content: |
| 57 | + |
| 58 | +```yaml |
| 59 | +name: Deploy to GitHub Pages |
| 60 | + |
| 61 | +on: |
| 62 | + # Trigger the workflow every time you push to the `main` branch |
| 63 | + # Using a different branch name? Replace `main` with your branch’s name |
| 64 | + push: |
| 65 | + branches: [main] |
| 66 | + # Allows you to run this workflow manually from the Actions tab on GitHub. |
| 67 | + workflow_dispatch: |
| 68 | + |
| 69 | +# Allow this job to clone the repo and create a page deployment |
| 70 | +permissions: |
| 71 | + contents: read |
| 72 | + pages: write |
| 73 | + id-token: write |
| 74 | + |
| 75 | +jobs: |
| 76 | + build: |
| 77 | + runs-on: ubuntu-latest |
| 78 | + steps: |
| 79 | + - name: Checkout your repository using git |
| 80 | + uses: actions/checkout@v4 |
| 81 | + - name: Install, build, and upload your site |
| 82 | + uses: withastro/action@v3 |
| 83 | + # with: |
| 84 | + # path: . # The root location of your Astro project inside the repository. (optional) |
| 85 | + # node-version: 20 # The specific version of Node that should be used to build your site. Defaults to 20. (optional) |
| 86 | + # 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) |
| 87 | + |
| 88 | + deploy: |
| 89 | + needs: build |
| 90 | + runs-on: ubuntu-latest |
| 91 | + environment: |
| 92 | + name: github-pages |
| 93 | + url: ${{ steps.deployment.outputs.page_url }} |
| 94 | + steps: |
| 95 | + - name: Deploy to GitHub Pages |
| 96 | + id: deployment |
| 97 | + uses: actions/deploy-pages@v4 |
| 98 | +``` |
| 99 | +
|
| 100 | +## Step 3: Configure GitHub Pages |
| 101 | +
|
| 102 | +### 3.1 Enable GitHub Pages |
| 103 | +
|
| 104 | +1. Go to your repository on GitHub |
| 105 | +2. Navigate to **Settings** → **Pages** |
| 106 | +3. Under **Source**, select **GitHub Actions** |
| 107 | +4. Click **Save** |
| 108 | +
|
| 109 | +### 3.2 Configure Pages Settings |
| 110 | +
|
| 111 | +- **Source**: GitHub Actions |
| 112 | +- **Branch**: Leave as default (GitHub Actions will handle deployment) |
| 113 | +
|
| 114 | +## Step 4: Deploy Your Website |
| 115 | +
|
| 116 | +### 4.1 Push Your Changes |
| 117 | +
|
| 118 | +```bash |
| 119 | +git add . |
| 120 | +git commit -m "Add GitHub Actions deployment workflow" |
| 121 | +git push origin main |
| 122 | +``` |
| 123 | + |
| 124 | +### 4.2 Monitor Deployment |
| 125 | + |
| 126 | +1. Go to your repository on GitHub |
| 127 | +2. Click on the **Actions** tab |
| 128 | +3. You should see your workflow running |
| 129 | +4. Wait for the deployment to complete |
| 130 | + |
| 131 | +## Step 5: Verify Deployment |
| 132 | + |
| 133 | +Once the workflow completes successfully: |
| 134 | + |
| 135 | +1. Go to your repository's **Settings** → **Pages** |
| 136 | +2. You should see a green checkmark indicating successful deployment |
| 137 | +3. Click on the provided URL to view your live website |
| 138 | + |
| 139 | +## Customization Options |
| 140 | + |
| 141 | +### Environment Variables |
| 142 | + |
| 143 | +You can add environment variables to your workflow for different deployment environments: |
| 144 | + |
| 145 | +```yaml |
| 146 | +- name: Install, build, and upload your site |
| 147 | + uses: withastro/action@v1 |
| 148 | + env: |
| 149 | + NODE_ENV: production |
| 150 | + # Add other environment variables as needed |
| 151 | +``` |
| 152 | + |
| 153 | +### Custom Build Commands |
| 154 | + |
| 155 | +If you need custom build commands, you can modify the workflow: |
| 156 | + |
| 157 | +```yaml |
| 158 | +- name: Install dependencies |
| 159 | + run: npm install |
| 160 | + |
| 161 | +- name: Build site |
| 162 | + run: npm run build |
| 163 | + |
| 164 | +- name: Upload to GitHub Pages |
| 165 | + uses: actions/upload-pages-artifact@v3 |
| 166 | + with: |
| 167 | + path: ./dist |
| 168 | +``` |
| 169 | +
|
| 170 | +### Multiple Branches |
| 171 | +
|
| 172 | +To deploy from different branches or on pull requests: |
| 173 | +
|
| 174 | +```yaml |
| 175 | +on: |
| 176 | + push: |
| 177 | + branches: [main, develop] |
| 178 | + pull_request: |
| 179 | + branches: [main] |
| 180 | +``` |
| 181 | +
|
| 182 | +## Troubleshooting |
| 183 | +
|
| 184 | +### Common Issues |
| 185 | +
|
| 186 | +1. **Build Failures** |
| 187 | +
|
| 188 | + - Check the Actions tab for error messages |
| 189 | + - Ensure all dependencies are properly installed |
| 190 | + - Verify your Astro configuration is correct |
| 191 | +
|
| 192 | +2. **404 Errors** |
| 193 | +
|
| 194 | + - Verify the `site` URL in `astro.config.mjs` matches your GitHub Pages URL |
| 195 | + - Check if you need to add a `base` path for project pages |
| 196 | + |
| 197 | +3. **Permission Errors** |
| 198 | + |
| 199 | + - Ensure the workflow has the correct permissions |
| 200 | + - Check that GitHub Pages is enabled in repository settings |
| 201 | + |
| 202 | +4. **Asset Loading Issues** |
| 203 | + - Verify all assets are in the `public/` directory |
| 204 | + - Check that image paths are correct |
| 205 | + |
| 206 | +### Debugging Steps |
| 207 | + |
| 208 | +1. **Check Workflow Logs** |
| 209 | + |
| 210 | + - Go to Actions tab in your repository |
| 211 | + - Click on the failed workflow run |
| 212 | + - Review the detailed logs for error messages |
| 213 | + |
| 214 | +2. **Test Locally** |
| 215 | + |
| 216 | + ```bash |
| 217 | + npm run build |
| 218 | + npm run preview |
| 219 | + ``` |
| 220 | + |
| 221 | +3. **Verify Configuration** |
| 222 | + - Check `astro.config.mjs` for correct site URL |
| 223 | + - Ensure all integrations are properly configured |
| 224 | + |
| 225 | +## Advanced Configuration |
| 226 | + |
| 227 | +### Custom Domain Setup |
| 228 | + |
| 229 | +If you're using a custom domain: |
| 230 | + |
| 231 | +1. Add your custom domain in repository Settings → Pages |
| 232 | +2. Update the `site` URL in `astro.config.mjs` |
| 233 | +3. Add a `CNAME` file to your `public/` directory with your domain |
| 234 | + |
| 235 | +### SEO Optimization |
| 236 | + |
| 237 | +Your current configuration includes sitemap generation. Ensure it's working by: |
| 238 | + |
| 239 | +1. Checking that `@astrojs/sitemap` is in your integrations |
| 240 | +2. Verifying the sitemap is generated in the build output |
| 241 | +3. Submitting your sitemap to search engines |
| 242 | + |
| 243 | +### Performance Optimization |
| 244 | + |
| 245 | +Consider adding these optimizations: |
| 246 | + |
| 247 | +1. **Image Optimization**: Use Astro's built-in image optimization |
| 248 | +2. **Code Splitting**: Ensure your components are properly optimized |
| 249 | +3. **Caching**: Configure appropriate cache headers |
| 250 | + |
| 251 | +## Maintenance |
| 252 | + |
| 253 | +### Regular Updates |
| 254 | + |
| 255 | +- Keep Astro and dependencies updated |
| 256 | +- Monitor GitHub Actions for any deprecation warnings |
| 257 | +- Regularly test your deployment workflow |
| 258 | + |
| 259 | +### Monitoring |
| 260 | + |
| 261 | +- Set up notifications for failed deployments |
| 262 | +- Monitor your website's performance and uptime |
| 263 | +- Keep track of GitHub Pages service status |
| 264 | + |
| 265 | +## Resources |
| 266 | + |
| 267 | +- [Astro Documentation](https://docs.astro.build/en/guides/deploy/github/) |
| 268 | +- [GitHub Actions Documentation](https://docs.github.com/en/actions) |
| 269 | +- [GitHub Pages Documentation](https://docs.github.com/en/pages) |
| 270 | + |
| 271 | +--- |
| 272 | + |
| 273 | +**Note**: This documentation is specific to your Astro project configuration. Adjust the site URL and other settings according to your specific deployment requirements. |
0 commit comments