Skip to content

Commit b465716

Browse files
refactor(ci): use reusable action
Signed-off-by: Victor Adossi <[email protected]>
1 parent 08dd692 commit b465716

File tree

5 files changed

+162
-91
lines changed

5 files changed

+162
-91
lines changed

.github/actions/mdbook/action.yml

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
name: mdbook
2+
3+
description: |
4+
Run the mdbook build, optionally outputting a pages artifact
5+
6+
inputs:
7+
publish-pages-artifact:
8+
type: boolean
9+
required: false
10+
default: false
11+
description: |
12+
Whether to publish a pages artifact
13+
14+
publish-domain:
15+
type: string
16+
required: false
17+
default: "component-model.bytecodealliance.org"
18+
description: |
19+
Path to which to store the artifac
20+
21+
mdbook-version:
22+
type: string
23+
required: false
24+
default: "0.4.21"
25+
description: |
26+
Version of mdbook to use (ex. '0.4.21')
27+
28+
mdbook-alerts-version:
29+
type: string
30+
required: false
31+
default: "0.6.7"
32+
description: |
33+
Version of mdbook-alerts to use (ex. '0.6.7')
34+
35+
mdbook-linkcheck-version:
36+
type: string
37+
required: false
38+
default: "0.7.7"
39+
description: |
40+
Version of mdbook-linkcheck to use (ex. '0.7.7')
41+
42+
env:
43+
ARTIFACT_PATH: ./component-model/book/html
44+
45+
runs:
46+
using: composite
47+
steps:
48+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
49+
50+
- uses: extractions/setup-just@v2
51+
52+
- uses: taiki-e/cache-cargo-install-action@4d586f211d9b0bca9e7b59e57e2a0febf36c0929 # v2.1.1
53+
with:
54+
tool: "just"
55+
56+
- uses: taiki-e/cache-cargo-install-action@4d586f211d9b0bca9e7b59e57e2a0febf36c0929 # v2.1.1
57+
with:
58+
tool: "mdbook@${{ inputs.mdbook-version }}"
59+
60+
- uses: taiki-e/cache-cargo-install-action@4d586f211d9b0bca9e7b59e57e2a0febf36c0929 # v2.1.1
61+
with:
62+
tool: "mdbook-alerts@${{ inputs.mdbook-alerts-version }}"
63+
64+
- uses: taiki-e/cache-cargo-install-action@4d586f211d9b0bca9e7b59e57e2a0febf36c0929 # v2.1.1
65+
with:
66+
tool: "mdbook-linkcheck@${{ inputs.mdbook-linkcheck-version }}"
67+
68+
- name: Build with mdBook
69+
shell: bash
70+
run: mdbook build component-model
71+
72+
- name: Setup Python
73+
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
74+
with:
75+
python-version: '3.13'
76+
77+
- name: Ensure build outputs are present
78+
shell: bash
79+
run: |
80+
if [[ ! -f "${{ env.ARTIFACT_PATH }}/index.html" ]]; then
81+
echo "[error] index.html @ [${{ env.ARTIFACT_PATH }}] is missing. Build or path misconfigured";
82+
exit 1;
83+
fi
84+
85+
- name: Generate sitemap
86+
shell: bash
87+
run: |
88+
cd ${{ env.ARTIFACT_PATH }}
89+
python3 ../../../scripts/generate_sitemap.py \
90+
--domain "${{ inputs.publish-domain }}" \
91+
--higher-priority "design" \
92+
--output-path sitemap.xml
93+
cd ../../../
94+
95+
- name: Ensure publish-domain was provided
96+
if: ${{ inputs.publish-pages-artifact }}
97+
shell: bash
98+
run: |
99+
if [[ -z "${{ inputs.publish-domain }}" ]]; then
100+
echo "[error] publish-domain input is required when publishing";
101+
exit 1;
102+
fi
103+
104+
- name: Setup Pages
105+
if: ${{ inputs.publish-pages-artifact }}
106+
id: pages
107+
uses: actions/configure-pages@983d7736d9b0ae728b81ab479565c72886d7745b # v5.0.0
108+
109+
- name: Upload artifact
110+
if: ${{ inputs.publish-pages-artifact }}
111+
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1
112+
with:
113+
path: ${{ env.ARTIFACT_PATH }}

.github/workflows/deploy.yml

+6-46
Original file line numberDiff line numberDiff line change
@@ -26,60 +26,20 @@ jobs:
2626
build:
2727
runs-on: ubuntu-latest
2828
steps:
29-
- uses: actions/checkout@v3
29+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3030

31-
- uses: taiki-e/cache-cargo-install-action@v2
31+
- uses: ./.github/actions/mdbook
3232
with:
33-
tool: mdbook@${{ env.MDBOOK_VERSION }}
34-
35-
- uses: taiki-e/cache-cargo-install-action@v2
36-
with:
37-
tool: mdbook-alerts@${{ env.MDBOOK_ALERTS_VERSION }}
38-
39-
- uses: taiki-e/cache-cargo-install-action@v2
40-
with:
41-
tool: mdbook-linkcheck@${{ env.MDBOOK_LINKCHECK_VERSION }}
42-
43-
- name: Build with mdBook
44-
run: mdbook build component-model
45-
46-
- name: Setup Python
47-
uses: actions/setup-python@v5
48-
with:
49-
python-version: '3.13'
50-
51-
- name: Ensure build outputs are present
52-
shell: bash
53-
run: |
54-
if [[ ! -f "${{ env.ARTIFACT_PATH }}/index.html" ]]; then
55-
echo "[error] index.html @ [${{ env.ARTIFACT_PATH }}] is missing. Build or path misconfigured";
56-
exit 1;
57-
fi
58-
59-
- name: Generate sitemap
60-
shell: bash
61-
run: |
62-
cd ${{ env.ARTIFACT_PATH }}
63-
python3 ../../../scripts/generate_sitemap.py --domain "component-model.bytecodealliance.org" --higher-priority "design" --output-path sitemap.xml
64-
cd ../../../
65-
66-
- name: Setup Pages
67-
id: pages
68-
uses: actions/configure-pages@v3
69-
70-
- name: Upload artifact
71-
uses: actions/upload-pages-artifact@v3
72-
with:
73-
path: ${{ env.ARTIFACT_PATH }}
33+
publish-pages: true
7434

7535
deploy:
7636
if: ${{ github.repository_owner == 'bytecodealliance' }}
37+
needs:
38+
- build
7739
environment:
7840
name: github-pages
7941
url: ${{ steps.deployment.outputs.page_url }}
80-
runs-on: ubuntu-latest
81-
needs: build
8242
steps:
8343
- name: Deploy to GitHub Pages
8444
id: deployment
85-
uses: actions/deploy-pages@v4
45+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5

.github/workflows/mdbook.yml

+3-38
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,13 @@ name: mdbook
33
on:
44
pull_request:
55

6-
env:
7-
MDBOOK_VERSION: 0.4.21
8-
MDBOOK_ALERTS_VERSION: 0.6.7
9-
MDBOOK_LINKCHECK_VERSION: 0.7.7
10-
116
jobs:
127
build:
138
runs-on: ubuntu-latest
149
steps:
15-
- uses: actions/checkout@v3
16-
17-
- uses: taiki-e/cache-cargo-install-action@v2
18-
with:
19-
tool: mdbook@${{ env.MDBOOK_VERSION }}
20-
21-
- uses: taiki-e/cache-cargo-install-action@v2
22-
with:
23-
tool: mdbook-alerts@${{ env.MDBOOK_ALERTS_VERSION }}
24-
25-
- uses: taiki-e/cache-cargo-install-action@v2
26-
with:
27-
tool: mdbook-linkcheck@${{ env.MDBOOK_LINKCHECK_VERSION }}
28-
29-
- name: Build with mdBook
30-
run: mdbook build component-model
31-
32-
- name: Ensure build outputs are present
33-
shell: bash
34-
run: |
35-
if [[ ! -f "${{ env.ARTIFACT_PATH }}/index.html" ]]; then
36-
echo "[error] index.html @ [${{ env.ARTIFACT_PATH }}] is missing. Build or path misconfigured";
37-
exit 1;
38-
fi
10+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3911

40-
- name: Setup Python
41-
uses: actions/setup-python@v5
12+
- uses: ./.github/actions/mdbook
4213
with:
43-
python-version: '3.13'
14+
publish-pages: true
4415

45-
- name: Generate sitemap
46-
shell: bash
47-
run: |
48-
cd ${{ env.ARTIFACT_PATH }}
49-
python3 ../../../scripts/generate_sitemap.py --domain "component-model.bytecodealliance.org" --higher-priority "design" --output-path sitemap.xml
50-
cd ../../../

justfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
just := env_var_or_default("JUST", "just")
2+
just_dir := env_var_or_default("JUST_DIR", justfile_directory())
3+
4+
python := env_var_or_default("PYTHON", "python3")
5+
cargo := env_var_or_default("CARGO", "cargo")
6+
7+
publish_domain := env_var_or_default("PUBLISH_DOMAIN", "component-model.bytecodealliance.org")
8+
9+
scripts_dir := env_var_or_default("SCRIPTS_DIR", "scripts")
10+
11+
sitemap_output_path := absolute_path("./component-model/book/html/sitemap.xml")
12+
13+
@_default:
14+
{{just}} --list
15+
16+
# Build the sitemap
17+
@build-sitemap:
18+
{{python}} {{scripts_dir}}/generate_sitemap.py --domain "{{publish_domain}}" --higher-priority "design" --output-path {{sitemap_output_path}}
19+

scripts/generate_sitemap.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
from urllib.parse import urljoin
33
from datetime import datetime
4+
from pathlib import Path
5+
46
import argparse
57

6-
def parse_summary():
8+
def parse_summary(summary_file_path):
79
"""Parse URLs from the SUMMARY.md file."""
8-
with open("../../src/SUMMARY.md", "r") as file:
10+
with open(summary_file_path, "r") as file:
911
for line in file:
1012
if "](" in line:
1113
url = line.split("](")[1].split(")")[0]
@@ -23,10 +25,10 @@ def determine_priority(url_path, higher_priority_section):
2325
else:
2426
return "0.5" # All other pages
2527

26-
def generate_sitemap(domain, output_path, higher_priority_section):
28+
def generate_sitemap(domain, output_path, summary_file_path, higher_priority_section):
2729
"""Generate a sitemap XML file from SUMMARY.md structure."""
2830
domain = "https://" + domain
29-
urls = parse_summary() # Add base URL to the list of URLs
31+
urls = parse_summary(summary_file_path) # Add base URL to the list of URLs
3032
urls = [""] + list(urls)
3133

3234
sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n'
@@ -48,11 +50,23 @@ def generate_sitemap(domain, output_path, higher_priority_section):
4850
with open(output_path, "w") as file:
4951
file.write(sitemap)
5052

53+
DEFAULT_SUMMARY_MD_PATH = (Path(__file__).parent / "../component-model/src/SUMMARY.md").resolve()
54+
DEFAULT_SITEMAP_XML_PATH = (Path(__file__).parent / "../component-model/book/html/sitemap.sml").resolve()
55+
5156
if __name__ == "__main__":
5257
parser = argparse.ArgumentParser(description="Generate a sitemap for mdBook")
53-
parser.add_argument("-d", "--domain", required=True, help="Domain for the mdBook site (e.g., component-model.bytecodealliance.org)")
54-
parser.add_argument("-o", "--output-path", default="sitemap.xml", help="Output path for the sitemap file")
58+
parser.add_argument("-d", "--domain", required=True, help="Domain for the mdBook site (e.g., 'component-model.bytecodealliance.org')")
59+
parser.add_argument("-o", "--output-path", default=DEFAULT_SITEMAP_XML_PATH, help="Output path for the sitemap file")
60+
parser.add_argument("-s", "--summary-md-path", default=DEFAULT_SUMMARY_MD_PATH, help="Path to SUMMARY.md")
5561
parser.add_argument("-p", "--higher-priority", help="Subsection path (e.g., 'design') to assign a higher priority of 0.8")
5662
args = parser.parse_args()
5763

58-
generate_sitemap(args.domain, args.output_path, args.higher_priority)
64+
summary_file_path = Path(args.summary_md_path).resolve()
65+
if not summary_file_path.exists():
66+
raise FileNotFoundError(f"failed to find summary file [{summary_file_path}]")
67+
68+
output_path = Path(args.output_path).resolve()
69+
if not output_path.parent.exists():
70+
raise FileNotFoundError(f"failed to find output dir [{output_path.parent}]")
71+
72+
generate_sitemap(args.domain, output_path, summary_file_path, args.higher_priority)

0 commit comments

Comments
 (0)