|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +name: Deploy Versioned Pages |
| 15 | +description: Will push the documentation to the gh-pages branch, possibly with a versioned URL. When the PR is closed, the documentation will be deleted. |
| 16 | +# Note: this has some shortcomings, like race conditions when multiple PRs are opened at the same time, |
| 17 | +# problems with overwriting existing files, not deleting deleted files, etc. |
| 18 | +# We'll address these when we transform this action into a truly reusable independent action. |
| 19 | +# But for now, let's get it working! |
| 20 | + |
| 21 | +inputs: |
| 22 | + source_folder: |
| 23 | + description: "Path to the html files to deploy in current working directory" |
| 24 | + required: true |
| 25 | + versions_file: |
| 26 | + description: "Path to the versions file on gh-pages branch" |
| 27 | + default: "versions.json" |
| 28 | + create_comment: |
| 29 | + description: "Create a comment on the PR with the URL to the documentation" # in case of PR |
| 30 | + default: "true" |
| 31 | + |
| 32 | +outputs: |
| 33 | + target_folder: |
| 34 | + description: "The target folder for the documentation" |
| 35 | + value: ${{ steps.calc.outputs.target_folder }} |
| 36 | + |
| 37 | +runs: |
| 38 | + using: "composite" |
| 39 | + steps: |
| 40 | + - name: Determine target_folder |
| 41 | + id: calc |
| 42 | + shell: bash |
| 43 | + run: | |
| 44 | + if [[ "${{ github.event_name }}" == 'pull_request_target' || "${{ github.event_name }}" == 'pull_request' ]]; then |
| 45 | + target_folder="pr-${{ github.event.pull_request.number }}" |
| 46 | + else |
| 47 | + target_folder="${{github.ref_name}}" |
| 48 | + fi |
| 49 | + echo "target_folder=$target_folder" >> $GITHUB_OUTPUT |
| 50 | +
|
| 51 | + - name: Prepare the deploy folder |
| 52 | + shell: bash |
| 53 | + run: | |
| 54 | + # Prepare the deploy folder |
| 55 | + mkdir -p deploy_root |
| 56 | + mkdir -p version_root |
| 57 | + # Move the files to the deploy folder |
| 58 | + mv ${{ inputs.source_folder }}/* deploy_root/ |
| 59 | + # Ensure that the folder is not treated as a Jekyll site |
| 60 | + touch deploy_root/.nojekyll |
| 61 | +
|
| 62 | + # Add the target folder to the versions file |
| 63 | + BASE_REPO="https://github.com/${{ github.repository }}.git" |
| 64 | +
|
| 65 | + echo "Fetching gh-pages from BASE_REPO: $BASE_REPO" |
| 66 | + git remote add base "$BASE_REPO" || git remote set-url base "$BASE_REPO" |
| 67 | + git fetch base gh-pages --depth 1 |
| 68 | +
|
| 69 | + # Checkout only the versions file from gh-pages branch of the base repo |
| 70 | + git checkout base/gh-pages -- "${{ inputs.versions_file }}" |
| 71 | +
|
| 72 | + target="${{ steps.calc.outputs.target_folder }}" |
| 73 | + new_version="${{ steps.calc.outputs.target_folder }}" |
| 74 | +
|
| 75 | +
|
| 76 | + if jq -e --arg version "$new_version" 'map(select(.version == $version)) | length > 0' "${{ inputs.versions_file }}" > /dev/null; then |
| 77 | + echo "Version '$new_version' already exists in ${{ inputs.versions_file }}" |
| 78 | + else |
| 79 | + REPO_NAME=$(basename ${{ github.repository }}) |
| 80 | + USER_NAME=$(echo ${{ github.repository }} | cut -d'/' -f1) |
| 81 | + GITHUB_PAGES_URL="https://${USER_NAME}.github.io/${REPO_NAME}" |
| 82 | + if [ "$target" = "/" ]; then |
| 83 | + new_url="${GITHUB_PAGES_URL}/" |
| 84 | + else |
| 85 | + new_url="${GITHUB_PAGES_URL}/$target/" |
| 86 | + fi |
| 87 | +
|
| 88 | + jq --arg version "$new_version" --arg url "$new_url" '. + [{"version": $version, "url": $url}]' "${{ inputs.versions_file }}" > tmp_versions.json |
| 89 | + mv tmp_versions.json "${{ inputs.versions_file }}" |
| 90 | + fi |
| 91 | + mv "${{ inputs.versions_file }}" version_root/ |
| 92 | + ls -al . |
| 93 | + ls -al deploy_root |
| 94 | + ls -al version_root |
| 95 | + cat version_root/"${{ inputs.versions_file }}" |
| 96 | +
|
| 97 | + - name: Deploy Documentation |
| 98 | + uses: JamesIves/github-pages-deploy-action@v4 |
| 99 | + with: |
| 100 | + folder: deploy_root |
| 101 | + target-folder: ${{ steps.calc.outputs.target_folder }} |
| 102 | + clean: true |
| 103 | + clean-exclude: .nojekyll |
| 104 | + |
| 105 | + - name: Deploy version file 🚀 |
| 106 | + uses: JamesIves/github-pages-deploy-action@v4 |
| 107 | + with: |
| 108 | + folder: version_root |
| 109 | + clean: false |
| 110 | + |
| 111 | + - name: Find Comment |
| 112 | + if: ${{ github.event_name == 'pull_request_target' }} |
| 113 | + uses: peter-evans/find-comment@v3 |
| 114 | + id: fc |
| 115 | + with: |
| 116 | + issue-number: ${{ github.event.pull_request.number }} |
| 117 | + comment-author: 'github-actions[bot]' |
| 118 | + body-includes: The created documentation from the pull request |
| 119 | + |
| 120 | + - name: Comment on PR with docs URL |
| 121 | + if: ${{ github.event_name == 'pull_request_target' && inputs.create_comment == 'true' && steps.fc.outputs.comment-id == '' }} |
| 122 | + uses: peter-evans/create-or-update-comment@v4 |
| 123 | + with: |
| 124 | + issue-number: ${{github.event.pull_request.number}} |
| 125 | + body: | |
| 126 | + The created documentation from the pull request is available at: [docu-html](https://eclipse-score.github.io/score/${{steps.calc.outputs.target_folder}}) |
| 127 | + reactions: rocket |
0 commit comments