Skip to content

Commit 58c1968

Browse files
authored
Use actions/deploy-pages to deploy docs in template (#578)
Resolves #561 Resolve #574 Switches from the current branch based workflow for deploying MkDocs documentation to GitHub Pages in template to instead use `actions/deploy-pages` and removes suggestion in post-generation message displayed to user to set repository wide write permissions for Github Actions workflows in favour of using granular permissions in workflow file. I originally completely removed the post-generation message as I thought by using the `actions/deploy-pages` approach we might be able to avoid having to do additional configuration of repository before site will deploy, but unfortunately it seems the default source setting for GitHub Pages on a new repository is branch so unless this is switched to GitHub Actions before doing the initial workflow run, the deploy job fails. I couldn't see any options for configuring this via CLI under `gh repo edit`. Tested this works at https://github.com/matt-graham/docs-deploy-test and everything seems to work as expected provided manual configuration steps are done as indicated in updated message.
1 parent 9543edb commit 58c1968

File tree

4 files changed

+57
-67
lines changed

4 files changed

+57
-67
lines changed

hooks/post_gen_project.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,12 @@ def main(initialise_git_repository: str, deploy_docs_to_github_pages: str) -> in
8686
return _EXIT_FAILURE
8787
if deploy_docs_to_github_pages == "True":
8888
print(
89-
"The 'Documentation' GitHub Actions workflow has been set up to push the "
90-
"built HTML documentation to a branch gh-pages on pushes to main for "
91-
"deploying as a GitHub Pages website. To allow the GitHub Actions bot to "
92-
"push to the gh-pages branch you need to enable 'Read and write "
93-
"permissions' under 'Workflow permissions' at\n\n"
94-
"{{cookiecutter.__repo_url}}/settings/actions\n\n"
95-
"After the 'Documentation' workflow has successfully completed at least "
96-
"once you will also need to configure the repository to deploy a GitHub "
97-
"pages site from the content on the gh-pages branch by going to\n\n"
89+
"The 'Documentation' GitHub Actions workflow has been set up to deploy the "
90+
"built HTML documentation to a GitHub Pages website on pushes to main. \n\n"
91+
"You will need to configure the repository to deploy a GitHub pages site "
92+
"using GitHub Actions by going to\n\n"
9893
"{{cookiecutter.__repo_url}}/settings/pages\n\n"
99-
"and under 'Built and deployment' selecting 'Deploy from a branch' for "
100-
"the 'Source' drop-down and 'gh-pages' for the 'Branch' drop-down, "
101-
"leaving the branch path drop-down with its default value of '/ (root)'.",
94+
"and under 'Build and deployment' selecting 'GitHub Actions' for 'Source'.",
10295
)
10396

10497
return _EXIT_SUCCESS

tests/data/test_package_generation/.github/workflows/docs.yml

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
pull_request:
88

99
jobs:
10-
docs:
10+
build:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout source
@@ -28,19 +28,26 @@ jobs:
2828

2929
- name: Install tox
3030
run: python -m pip install tox
31+
3132
- name: Build HTML documentation with tox
3233
run: tox -e docs
3334

34-
# Automatically deploy documentation to a GitHub Pages website on pushing to main.
35-
# Requires configuring the repository to deploy GitHub pages from a branch
36-
# gh-pages (https://tinyurl.com/gh-pages-from-branch), which will be created the
37-
# first time this workflow step is run.
38-
- name: Publish documentation on GitHub pages
39-
if: success() && github.event_name != 'pull_request'
40-
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
35+
- name: Upload built docs as artifact
36+
id: deployment
37+
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3
4138
with:
42-
github_token: ${{ secrets.GITHUB_TOKEN }}
43-
publish_dir: site
44-
publish_branch: gh-pages
45-
user_name: "github-actions[bot]"
46-
user_email: "github-actions[bot]@users.noreply.github.com"
39+
path: site
40+
41+
deploy:
42+
environment:
43+
name: github-pages
44+
url: ${{ steps.deployment.outputs.page_url }}
45+
permissions:
46+
pages: write
47+
id-token: write
48+
runs-on: ubuntu-latest
49+
needs: build
50+
steps:
51+
- name: Deploy to GitHub Pages
52+
id: deployment
53+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4

tutorial.md

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -189,60 +189,44 @@ When you pushed to `main` you will have triggered runs of the [GitHub actions](h
189189
You can view the status of the workflow jobs by browsing the `Actions` button at the top bar of the repository homepage, which takes you to `https://github.com/{github_owner}/{project_slug}/actions`.
190190

191191
Depending on your account settings, you may find that the `Documentation` workflow shows as failed (❌).
192-
If you see this it's probably because of the repository permissions settings for GitHub Actions.
192+
If you see this it's probably because of the repository setting specifying the source for deploying a GitHub pages site from.
193193
We'll fix this in the next section.
194194

195195
For now, have a browse around the GitHub web interface.
196196

197197
## 📄 Configuring repository to host docs on GitHub Pages
198198

199-
Now that your repository is on GitHub, you need to do a few more steps to set up your project's documentation website.
199+
Now that your repository is on GitHub, you need to do one more step to set up your project's documentation website.
200200
The HTML documentation will be built using the `Documentation` workflow (that we're going to fix) and hosted on [GitHub Pages](https://pages.github.com/).
201201

202202
If you scroll back in your terminal to the end of the [cookiecutter] questions, you should be able to find a message like:
203203

204204
```sh
205-
The 'Documentation' GitHub Actions workflow has been set up to push the built
206-
HTML documentation to a branch gh-pages on pushes to main for deploying as a
207-
GitHub Pages website. To allow the GitHub Actions bot to push to the gh-pages
208-
branch you need to enable 'Read and write permissions' under 'Workflow
209-
permissions' at
205+
The 'Documentation' GitHub Actions workflow has been set up to deploy the built
206+
HTML documentation to a GitHub Pages website on pushes to main.
210207

211-
https://github.com/{github_owner}/{project_slug}/settings/actions
212-
213-
After the 'Documentation' workflow has successfully completed at least once
214-
you will also need to configure the repository to deploy a GitHub pages site
215-
from the content on the gh-pages branch by going to
208+
You will need to configure the repository to deploy a GitHub pages site using
209+
GitHub Actions by going to
216210

217211
https://github.com/{github_owner}/{project_slug}/settings/pages
218212

219-
and under 'Built and deployment' selecting 'Deploy from a branch' for the
220-
'Source' drop-down and 'gh-pages' for the 'Branch' drop-down, leaving the
221-
branch path drop-down with its default value of '/ (root)'.
213+
and under 'Build and deployment' selecting 'GitHub Actions' for 'Source'.
222214
```
223215

224216
As before replace `{github_owner}` and `{project_slug}` with the relevant GitHub repository owner and project slug that you chose at the start.
225217

226-
The first part of the instructions explains how to set the permissions for GitHub Actions.
227-
This lets the Actions bot push the built HTML documentation to a branch `gh-pages` when you update the default `main` branch.
218+
Following tha instructions printed to the terminal, go to `https://github.com/{github_owner}/{project_slug}/settings/pages` and set GitHub Actions as the source for GitHub Pages.
228219

229-
- Go to the Actions settings page at the URL above (or via the repository `Settings` tab, then `Actions > General`).
230-
- Set `Workflow permissions` to `Read and write permissions`.
220+
- Set `Source` to `GitHub Actions`.
231221

232-
Now that the permissions have changed, we need to re-run the `Documentation` workflow.
222+
Now that the source settings has been changed, we need to re-run the `deploy` job in the `Documentation` workflow.
233223

234224
- Go back to the Actions page. Select the Actions button (in the top bar), or visit
235225
`https://github.com/{github_owner}/{project_slug}/actions`.
236226
- Click on the failing entry for the `Documentation` workflow.
237-
- Click on the `Re-run all jobs` button (top right of the page).
238-
239-
After the `Documentation` workflow finishes, a new `gh-pages` branch will be created containing only the HTML docs.
240-
241-
Following tha instructions printed to the terminal, go to `https://github.com/{github_owner}/{project_slug}/settings/pages` and set this branch as the source for GitHub Pages.
242-
243-
- Set `Source` to `Deploy from branch` and `Branch` to `gh-pages`.
227+
- Click on the drop down ▼ symbol next to `Re-run jobs` button (top right of the page) and click `Re-run failed jobs`.
228+
- Click `Re-run jobs` button in dialogue box which appears which should list the `deploy` job as the failed job to re-run.
244229

245-
This will start another new workflow called `pages-build-deployment` (in the Actions page).
246230
When it finishes, you can view your HTML docs at:
247231

248232
```sh

{{cookiecutter.project_slug}}/.github/workflows/docs.yml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
pull_request:
88

99
jobs:
10-
docs:
10+
build:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout source
@@ -28,21 +28,27 @@ jobs:
2828

2929
- name: Install tox
3030
run: python -m pip install tox
31+
3132
- name: Build HTML documentation with tox
3233
run: tox -e docs
33-
{%- if cookiecutter.deploy_docs_to_github_pages %}
3434

35-
# Automatically deploy documentation to a GitHub Pages website on pushing to main.
36-
# Requires configuring the repository to deploy GitHub pages from a branch
37-
# gh-pages (https://tinyurl.com/gh-pages-from-branch), which will be created the
38-
# first time this workflow step is run.
39-
- name: Publish documentation on GitHub pages
40-
if: success() && github.event_name != 'pull_request'
41-
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
35+
- name: Upload built docs as artifact
36+
id: deployment
37+
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3
4238
with:
43-
github_token: ${{ '{{' }} secrets.GITHUB_TOKEN {{ '}}' }}
44-
publish_dir: site
45-
publish_branch: gh-pages
46-
user_name: "github-actions[bot]"
47-
user_email: "github-actions[bot]@users.noreply.github.com"
39+
path: site
40+
{% if cookiecutter.deploy_docs_to_github_pages %}
41+
deploy:
42+
environment:
43+
name: github-pages
44+
url: ${{ '{{' }} steps.deployment.outputs.page_url {{ '}}' }}
45+
permissions:
46+
pages: write
47+
id-token: write
48+
runs-on: ubuntu-latest
49+
needs: build
50+
steps:
51+
- name: Deploy to GitHub Pages
52+
id: deployment
53+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4
4854
{%- endif %}

0 commit comments

Comments
 (0)