|
| 1 | +# Publishing with GitHub Pages |
| 2 | + |
| 3 | +A strict MkDocs build proves that the documentation can be generated. GitHub |
| 4 | +Pages makes that generated site available to readers on the web. |
| 5 | + |
| 6 | +This project publishes the site at: |
| 7 | + |
| 8 | +<https://michalmaj.github.io/modern-python-project-guide/> |
| 9 | + |
| 10 | +## Build and deployment remain separate |
| 11 | + |
| 12 | +The repository has two workflows with different responsibilities: |
| 13 | + |
| 14 | +- `.github/workflows/ci.yml` validates every pull request and push to `main`, |
| 15 | +- `.github/workflows/pages.yml` publishes documentation after relevant changes |
| 16 | + reach `main`. |
| 17 | + |
| 18 | +A pull request therefore cannot update the public site. It must first pass the |
| 19 | +normal review and `Quality checks` requirement. The merge commit then starts the |
| 20 | +documentation workflow. |
| 21 | + |
| 22 | +## One-time repository setting |
| 23 | + |
| 24 | +The repository owner must enable GitHub Pages once: |
| 25 | + |
| 26 | +1. Open the repository **Settings**. |
| 27 | +2. Select **Pages** under **Code, planning, and automation**. |
| 28 | +3. Under **Build and deployment**, set **Source** to **GitHub Actions**. |
| 29 | + |
| 30 | +The workflow does not publish from a `gh-pages` branch and does not commit the |
| 31 | +generated `site/` directory. |
| 32 | + |
| 33 | +## When deployment runs |
| 34 | + |
| 35 | +The documentation workflow runs when a push to `main` changes its own workflow, |
| 36 | +the Python version, documentation sources, MkDocs configuration, or dependency |
| 37 | +files. It can also be started manually from the **Actions** tab. |
| 38 | + |
| 39 | +The path filter avoids redeploying an unchanged site after unrelated source-code |
| 40 | +changes. Manual dispatch remains available for recovery or verification. |
| 41 | + |
| 42 | +## Build job |
| 43 | + |
| 44 | +The `build` job recreates the site from a clean checkout: |
| 45 | + |
| 46 | +1. check out the repository, |
| 47 | +2. install `uv` and the configured Python version, |
| 48 | +3. install locked development dependencies, |
| 49 | +4. configure GitHub Pages, |
| 50 | +5. run `uv run mkdocs build --strict`, |
| 51 | +6. upload `site/` as a GitHub Pages artifact. |
| 52 | + |
| 53 | +The artifact is the boundary between building and deploying. It contains the |
| 54 | +generated static site, but it is not committed to the repository. |
| 55 | + |
| 56 | +## Deploy job |
| 57 | + |
| 58 | +The `deploy` job waits for the build job. It deploys the uploaded artifact to |
| 59 | +the `github-pages` environment and reports the resulting public URL. |
| 60 | + |
| 61 | +The workflow grants only the permissions it needs: |
| 62 | + |
| 63 | +- `contents: read` reads repository files, |
| 64 | +- `pages: write` creates the Pages deployment, |
| 65 | +- `id-token: write` lets GitHub verify the deployment with OpenID Connect. |
| 66 | + |
| 67 | +The write permissions belong only to the deploy job. The normal CI workflow |
| 68 | +does not receive them. |
| 69 | + |
| 70 | +## Why use an environment? |
| 71 | + |
| 72 | +GitHub Pages deployments use the `github-pages` environment. It records |
| 73 | +deployment history and can enforce protection rules. |
| 74 | + |
| 75 | +The workflow also uses a `pages` concurrency group. A running production |
| 76 | +deployment is allowed to finish instead of being cancelled by a newer run. |
| 77 | + |
| 78 | +## Verify a deployment |
| 79 | + |
| 80 | +After merging a documentation change: |
| 81 | + |
| 82 | +1. open the repository **Actions** tab, |
| 83 | +2. select the **Documentation** workflow, |
| 84 | +3. confirm that **Build documentation** and **Deploy documentation** passed, |
| 85 | +4. open the deployment URL shown by the deploy job, |
| 86 | +5. verify the changed page and navigation. |
| 87 | + |
| 88 | +The existing `Quality checks` status remains the branch-protection requirement. |
| 89 | +Deployment happens after merge, so a deployment failure should be fixed with a |
| 90 | +new pull request rather than by weakening that requirement. |
| 91 | + |
| 92 | +## Public output and secrets |
| 93 | + |
| 94 | +Everything in the generated site is public. Documentation sources and build |
| 95 | +configuration must not contain credentials, private notes, or other sensitive |
| 96 | +information. |
| 97 | + |
| 98 | +This workflow needs no repository secrets. GitHub supplies a short-lived token |
| 99 | +with the declared permissions. |
| 100 | + |
| 101 | +## Custom domains are separate |
| 102 | + |
| 103 | +This project uses the default `github.io` address. A custom domain would require |
| 104 | +additional DNS and repository settings and is intentionally outside this setup. |
| 105 | + |
| 106 | +## Rule of thumb |
| 107 | + |
| 108 | +Validate on pull requests. Publish only from the protected default branch. |
| 109 | + |
| 110 | +Keep generated files out of Git and deploy a reproducible artifact instead. |
| 111 | + |
| 112 | +## Continue |
| 113 | + |
| 114 | +- [Previous: Documentation Site with MkDocs](15_documentation_site_with_mkdocs.md) |
| 115 | +- [Next: Project Checklist](09_checklist.md) |
| 116 | +- [Back to documentation home](index.md) |
0 commit comments