|
| 1 | +--- |
| 2 | +title: Hosting |
| 3 | +--- |
| 4 | + |
| 5 | +::: {.pale-blue} |
| 6 | + |
| 7 | +**On this page we will:** |
| 8 | + |
| 9 | +* Enable the GitHub action to automatically render and publish your site. |
| 10 | +* Trigger a build and visit your live site. |
| 11 | + |
| 12 | +::: |
| 13 | + |
| 14 | +You can host your Quarto site for free using **GitHub Pages**. GitHub Pages takes the HTML files that Quarto creates and puts them on the web so that anyone with the link can see your site. |
| 15 | + |
| 16 | +## Go to Actions tab |
| 17 | + |
| 18 | +Open your fork of the `quarto-template` repository on GitHub. If you need to find it, go to GitHub → Repositories → `quarto-template`. |
| 19 | + |
| 20 | +Click on the **Actions** tab. |
| 21 | + |
| 22 | +{fig-alt="Screenshot of cursor hovering on Actions tab."} |
| 23 | + |
| 24 | +## Enable workflows |
| 25 | + |
| 26 | +GitHub will show a message that workflows aren't being run on this forked repository. |
| 27 | + |
| 28 | +This is just a safety check. We know what the workflow does (it builds and publishes the Quarto site), so it is safe to enable it for this workshop. |
| 29 | + |
| 30 | +Click the green button to enable Actions for this repository. |
| 31 | + |
| 32 | +{fig-alt="Screenshot of 'Workflows aren't being run' message."} |
| 33 | + |
| 34 | +After you click, you should see that Actions are now enabled: |
| 35 | + |
| 36 | +{fig-alt="Screenshot of screen after clicking to enable actions."} |
| 37 | + |
| 38 | +## Set the GitHub Pages branch |
| 39 | + |
| 40 | +Next, tell GitHub Pages which branch to use for the website. |
| 41 | + |
| 42 | +In your repository on GitHub, click on the **Settings** tab. In the left sidebar, click **Pages**. |
| 43 | + |
| 44 | +Under **Branch**, change the source from `None` to `gh-pages`. Click **Save**. |
| 45 | + |
| 46 | +> **Why `gh-pages`?** |
| 47 | +> |
| 48 | +> A Git repository can have more than one **branch**. You can think of branches as different versions or lines of development in the same project. |
| 49 | +> For this template, the main branch (`main`) holds your source files (the `.qmd` files, `_quarto.yml`, etc.), and the workflow puts the **built website** into a special branch called `gh-pages`. |
| 50 | +> GitHub Pages is then told, "Use the files from the `gh-pages` branch as the live website."" |
| 51 | +
|
| 52 | +{fig-alt="Screenshot of GitHub Pages settings page."} |
| 53 | + |
| 54 | +## Make a commit to trigger the build |
| 55 | + |
| 56 | +The workflow runs **each time you push a commit** to GitHub on the `main` branch. That is how your site will stay up to date. |
| 57 | + |
| 58 | +Go back to your Codespace and make a small change to your site (e.g., edit some text on the homepage), then **commit and push** you change. |
| 59 | + |
| 60 | +Then, return to your GitHub repository **Actions** tab, and you should see a new workflow run starting. |
| 61 | + |
| 62 | +{fig-alt="Screenshot showing a workflow run in the Actions list."} |
| 63 | + |
| 64 | +Then click on the `build-deploy` job to watch the steps. Near the end of the "Render and publish to GitHub pages" step, you should see a line with the URL of your site. Click that link (or copy and paste it into your browser). |
| 65 | + |
| 66 | +{fig-alt="Screenshot of a running workflow with build-deploy job."} |
| 67 | + |
| 68 | +Then click on the `build-deploy` job to watch the steps. Near the end, you should see a line with the URL of your site. Click that link (or copy and paste it into your browser). |
| 69 | + |
| 70 | +{fig-alt="Screenshot showing the GitHub Pages URL in the workflow output."} |
| 71 | + |
| 72 | +Sometimes, you may briefly see a 404 error while GitHub finishes publishing. If that happens, wait a few moments and refresh the page. |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +## Update the repository "About" url |
| 77 | + |
| 78 | +To make the site easy to find later, add the GitHub Pages URL to the repository's "About" section. |
| 79 | + |
| 80 | +On the main page of your repository on GitHub, look at the **About** box on the right, and click the small **settings** (gear) icon next to it. |
| 81 | + |
| 82 | +{fig-alt="Screenshot of the About pane with the settings icon."} |
| 83 | + |
| 84 | +Paste your GitHub Pages URL into the **Website** field then click **save changes**. |
| 85 | + |
| 86 | +{fig-alt="Screenshot showing the Website field."} |
| 87 | + |
| 88 | +<br> |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## How did that work? |
| 93 | + |
| 94 | +The publishing is handled by a **GitHub Actions workflow**. GitHub Actions is a service that can run steps for you in the cloud when certain events happen (for example, when you push a commit). |
| 95 | + |
| 96 | +The instructions live in a workflow file in `.github/workflows/`. For this template, it looks like: |
| 97 | + |
| 98 | + |
| 99 | +```{.yaml} |
| 100 | +name: Render quarto site and publish on GitHub pages # <1> |
| 101 | +run-name: Render quarto site and publish on GitHub pages # <1> |
| 102 | +
|
| 103 | +on: # <2> |
| 104 | + push: # <2> |
| 105 | + branches: main # <2> |
| 106 | +
|
| 107 | +jobs: # <3> |
| 108 | + build-deploy: # <3> |
| 109 | + runs-on: ubuntu-latest # <4> |
| 110 | + permissions: # <5> |
| 111 | + contents: write # <5> |
| 112 | + steps: # <6> |
| 113 | + - name: Check out repository # <7> |
| 114 | + uses: actions/checkout@v4 # <7> |
| 115 | +
|
| 116 | + - name: Set up Quarto # <8> |
| 117 | + uses: quarto-dev/quarto-actions/setup@v2 # <8> |
| 118 | +
|
| 119 | + - name: Render and publish to GitHub pages # <9> |
| 120 | + uses: quarto-dev/quarto-actions/publish@v2 # <9> |
| 121 | + with: # <9> |
| 122 | + target: gh-pages # <9> |
| 123 | + env: # <9> |
| 124 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # <9> |
| 125 | +``` |
| 126 | + |
| 127 | +1. **name/run-name**: Labels for the workflow and its runs, so you can recognise them in the Actions tab. |
| 128 | +2. **on: push: branches: main:** Tells GitHub to run this workflow whenever you push a commit to the `main` branch. |
| 129 | +3. **jobs / build-deploy:** Defines a job called `build-deploy` that does the work. |
| 130 | +4. **runs-on: ubuntu-latest:** Uses a fresh Linux (Ubuntu) virtual machine in the cloud to run the job. |
| 131 | +5. **permissions: contents: write:** Allows the job to write back to the repository (needed to update the `gh-pages` branch). |
| 132 | +6. **steps:** The list of actions to perform. |
| 133 | +7. **Check out repository:** Downloads the code from your repository into the runner. |
| 134 | +8. **Set up Quarto:** Installs Quarto so it can render the site. |
| 135 | +9. **Render and publish to GitHub Pages:** Renders the Quarto project and pushes the built site to the `gh-pages` branch using the `quarto-actions/publish` action. `GITHUB_TOKEN` is a special token that GitHub provides so the workflow can authenticate and push to your repository securely. |
0 commit comments