|
| 1 | +name: Deploy Production and Preview |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + artifact_name: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + condition_production: |
| 10 | + required: true |
| 11 | + type: boolean |
| 12 | + domain_preview: |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + ssh_host: |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + ssh_user: |
| 19 | + required: true |
| 20 | + type: string |
| 21 | + ssh_port: |
| 22 | + required: false |
| 23 | + default: 22 |
| 24 | + type: number |
| 25 | + ssh_timeout: |
| 26 | + required: false |
| 27 | + default: "1m" |
| 28 | + type: string |
| 29 | + ssh_command_timeout: |
| 30 | + required: false |
| 31 | + default: "2m" |
| 32 | + type: string |
| 33 | + ssh_rm: |
| 34 | + required: false |
| 35 | + default: true |
| 36 | + type: boolean |
| 37 | + ssh_strip_components: |
| 38 | + required: false |
| 39 | + default: 1 |
| 40 | + type: number |
| 41 | + dir_base: |
| 42 | + required: true |
| 43 | + type: string |
| 44 | + dir_production: |
| 45 | + required: true |
| 46 | + type: string |
| 47 | + dir_preview_base: |
| 48 | + required: true |
| 49 | + type: string |
| 50 | + dir_preview_subdir: |
| 51 | + required: true |
| 52 | + type: string |
| 53 | + linkchecker_enabled: |
| 54 | + required: false |
| 55 | + default: true |
| 56 | + type: boolean |
| 57 | + linkchecker_exclude: |
| 58 | + required: false |
| 59 | + default: "" |
| 60 | + type: string |
| 61 | + linkchecker_include_fragments: |
| 62 | + required: false |
| 63 | + default: "true" |
| 64 | + type: string |
| 65 | + linkchecker_max_concurrency: |
| 66 | + required: false |
| 67 | + default: 1 |
| 68 | + type: number |
| 69 | + linkchecker_user_agent: |
| 70 | + required: false |
| 71 | + default: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" |
| 72 | + type: string |
| 73 | + linkchecker_retry_times: |
| 74 | + required: false |
| 75 | + default: 5 |
| 76 | + type: number |
| 77 | + linkchecker_fail_on_errors: |
| 78 | + required: false |
| 79 | + default: false |
| 80 | + type: boolean |
| 81 | + sticky_comment_enabled: |
| 82 | + required: false |
| 83 | + default: true |
| 84 | + type: boolean |
| 85 | + secrets: |
| 86 | + ssh_key: |
| 87 | + required: true |
| 88 | + |
| 89 | +jobs: |
| 90 | + vars: |
| 91 | + runs-on: ubuntu-24.04 |
| 92 | + outputs: |
| 93 | + pr_closed_merged: ${{ steps.pr_status.outputs.pr_closed_merged }} |
| 94 | + datetime: ${{ steps.datetime.outputs.datetime }} |
| 95 | + linkchecker_fragments: ${{ steps.linkchecker_fragments.outputs.linkchecker_fragments }} |
| 96 | + linkchecker_exclude: ${{ steps.linkchecker_exclude.outputs.linkchecker_exclude }} |
| 97 | + steps: |
| 98 | + - id: pr_status |
| 99 | + run: echo "pr_closed_merged=${{ github.event.pull_request.closed_at || github.event.pull_request.merged }}" >> "$GITHUB_OUTPUT" |
| 100 | + |
| 101 | + - id: datetime |
| 102 | + run: echo "datetime=$(date '+%Y-%m-%d %H:%M:%S %Z')" >> "$GITHUB_OUTPUT" |
| 103 | + |
| 104 | + - id: linkchecker_fragments |
| 105 | + run: | |
| 106 | + if [ "${{ inputs.linkchecker_include_fragments }}" == 'true' ]; then |
| 107 | + echo "linkchecker_fragments=--include-fragments" >> "$GITHUB_OUTPUT" |
| 108 | + else |
| 109 | + echo "linkchecker_fragments=" >> "$GITHUB_OUTPUT" |
| 110 | + fi |
| 111 | +
|
| 112 | + - id: linkchecker_exclude |
| 113 | + run: | |
| 114 | + if [ -n "${{ inputs.linkchecker_exclude }}" ]; then |
| 115 | + excludes=$(echo "${{ inputs.linkchecker_exclude }}" | sed 's/,/ --exclude /g; s/^/--exclude /') |
| 116 | + echo "linkchecker_exclude=$excludes" >> "$GITHUB_OUTPUT" |
| 117 | + else |
| 118 | + echo "linkchecker_exclude=" >> "$GITHUB_OUTPUT" |
| 119 | + fi |
| 120 | +
|
| 121 | + linkchecker: |
| 122 | + runs-on: ubuntu-24.04 |
| 123 | + needs: vars |
| 124 | + if: inputs.linkchecker_enabled == true && (needs.vars.outputs.pr_closed_merged == '' || needs.vars.outputs.pr_closed_merged == 'false') |
| 125 | + steps: |
| 126 | + - name: Download artifact |
| 127 | + uses: actions/download-artifact@v4 |
| 128 | + with: |
| 129 | + name: ${{ inputs.artifact_name }} |
| 130 | + path: . |
| 131 | + |
| 132 | + - name: Link Checker |
| 133 | + uses: lycheeverse/lychee-action@v2 |
| 134 | + with: |
| 135 | + args: >- |
| 136 | + -r ${{ inputs.linkchecker_retry_times }} |
| 137 | + -u "${{ inputs.linkchecker_user_agent }}" |
| 138 | + ${{ needs.vars.outputs.linkchecker_fragments }} |
| 139 | + ${{ needs.vars.outputs.linkchecker_exclude }} |
| 140 | + --max-concurrency ${{ inputs.linkchecker_max_concurrency }} . |
| 141 | + fail: ${{ inputs.linkchecker_fail_on_errors }} |
| 142 | + |
| 143 | + # Deployment jobs |
| 144 | + deploy-production: |
| 145 | + runs-on: ubuntu-24.04 |
| 146 | + needs: |
| 147 | + - vars |
| 148 | + # Only deploy if production condition is met |
| 149 | + if: inputs.condition_production |
| 150 | + steps: |
| 151 | + - name: Download artifact |
| 152 | + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 |
| 153 | + with: |
| 154 | + name: ${{ inputs.artifact_name }} |
| 155 | + path: ./${{ inputs.artifact_name }} |
| 156 | + |
| 157 | + - name: Copy website to host |
| 158 | + uses: appleboy/scp-action@ff85246acaad7bdce478db94a363cd2bf7c90345 # v1.0.0 |
| 159 | + with: |
| 160 | + host: ${{ inputs.ssh_host }} |
| 161 | + username: ${{ inputs.ssh_user }} |
| 162 | + key: ${{ secrets.ssh_key }} |
| 163 | + port: ${{ inputs.ssh_port }} |
| 164 | + timeout: ${{ inputs.ssh_timeout }} |
| 165 | + command_timeout: ${{ inputs.ssh_command_timeout }} |
| 166 | + target: ${{ inputs.dir_base }}/${{ inputs.dir_production }}/ |
| 167 | + source: "${{ inputs.artifact_name }}/*" |
| 168 | + rm: ${{ inputs.ssh_rm }} |
| 169 | + strip_components: ${{ inputs.ssh_strip_components }} |
| 170 | + |
| 171 | + deploy-preview: |
| 172 | + runs-on: ubuntu-24.04 |
| 173 | + needs: |
| 174 | + - vars |
| 175 | + # Only deploy if non-closed/non-merged pull request |
| 176 | + if: github.event.pull_request && needs.vars.outputs.pr_closed_merged == 'false' |
| 177 | + steps: |
| 178 | + - name: Download artifact |
| 179 | + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 |
| 180 | + with: |
| 181 | + name: ${{ inputs.artifact_name }} |
| 182 | + path: ./${{ inputs.artifact_name }} |
| 183 | + |
| 184 | + - name: Copy website to host |
| 185 | + uses: appleboy/scp-action@ff85246acaad7bdce478db94a363cd2bf7c90345 # v1.0.0 |
| 186 | + with: |
| 187 | + host: ${{ inputs.ssh_host }} |
| 188 | + username: ${{ inputs.ssh_user }} |
| 189 | + key: ${{ secrets.ssh_key }} |
| 190 | + port: ${{ inputs.ssh_port }} |
| 191 | + timeout: ${{ inputs.ssh_timeout }} |
| 192 | + command_timeout: ${{ inputs.ssh_command_timeout }} |
| 193 | + target: ${{ inputs.dir_base }}/${{ inputs.dir_preview_base }}/${{ inputs.dir_preview_subdir }}/ |
| 194 | + source: "${{ inputs.artifact_name }}/*" |
| 195 | + rm: ${{ inputs.ssh_rm }} |
| 196 | + strip_components: ${{ inputs.ssh_strip_components }} |
| 197 | + |
| 198 | + - name: Inform about Preview URL |
| 199 | + uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2.9.4 |
| 200 | + if: inputs.sticky_comment_enabled == true |
| 201 | + with: |
| 202 | + header: pr-preview |
| 203 | + message: | |
| 204 | + Pull Request Live Preview |
| 205 | + :---: |
| 206 | + | <p><br />:rocket: View preview at <br />https://${{ inputs.domain_preview }}/${{ inputs.dir_preview_subdir }}/<br /><br /></p> |
| 207 | + | <p>Last updated: ${{ needs.vars.outputs.datetime }}</p> |
| 208 | +
|
| 209 | + remove-preview: |
| 210 | + runs-on: ubuntu-24.04 |
| 211 | + needs: |
| 212 | + - vars |
| 213 | + # Only deploy if non-closed/non-merged pull request |
| 214 | + if: github.event.pull_request && needs.vars.outputs.pr_closed_merged != 'false' |
| 215 | + steps: |
| 216 | + - name: Create deletion notice |
| 217 | + run: mkdir -p ${{ inputs.artifact_name }} && echo "This PR has been closed or merged. The preview has been removed." > ${{ inputs.artifact_name }}/index.html |
| 218 | + |
| 219 | + - name: Copy empty folder to host |
| 220 | + uses: appleboy/scp-action@ff85246acaad7bdce478db94a363cd2bf7c90345 # v1.0.0 |
| 221 | + with: |
| 222 | + host: ${{ inputs.ssh_host }} |
| 223 | + username: ${{ inputs.ssh_user }} |
| 224 | + key: ${{ secrets.ssh_key }} |
| 225 | + port: ${{ inputs.ssh_port }} |
| 226 | + timeout: ${{ inputs.ssh_timeout }} |
| 227 | + command_timeout: ${{ inputs.ssh_command_timeout }} |
| 228 | + target: ${{ inputs.dir_base }}/${{ inputs.dir_preview_base }}/${{ inputs.dir_preview_subdir }}/ |
| 229 | + source: "${{ inputs.artifact_name }}/*" |
| 230 | + rm: ${{ inputs.ssh_rm }} |
| 231 | + strip_components: ${{ inputs.ssh_strip_components }} |
| 232 | + |
| 233 | + - name: Inform about preview removal |
| 234 | + uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2.9.4 |
| 235 | + if: inputs.sticky_comment_enabled == true |
| 236 | + with: |
| 237 | + header: pr-preview |
| 238 | + message: | |
| 239 | + Pull Request Live Preview |
| 240 | + :---: |
| 241 | + | <p><br />:x: Preview has been removed.<br /><br /></p> |
0 commit comments