Sub topic 6
Publishing with a GitHub Actions workflow
This chapter assumes you can commit and push with Git, read YAML, and already have a repository with a site folder holding an index.html. You should also know that GitHub Pages can publish from a branch. Here you will publish with a workflow instead.
Why use a workflow
Branch publishing serves a branch and folder as they are, or runs Jekyll on them. A GitHub Actions workflow gives you control over the build. The workflow can run a static site generator, copy only selected files, or upload a folder that is not / or /docs.
Choose a workflow when:
- you build the site with a tool other than Jekyll, or with a Jekyll version or plugins the branch builder does not support
- the files to publish live in a folder such as
siteordist - you want a build step to pass before anything goes live
Choose branch publishing when your files are already ready to serve. It has fewer moving parts.
Switch the publishing source
In the repository, open Settings, then Pages. Under Build and deployment, set Source to GitHub Actions. Without this, the deploy step fails because Pages is still expecting a branch.
How the deployment flows
A deployment has two stages. The first uploads your files as a Pages artifact. The second publishes that artifact to the github-pages environment.
The workflow file
Save this as .github/workflows/pages.yml.
name: Deploy static site to Pages
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v4
- uses: actions/configure-pages@v5
- uses: actions/upload-pages-artifact@v3
with:
path: site
- id: deployment
uses: actions/deploy-pages@v4Each part has a job to do:
| Part | Purpose |
|---|---|
| `on.push.branches` | Runs the workflow on every push to `main`. |
| `workflow_dispatch` | Adds a **Run workflow** button so you can deploy by hand. |
| `permissions` | `pages: write` lets the job publish. `id-token: write` lets `deploy-pages` verify that the deployment comes from this workflow. `contents: read` lets it check out the code. |
| `concurrency` | Runs one deployment at a time. `cancel-in-progress: false` lets a running deployment finish instead of being cut off. |
| `environment` | Uses the `github-pages` environment and shows the live URL on the run page. |
| `path: site` | The folder that becomes the root of your site. |
The id: deployment on the last step matters. The environment.url line reads steps.deployment.outputs.page_url, so it needs that step ID.
Run it and check it
- Commit the workflow file and push to
main. - Open the Actions tab and select the run named "Deploy static site to Pages".
- Wait for the job to finish, then follow the URL shown under the
deployjob.
The site root is the contents of site. A file at site/index.html is served at the top of your Pages URL, and site/style.css is served next to it.
Common errors
- The deploy step fails with a permissions or environment error. Check that
pages: writeandid-token: writeare present, and that Source is set to GitHub Actions. - The workflow succeeds but the site shows a 404. Check that
index.htmlsits at the top of the uploaded folder, not in a subfolder of it. - The workflow never runs. The trigger lists
main. If your default branch has another name, changebranches. - A deployment is blocked by environment rules. The
github-pagesenvironment can restrict which branches deploy. Look under Settings, then Environments.
The Actions log for the failed step normally names the cause. Read it before changing anything.
Exercise
Your workflow runs green, but the live site is still the old version. The upload step uses path: site, and your new files are in a folder named public. What is the likely cause, and what is the fix?
Show answer
The artifact contains only the site folder, so the new files in public were never uploaded. Change path to public, or move the files into site, then push again.
Exercise
Why does concurrency use cancel-in-progress: false in a Pages workflow?
Show answer
So a deployment that is already running finishes. A newer push waits in the queue, which avoids a half-finished deployment being cancelled.