|
| 1 | +--- |
| 2 | +name: Ansible collection docs - publish to GitHub Pages |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + artifact-name: |
| 7 | + description: The build artifact that contains the rendered HTML. Required when action == 'publish' |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + destination-dir: |
| 11 | + description: | |
| 12 | + The destination path within the published site. Should not start with a forward slash '/'. |
| 13 | + If not given, the destination will be calculated based on what triggered the event: |
| 14 | + - pull - pr/<pr #> |
| 15 | + - push (branch) - branch/<branch name> |
| 16 | + - push (tag) - tag/<tag name> |
| 17 | + required: false |
| 18 | + type: string |
| 19 | + action: |
| 20 | + description: Action to perform. 'publish' to publish the site, 'teardown' to tear it down. |
| 21 | + required: false |
| 22 | + default: 'publish' |
| 23 | + type: string |
| 24 | + base-url: |
| 25 | + description: | |
| 26 | + The base URL of the published site. Do not include a trailing forward slash '/'. |
| 27 | + This is not used for the publishing process, but the resulting destination dir will be concatenated to it, |
| 28 | + and then returned in the workflow's outputs. |
| 29 | + required: false |
| 30 | + type: string |
| 31 | + default: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }} |
| 32 | + outputs: |
| 33 | + url: |
| 34 | + description: The destination path pre-pended with the base-url. |
| 35 | + value: ${{ jobs.publish-gh-pages.outputs.url }} |
| 36 | + secrets: |
| 37 | + GH_TOKEN: |
| 38 | + description: | |
| 39 | + The token used for publishing to GitHub Pages. |
| 40 | + Even when using the workflow token (secrets.GITHUB_TOKEN), it must be passed explicitly. |
| 41 | + required: true |
| 42 | +jobs: |
| 43 | + publish-gh-pages: |
| 44 | + name: Publish to GitHub Pages |
| 45 | + runs-on: ubuntu-latest |
| 46 | + permissions: |
| 47 | + contents: write |
| 48 | + outputs: |
| 49 | + url: ${{ steps.vars.outputs.url }} |
| 50 | + steps: |
| 51 | + - name: Process variables |
| 52 | + id: vars |
| 53 | + uses: actions/github-script@v6 |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const inputs = ${{ toJSON(inputs) }} |
| 57 | + var destination_dir = inputs['destination-dir'] |
| 58 | + var base_url = inputs['base-url'] |
| 59 | +
|
| 60 | + if (destination_dir == '') { |
| 61 | + const gh = ${{ toJSON(github) }} |
| 62 | +
|
| 63 | + if (gh['event_name'].startsWith('pull_request')) { |
| 64 | + destination_dir = `pr/${gh['event']['number']}` |
| 65 | + } |
| 66 | + else { |
| 67 | + destination_dir = `${gh['ref_type']}/${gh['ref_name']}` |
| 68 | + } |
| 69 | + } |
| 70 | +
|
| 71 | + core.setOutput('dest', destination_dir) |
| 72 | + core.setOutput('url', `${base_url}/${destination_dir}`) |
| 73 | +
|
| 74 | + - name: Check required |
| 75 | + if: inputs.action == 'publish' && inputs.artifact-name == '' |
| 76 | + run: | |
| 77 | + echo "::error title=Missing artifact-name::action was 'publish' but artifact-name was not supplied." |
| 78 | + exit 1 |
| 79 | +
|
| 80 | + - name: Retrieve rendered docs |
| 81 | + if: inputs.action == 'publish' |
| 82 | + id: download |
| 83 | + uses: actions/download-artifact@v3 |
| 84 | + with: |
| 85 | + name: ${{ inputs.artifact-name }} |
| 86 | + path: html |
| 87 | + |
| 88 | + - name: Teardown source |
| 89 | + if: inputs.action == 'teardown' |
| 90 | + run: mkdir -p html |
| 91 | + |
| 92 | + - name: Publish |
| 93 | + # this action uses a token with contents:write, pinning to commit |
| 94 | + # 068dc23d9710f1ba62e86896f84735d869951305 == v3.8.0 |
| 95 | + # https://github.com/peaceiris/actions-gh-pages/releases/tag/v3.8.0 |
| 96 | + uses: peaceiris/actions-gh-pages@068dc23d9710f1ba62e86896f84735d869951305 |
| 97 | + with: |
| 98 | + github_token: ${{ secrets.GH_TOKEN }} |
| 99 | + publish_dir: html |
| 100 | + keep_files: false # with destination_dir, this applies only to the chosen dir |
| 101 | + destination_dir: ${{ steps.vars.outputs.dest }} |
| 102 | + # NOTE: do not use the force_orphan (keep_history) option. |
| 103 | + # It does not yet work correctly with keep_files and destination_dir: |
| 104 | + # - https://github.com/peaceiris/actions-gh-pages/issues/455 |
0 commit comments