# Publishing Lectures

This page provides instructions on how to update the `live` content that is
hosted on `GitHub Pages`. `GitHub Pages` is a service provided by GitHub that acts
as a host for static web sites, such as those generated by `jupyter-book`.

There are `three` publishing pathways available:

1. [](publish/automatic)
2. [](publish/gh-cli)
3. [](publish/local)

```{note}
QuantEcon is transitioning away from `gh-pages` branch-based deployment
(using [peaceiris/actions-gh-pages](https://github.com/peaceiris/actions-gh-pages))
towards the native GitHub Pages **artifact** deployment (using
[actions/deploy-pages](https://github.com/actions/deploy-pages)).

The **target architecture** is the reusable
{ref}`quantecon/actions <tools:github-actions:quantecon-actions>` library, which wraps the native
artifact deployment in a `publish-gh-pages` action;
[lecture-dp](https://github.com/QuantEcon/lecture-dp) is the reference implementation. Most active lecture
repositories have now migrated to the native artifact path — the majority via `quantecon/actions`, with a
couple of repositories deploying through inline workflow steps as a transitional form — leaving only a
couple still on the legacy `gh-pages` branch workflow. See the
{ref}`lecture deployment table <publish/gh-pages-repos>` for the current per-repo status.
```

(publish/automatic)=
## Build and Publish via GitHub Release

You can use `GitHub Actions` to build the `main` branch and publish directly to `GitHub Pages` by
tagging a `New Release` as a web based workflow.

QuantEcon repositories are also setup to provide `previews` of the `html`
when submitting changes either via `pull request` or `main` branch updates.

### Workflow

To build and publish the current `main` branch you create a GitHub release:

1. Use your browser and open the `repo` page on GitHub such as
   [lecture-python.myst](https://github.com/QuantEcon/lecture-python.myst)

2. Click on `Releases`

   ```{figure} /assets/publish/github-releases.png
   ```

3. Click on `Draft a new release`

   ```{figure} /assets/publish/github-draft-new-release.png
   ```

4. You will see an empty `tag` field and `title` field

   ```{figure} /assets/publish/github-new-release.png
   ```

5. Add a `tag` following the pattern `publish-<date>` such
   as `publish-2021march16` and update the title such as `PUBLISH: 16th March 2021`.

   ```{figure} /assets/publish/github-add-release-info.png
   ```

6. Click on `Generate release notes` to auto-populate the description field with
   a summary of commits and contributors since the last release. This is a built-in
   GitHub feature that provides a changelog of pull requests merged and new contributors.

   ```{tip}
   You can customise the auto-generated release notes by adding a
   `.github/release.yml` file to the repository. See the
   [GitHub documentation](https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes)
   for details on configuring categories and exclude labels.
   ```

7. Click on `Publish Release`

You can check the status by clicking on the `orange` circle:

```{figure} /assets/publish/github-publish-status.png
```

once the status against the `Build & Publish` task turns green this indicates
the `build` and `publish` workflow has been completed and the `live` site will
be up to date within a couple of minutes.


(publish/gh-cli)=
## Tagging a Release with the GitHub CLI

The [GitHub CLI (`gh`)](https://cli.github.com/) provides a fast way to create
a release and trigger the publish workflow directly from the terminal.

### Prerequisites

Install the GitHub CLI if you haven't already:

::::{tab-set}

:::{tab-item} macOS
```bash
brew install gh
```
:::

:::{tab-item} Linux
```bash
conda install gh --channel conda-forge
```
:::

::::

Authenticate once with:

```bash
gh auth login
```

### Workflow

1. Navigate to the lecture repository and ensure you are on `main` with the latest changes:

   ```bash
   cd ~/work/quantecon/lecture-python.myst
   git checkout main && git pull
   ```

2. Create a tag and release in a single command:

   ```bash
   gh release create publish-$(date +%Y%m%d) --title "PUBLISH: $(date +'%d %B %Y')" --generate-notes
   ```

   This will:
   - Create a new git tag (e.g. `publish-20260415`)
   - Generate release notes automatically from merged pull requests
   - Trigger the `publish.yml` GitHub Actions workflow

3. Monitor the triggered workflow run:

   ```bash
   gh run watch
   ```

````{tip}
You can also create a release for a specific commit using the `--target` flag:
```bash
gh release create publish-$(date +%Y%m%d) --target <commit-sha> --title "PUBLISH: $(date +'%d %B %Y')" --generate-notes
```
````


(publish/local)=
## Building Locally and Push to GitHub Pages

```{warning}
This workflow should only be used as a fallback if the automated publish
infrastructure (GitHub Actions) is failing. Prefer using a
{ref}`GitHub Release <publish/automatic>` or the {ref}`GitHub CLI <publish/gh-cli>` under
normal circumstances.
```

```{important}
This `ghp-import` fallback only applies to repositories that still deploy to the `gh-pages` branch
(the `peaceiris` rows in the {ref}`deployment table <publish/gh-pages-repos>`). Repositories that use the
native artifact deployment (inline or via {ref}`quantecon/actions <tools:github-actions:quantecon-actions>`)
have **no** `gh-pages` branch, so this method does not apply to them — re-run their `publish.yml` workflow
instead.
```

These instructions assume you are at the base level of a QuantEcon lecture repository
such as `lecture-python.myst`

```{note}
QuantEcon uses the folder `lectures` to contain all `jupyter-book` documents
```

(publish/gh-pages-repos)=
### Lecture Deployment Status

QuantEcon lecture repositories currently use one of three deployment paths. In all cases the publish
workflow is triggered by pushing a tag matching `publish*` (see {ref}`publish/automatic` and
{ref}`publish/gh-cli`).

- **{ref}`quantecon/actions <tools:github-actions:quantecon-actions>` (target)** — native artifact
  deployment composed from the reusable `quantecon/actions` library. No `gh-pages` branch. This is the
  target architecture and now covers most active repositories.
- **native artifacts (inline)** — deploys via
  [actions/deploy-pages](https://github.com/actions/deploy-pages) with build steps written inline in the
  repository's `publish.yml`. No `gh-pages` branch. This was a transitional form on the way to the reusable
  `quantecon/actions` library; the remaining inline repositories are being folded into the target path.
- **`peaceiris` (legacy)** — deploys to the `gh-pages` branch via
  [peaceiris/actions-gh-pages](https://github.com/peaceiris/actions-gh-pages). Only a couple of repositories
  remain on this path.

| Repository | Website | Deployment |
|---|---|---|
| [lecture-python.myst](https://github.com/QuantEcon/lecture-python.myst) | [python.quantecon.org](https://python.quantecon.org/) | `quantecon/actions` (target) |
| [lecture-python-advanced.myst](https://github.com/QuantEcon/lecture-python-advanced.myst) | [python-advanced.quantecon.org](https://python-advanced.quantecon.org/) | `quantecon/actions` (target) |
| [lecture-jax](https://github.com/QuantEcon/lecture-jax) | [jax.quantecon.org](https://jax.quantecon.org/) | `quantecon/actions` (target) |
| [lecture-dp](https://github.com/QuantEcon/lecture-dp) | [dp.quantecon.org](https://dp.quantecon.org/) | `quantecon/actions` (target) |
| [continuous_time_mcs](https://github.com/QuantEcon/continuous_time_mcs) | [continuous-time-mcs.quantecon.org](https://continuous-time-mcs.quantecon.org/) | `quantecon/actions` (target) |
| [lecture-python-intro](https://github.com/QuantEcon/lecture-python-intro) | [intro.quantecon.org](https://intro.quantecon.org/) | `quantecon/actions` (target) |
| [lecture-python-programming](https://github.com/QuantEcon/lecture-python-programming) | [python-programming.quantecon.org](https://python-programming.quantecon.org/) | `quantecon/actions` (target) |
| [lecture-datascience.myst](https://github.com/QuantEcon/lecture-datascience.myst) | [datascience.quantecon.org](https://datascience.quantecon.org/) | `peaceiris` (legacy) |
| [lecture-julia.myst](https://github.com/QuantEcon/lecture-julia.myst) | [julia.quantecon.org](https://julia.quantecon.org/) | `peaceiris` (legacy) |

```{note}
This table is maintained by hand and lists the most active repositories. To confirm the deployment path
for any repository, check the `Deploy` step in its `.github/workflows/publish.yml`.
```

### Workflow

1. Activate the [quantecon environment](https://quantecon.github.io/QuantEcon.manual/intro.html#the-quantecon-environment) using `conda`:
   ```bash
   conda activate quantecon
   ```
   ```{warning}
   Your `software environment` may change how the site is built. It is important to
   use the [quantecon environment](https://quantecon.github.io/QuantEcon.manual/intro.html#the-quantecon-environment) to ensure consistent `html` output.
   ```
2. Build `HTML` files locally using [jb build command](https://jupyterbook.org/start/build.html)
   ```bash
   jb build lectures
   ```
   or if you are editing files in the `lectures` folder you can use
   ```bash
   jb build ./
   ```
3. Build `IPYNB` download notebooks locally using custom builder `sphinx-tojupyter`
   ```bash
   jb build lectures --builder=custom --custom-builder=jupyter
   ```
   and copy asset to `_notebooks` folder in `_build/html`
   ```bash
   mkdir lectures/_build/html/_notebooks
   cp lectures/_build/jupyter/*.ipynb lectures/_build/html/_notebooks
   ```
4. Build `PDF` book style pdf locally using the `pdflatex` builder
   ```bash
   jb build lectures --builder=pdflatex
   ```
   and copy asset to `_pdf` folder in `_build/html`
   ```bash
   mkdir lectures/_build/html/_pdf
   cp lectures/_build/latex/*.pdf lectures/_build/html/_pdf
   ```
5. You can preview the `HTML` by following the instructions displayed by `jupyter book` and open
   the `index.html` file located in your `_build/html`.

   ::::{tab-set}

   :::{tab-item} Linux
   ```bash
   chrome lectures/_build/html/index.html
   ```
   or if you are editing in the `lectures` folder
   ```bash
   chrome _build/html/index.html
   ```
   :::

   :::{tab-item} macOS
   ```bash
   open lectures/_build/html/index.html
   ```
      or if you are editing in the `lectures` folder
   ```bash
   open _build/html/index.html
   ```   
   :::

   ::::

6. Use `ghp-import` to sync the `HTML` files to the `gh-pages` branch using
   
   ```bash
   ghp-import -n -p -f -c <domain-name> -m <MESG> lectures/_build/html
   ```
   (or `_build/html` if you are running it from inside the `lectures` folder)

   where:
   
   1. `-n` instructs `github` **not** to build your project using Jekyll,
   2. `-p` pushes the branch to `origin/gh-pages`, and
   3. `-f` forces the push to the repository to ensure the transfer is made.
   4. `-c` adds a `CNAME` file with the custom domain settings
   5. `-m` sets the commit message. Use a descriptive message such as `publish-<date>`

   [ghp-import](https://github.com/c-w/ghp-import) is a tool that will publish a directory of files
   such as the `_build/html/` directory into the `gh-pages` branch of a repository and push them to
   `github` for publication. This tool will `replace`
   the `gh-pages` branch with the contents of what you point at it so it should be used carefully.

   :::{note}
   If `ghp-import` is not installed you can do so using `pip`:
   ```bash
   pip install ghp-import
   ```
   it is **not** included in the quantecon environment, so this install step is required.
   :::

Additional details are provided in the [jupyter-book documentation](https://jupyterbook.org/publish/gh-pages.html#push-your-book-to-a-branch-hosted-by-github-pages)
