|
| 1 | +# How to set up docs previews |
| 2 | + |
| 3 | +This guide will help you set up docs previews for your GitHub repository. |
| 4 | + |
| 5 | +## GitHub Workflows |
| 6 | + |
| 7 | +The docs preview system consists of three GitHub Workflows: |
| 8 | +- [`docs-build.yml`](#build): **Build** the docs on a PR |
| 9 | +- [`docs-deploy.yml`](#deploy): **Deploy** the docs to our preview environment |
| 10 | +- [`docs-cleanup.yml`](#cleanup): **Cleanup** the docs after a PR is merged or closed |
| 11 | + |
| 12 | + |
| 13 | +### Build |
| 14 | + |
| 15 | +This workflow is triggered when a PR is opened. The underlying reusable workflow, builds the documentation and uploads it as an artifact. |
| 16 | +If the `path-pattern` input does not match any changes in the PR, the workflow will skip the build, but still set a green status check. |
| 17 | +This way you only build and deploy the docs when there are changes to the docs and you can still set it as a required status check. |
| 18 | + |
| 19 | + |
| 20 | +::::{dropdown} .github/workflows/docs-build.yml |
| 21 | +:open: |
| 22 | + |
| 23 | +```yaml |
| 24 | +--- |
| 25 | +name: docs-build <1> |
| 26 | + |
| 27 | +on: |
| 28 | + pull_request: ~ |
| 29 | + |
| 30 | +jobs: |
| 31 | + docs-preview: |
| 32 | + uses: elastic/docs-builder/.github/workflows/preview-build.yml <2> |
| 33 | + with: |
| 34 | + path-pattern: docs/** <3> |
| 35 | + permissions: |
| 36 | + contents: read |
| 37 | + pull-requests: read |
| 38 | +``` |
| 39 | +
|
| 40 | +1. The naming is important so that the `docs-deploy` workflow is triggered. |
| 41 | +2. This should be the path to your docs folder. |
| 42 | +3. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-build.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-build.yml) |
| 43 | + |
| 44 | + |
| 45 | +:::: |
| 46 | + |
| 47 | +### Deploy |
| 48 | + |
| 49 | +This workflow is triggered when the `docs-build` workflow is completed. The underlying reusable workflow, downloads the artifact and deploys the docs to our preview environment. |
| 50 | + |
| 51 | + |
| 52 | +::::{dropdown} .github/workflows/docs-deploy.yml |
| 53 | +:open: |
| 54 | + |
| 55 | +```yaml |
| 56 | +--- |
| 57 | +name: docs-deploy |
| 58 | +
|
| 59 | +on: |
| 60 | + workflow_run: |
| 61 | + workflows: |
| 62 | + - docs-build <1> |
| 63 | + types: |
| 64 | + - completed |
| 65 | +
|
| 66 | +jobs: |
| 67 | + docs-preview: |
| 68 | + uses: elastic/docs-builder/.github/workflows/preview-deploy.yml <2> |
| 69 | + permissions: |
| 70 | + contents: none <3> |
| 71 | + id-token: write |
| 72 | + deployments: write |
| 73 | + actions: read |
| 74 | +``` |
| 75 | +1. The name of the previously mentioned `docs-build` workflow. |
| 76 | +2. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-deploy.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-deploy.yml) |
| 77 | +3. No need to read the code. |
| 78 | + |
| 79 | +:::: |
| 80 | + |
| 81 | +### Cleanup |
| 82 | + |
| 83 | +This workflow is triggered when a PR is either merged or closed. The underlying reusable workflow, deletes the docs from the preview environment. |
| 84 | + |
| 85 | +:::{note} |
| 86 | +We are aware of the security implications of using `pull_request_target` as described in [this blog post](https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/). |
| 87 | +The workflow never checks out the code and doesn't use any user modifiable inputs (e.g. PR title). |
| 88 | +::: |
| 89 | + |
| 90 | +::::{dropdown} .github/workflows/docs-cleanup.yml |
| 91 | +:open: |
| 92 | +```yaml |
| 93 | +--- |
| 94 | +name: docs-cleanup |
| 95 | +
|
| 96 | +on: |
| 97 | + pull_request_target: |
| 98 | + types: |
| 99 | + - closed |
| 100 | +
|
| 101 | +jobs: |
| 102 | + docs-preview: |
| 103 | + uses: elastic/docs-builder/.github/workflows/preview-cleanup.yml <1> |
| 104 | + permissions: |
| 105 | + contents: none <2> |
| 106 | + id-token: write |
| 107 | + deployments: write |
| 108 | +``` |
| 109 | + |
| 110 | +1. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-cleanup.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-cleanup.yml) |
| 111 | +2. No permissions to read content |
| 112 | + |
| 113 | +:::: |
| 114 | + |
| 115 | +## Required Status Checks |
| 116 | + |
| 117 | +To ensure that the docs are always in a deployable state, we need to set up required status checks. |
| 118 | + |
| 119 | +In this case only the `docs-build` workflow is required, which will produce a status check named `docs-preview / build`. |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +## Deployments |
| 125 | + |
| 126 | +If the `docs-build` workflow is successful, the `docs-deploy` workflow will create a deployment visible in the GitHub UI on your PR. |
| 127 | + |
| 128 | + |
0 commit comments