quarto publish gh-pages GitHub Actions as CI/CD
Lab 5 showed how CI/CD pipelines react to Git events by building, testing, and deploying code. This chapter puts that into practice with GitHub Actions (or just Actions). Actions can deploy R or Python code (e.g., models, APIs, and apps) to a published website. View the original lab file, publish-r-py.yml.
To help explain/demonstrate GitHub Actions, I’ll cover a few examples I use often.
Publishing Quarto/GitHub Pages
This book is published using GitHub Pages, which is a free hosting service provided by GitHub. Quarto provides an easy Terminal command for publishing using GitHub Pages:
The lab for this section will accomplish the same thing, but does so using a GitHub Action workflow file. However, quarto publish gh-pages (the command I used before this book moved to CI) never creates workflow files because it renders and pushes from your machine. This local setup means Quarto renders the files locally, then pushes the rendered HTML directly to the gh-pages branch. The commit log for this branch confirms it:
git log gh-pages --oneline -5
# 76175e88 Built site for gh-pages
# 559df417 Built site for gh-pages
# ...The Built site for gh-pages messages are Quarto’s signature commit message when it pushes to the branch itself. I made these commits from my machine, so no workflow file is involved. Quarto writes the same message when it publishes from CI, but those commits list Quarto GHA Workflow Runner as the author.
To convert this to CI publishing, we need to create and commit one workflow file, .github/workflows/book.yml, so GitHub Actions can render and deploy on every push to main. The other workflow in that folder, publish.yml, belongs to Lab 7: it publishes the models to S3 and is covered in GitHub Actions and S3.
Workflow file
Create .github/workflows/book.yml with the following:
# .github/workflows/book.yml
name: Publish Book
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: write
jobs:
publish-book:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Quarto
uses: quarto-dev/quarto-actions/setup@v2
- name: Render and publish to gh-pages
uses: quarto-dev/quarto-actions/publish@v2
with:
target: gh-pages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}The workflow_dispatch trigger lets you kick off a manual build from the GitHub Actions UI without needing to push a commit.
How the workflow publishes the book
book.yml listens for a push to main, spins up a runner, installs Quarto, and then renders and publishes the book with quarto-actions/publish@v2. The target: gh-pages line tells Quarto where to deploy, so the workflow needs no other configuration file.
The GITHUB_TOKEN is provided automatically by GitHub for every workflow run, so no manual secret setup is needed. permissions: contents: write lets that token push the rendered site to the gh-pages branch. Once the file is committed and pushed, every subsequent push to main will trigger a fresh render and deploy.
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%
flowchart TD
Push(["Developer<br>pushes to <code>main</code>"]) -->|"triggers"| GHA["GitHub Actions<br>Runner<br>(ubuntu-latest)"]
GHA --> Step1("<strong>Step 1</strong><br>Checkout Repository<br><code>actions/checkout@v4</code>")
Step1 --> Step2("<strong>Step 2</strong><br>Set up Quarto<br><code>quarto-actions/setup@v2</code>")
Step2 --> Step3("<strong>Step 3</strong><br>Render & Publish<br><code>quarto-actions/publish@v2</code>")
Step3 -->|"authenticates with"| Token("<code>GITHUB_TOKEN</code><br>(auto-provided<br>by GitHub)")
Token -->|"writes rendered output"| GHPages("<code>gh-pages</code><br>branch")
GHPages -->|"served as"| Site["Live GitHub<br>Pages Site"]
style Push fill:#5B8C5A,stroke:#000000,stroke-width:1px,color:#ffffff
style GHA fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style Step1 fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style Step2 fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style Step3 fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style Token fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
style GHPages fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff
style Site fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff
The gitGraph below shows what each push leaves behind. Every commit pushed to main triggers book.yml, and the runner adds a Built site for gh-pages commit with the rendered HTML to gh-pages. The two branches never merge: gh-pages holds only the rendered site.
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace', 'fontSize': '16px', 'commitLabelFontSize': '14px'}}}%%
gitGraph
commit id: "add book.yml"
branch gh-pages
commit id: "Built site 1"
checkout main
commit id: "edit chapter"
checkout gh-pages
commit id: "Built site 2"
checkout main
commit id: "fix typo"
checkout gh-pages
commit id: "Built site 3"
Running Tests (Python)
You can also use GitHub Actions to run tests on your code. Suppose we had a Shiny for Python package. We could set up a workflow that runs unit tests when the code is pushed to the test branch. The code moves to the prod branch only if the tests pass. We’ll use the application from Python App Logging.1
Workflow file
The workflow below lives in .github/workflows/python-tests.yml and runs on every push to test and every pull request targeting prod:
# stored in _labs/lab05/cicd/Python/penguin_predictor/.github/workflows/python-tests.yml
name: Python Tests
on:
push:
branches: [test]
pull_request:
branches: [prod]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install package and test dependencies
run: pip install -e ".[test]"
- name: Run tests
run: pytest tests/ -vHow the Python test workflow runs
- The workflow above triggers on pushes to
test(fromdev) and on pull requests targetingprod pip install -e ".[test]"installs thepenguin_predictorpackage and its test dependencies (pytest) frompyproject.toml- A failing workflow doesn’t block a merge on its own. To require passing tests before merging to
prod, enable branch protection rules in GitHub (Settings > Branches > Add rule forprod) and require this workflow as a status check
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%
flowchart TD
DevBr(["<code>dev</code> branch"]) -->|"push to test"| TestBr("<code>test</code> branch")
TestBr -->|"triggers"| GHA("GitHub Actions<br>Run <code>pytest</code> suite")
GHA --> Pass{"Tests pass?"}
Pass -->|"yes"| PR("Pull Request<br><code>test</code> to <code>prod</code>")
Pass -->|"no"| Block("Promotion<br>Blocked")
PR -->|"approved & merged"| ProdBr("<code>prod</code> branch")
style DevBr fill:#5B8C5A,stroke:#000000,stroke-width:1px,color:#ffffff
style TestBr fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style GHA fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style PR fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
style Block fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
style ProdBr fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff
The gitGraph below shows the same promotion as commits on each branch. The first push to test fails pytest, a fix on dev passes, and the pull request merges test into prod:
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace', 'fontSize': '16px', 'commitLabelFontSize': '14px'}, 'gitGraph': {'mainBranchName': 'prod'}}}%%
gitGraph
commit id: "init package"
branch test
commit id: "add python-tests.yml"
branch dev
commit id: "add tests"
checkout test
merge dev id: "pytest fails" type: REVERSE
checkout dev
commit id: "fix encode_inputs()"
checkout test
merge dev id: "pytest passes" type: HIGHLIGHT
checkout prod
merge test id: "PR merged"
Running Tests (R)
The R equivalent lives in _labs/lab05/cicd/R/penguinpredictor/.2 It follows the same pattern as the Python package. Pure functions are extracted from the Shiny app into a proper R package, so testthat can test them independently of the UI.
The package structure mirrors a standard R package:
penguinpredictor/
├── DESCRIPTION
├── NAMESPACE
├── R/
│ ├── data.R
│ └── logging_setup.R
├── tests/
│ └── testthat/
│ └── test-data.R
└── app.R- 1
-
validate_inputs(),encode_inputs(),parse_prediction(): the same three functions extracted from the app - 2
-
21
testthattests covering valid inputs, boundary conditions, encoding, and response parsing
Workflow file
The R workflow lives in .github/workflows/r-tests.yml and runs on the same triggers (pushes to test and pull requests targeting prod):
# stored in _labs/lab05/cicd/R/penguinpredictor/.github/workflows/r-tests.yml
name: R Tests
on:
push:
branches: [test]
pull_request:
branches: [prod]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up R
uses: r-lib/actions/setup-r@v2
with:
r-version: "4.4"
- name: Install system dependencies
run: sudo apt-get install -y libcurl4-openssl-dev libssl-dev
- name: Install package and test dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::testthat
any::pkgload
local::.
- name: Run tests
run: Rscript -e "testthat::test_dir('tests/testthat', package = 'penguinpredictor', load_package = 'installed')"How the R test workflow runs
- The workflow triggers on pushes to
testand on pull requests targetingprod, identical to the Python version r-lib/actions/setup-r-dependencies@v2readsDESCRIPTIONand installs declared packages (the R equivalent ofpip install -e ".[test]")- The system dependency step (
libcurl4-openssl-dev,libssl-dev) is needed becausehttr2imports thecurlandopensslpackages, which link against these libraries on Linux runners - Tests run via
testthat::test_dir()withload_package = 'installed', which loads the installed package withlibrary()rather than sourcing the files directly.local::.in the dependencies step installs the package on the runner first
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%
flowchart TD
DevBr(["<code>dev</code> branch"]) -->|"push to test"| TestBr("<code>test</code> branch")
TestBr -->|"triggers"| GHA("GitHub Actions<br>Run <code>testthat</code> suite")
GHA --> Pass{"Tests pass?"}
Pass -->|"yes"| PR("Pull Request<br><code>test</code> to <code>prod</code>")
Pass -->|"no"| Block("Promotion<br>Blocked")
PR -->|"approved & merged"| ProdBr("<code>prod</code> branch")
style DevBr fill:#5B8C5A,stroke:#000000,stroke-width:1px,color:#ffffff
style TestBr fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style GHA fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
style PR fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
style Block fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
style ProdBr fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff
The gitGraph below shows the same promotion as commits on each branch. The first push to test fails testthat, a fix on dev passes, and the pull request merges test into prod:
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace', 'fontSize': '16px', 'commitLabelFontSize': '14px'}, 'gitGraph': {'mainBranchName': 'prod'}}}%%
gitGraph
commit id: "init package"
branch test
commit id: "add r-tests.yml"
branch dev
commit id: "add tests"
checkout test
merge dev id: "testthat fails" type: REVERSE
checkout dev
commit id: "fix validate_inputs()"
checkout test
merge dev id: "testthat passes" type: HIGHLIGHT
checkout prod
merge test id: "PR merged"
A note on workflow file placement
Both workflow files are reference copies and won’t execute when this book is pushed to GitHub. The Python copy is in .github/workflows/python-tests.yml, and the R copy is in .github/workflows/r-tests.yml.
GitHub Actions only picks up workflow files located in the repository root’s .github/workflows/ directory, so anything nested deeper is treated as an ordinary text file.
Workflow files in a standalone repository
If these app-packages were their own projects, the same workflow file would go at the root of the repository:
penguin_predictor/
├── app.py
├── .github/
│ └── workflows/
│ └── python-tests.yml
├── .gitignore
├── penguin_predictor/
│ ├── __init__.py
│ ├── data.py
│ └── logging_setup.py
├── pyproject.toml
├── README.md
├── requirements.txt
└── tests/
├── __init__.py
└── test_data.py- 1
- GitHub Actions reads this
The R package follows the same layout:
penguinpredictor/
├── app.R
├── DESCRIPTION
├── .github/
│ └── workflows/
│ └── r-tests.yml
├── .gitignore
├── NAMESPACE
├── R
│ ├── data.R
│ └── logging_setup.R
├── .Rbuildignore
├── README.md
├── renv.lock
└── tests
└── testthat
└── test-data.R- 1
- GitHub Actions reads this
With that structure in place, every push to test and every pull request targeting prod will trigger the workflow automatically.
Recap
quarto publish gh-pagesrenders locally and pushes the HTML straight to thegh-pagesbranch, so no workflow file is involved. Publishing through CI takes one workflow file:.github/workflows/book.ymlrenders and deploys the book on every push tomain.The Python and R test workflows follow the same pattern: both run on pushes to
testand on pull requests targetingprod. Python installs the package withpip install -e ".[test]"and runspytest, while R installs dependencies withr-lib/actions/setup-r-dependencies@v2and runstestthat. Requiring the workflow as a status check in a branch protection rule forprodblocks promotion when tests fail.GitHub Actions only reads workflow files in the repository root’s
.github/workflows/directory. The test workflows in this book are references; in a standalone app-package repository, they would run automatically.
Next, we’ll look at containers in Lab 6: Demystifying Docker.
The original app is in
_labs/lab04/Python/app/. The refactored package used here is in_labs/lab05/cicd/Python/penguin_predictor/.↩︎R package names can only contain letters, numbers, and periods, so the R version is
penguinpredictor(no underscore).↩︎