From 06fdb8d9769fd0ec9cc265df9977e4bfadf2f981 Mon Sep 17 00:00:00 2001 From: Mogyuchi Date: Sat, 5 Aug 2023 15:02:44 +0900 Subject: [PATCH] ci: create deployment action (#342) --- .github/workflows/deploy.yml | 126 +++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 000000000..fbe957d8b --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,126 @@ +name: Cloudflare Pages + +env: + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + CLOUDFLARE_PROJECT_NAME: ${{ vars.CLOUDFLARE_PROJECT_NAME }} + CLOUDFLARE_DIRECTORY: _site + +on: + push: + branches: ["**"] + pull_request_target: + +jobs: + pre-deployment: + outputs: + branch: ${{ steps.get.outputs.branch }} + environment: ${{ steps.get.outputs.environment }} + runs-on: ubuntu-latest + steps: + - id: get + run: | + if ${{ github.event_name == 'push' }}; then + echo "branch=${{ github.ref_name }}" >> "$GITHUB_OUTPUT" + if ${{ github.ref_name == 'main' }}; then + echo "environment=Production" >> "$GITHUB_OUTPUT" + else + echo "environment=Preview" >> "$GITHUB_OUTPUT" + fi + elif ${{ github.event_name == 'pull_request_target' }}; then + echo "branch=pull/${{ github.event.number }}/merge" >> "$GITHUB_OUTPUT" + if ${{ github.event.head.repo.html_url != github.repository.html_url }}; then + echo "environment=External PR" >> "$GITHUB_OUTPUT" + else + echo "environment=Preview" >> "$GITHUB_OUTPUT" + fi + else + exit 1 + fi + deployment: + needs: ["pre-deployment"] + env: + BRANCH: ${{ needs.pre-deployment.outputs.branch }} + runs-on: ubuntu-latest + environment: + name: ${{ needs.pre-deployment.outputs.environment }} + url: ${{ steps.publish.outputs.url }} + + permissions: + contents: write + deployments: write + #pull-requests: write + + steps: + - id: pr-checkout + if: startsWith( needs.pre-deployment.outputs.branch, 'pull/' ) && endsWith( needs.pre-deployment.outputs.branch, '/merge' ) + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 + with: + ref: refs/${{ env.BRANCH }} + + - if: steps.pr-checkout.conclusion == 'skipped' + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 + with: + ref: ${{ env.BRANCH }} + + - run: mkdir _site && cp README.md _site/ + + - id: publish + uses: cloudflare/pages-action@f0a1cd58cd66095dee69bfa18fa5efd1dde93bca # v1.5.0 + with: + apiToken: ${{ env.CLOUDFLARE_API_TOKEN }} + accountId: ${{ env.CLOUDFLARE_ACCOUNT_ID }} + projectName: ${{ env.CLOUDFLARE_PROJECT_NAME }} + directory: ${{ env.CLOUDFLARE_DIRECTORY }} + gitHubToken: ${{ secrets.GITHUB_TOKEN }} + branch: ${{ env.BRANCH }} + + - if: ${{ ! cancelled() && github.event_name == 'pull_request_target' }} + uses: peter-evans/find-comment@a54c31d7fa095754bfef525c0c8e5e5674c4b4b1 # v2.4.0 + id: fc + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: "github-actions[bot]" + body-includes: DEPLOYMENT_COMMENT + + - if: steps.fc.conclusion == 'success' + uses: peter-evans/create-or-update-comment@c6c9a1a66007646a28c153e2a8580a5bad27bcfa # v3.0.2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + + + + + +
Latest commit: + ${{ github.event.pull_request.head.sha }} +
Status: ✅  Deploy successful!
Preview URL: + ${{ steps.publish.outputs.url }} +
Branch Preview URL: + https://pr-${{ github.event.pull_request.number }}.fork-pr-deploy-test.pages.dev +
+ + [View workflow logs](${{ github.repository.html_url }}/actions/runs/${{ github.run_id }}) + [View Cloudflare logs](https://dash.cloudflare.com/?to=/${{ env.CLOUDFLARE_ACCOUNT_ID }}/pages/view/${{ env.CLOUDFLARE_PROJECT_NAME }}/${{ steps.publish.outputs.id }}) + edit-mode: replace + + - uses: peter-evans/create-or-update-comment@c6c9a1a66007646a28c153e2a8580a5bad27bcfa # v3.0.2 + if: failure() && steps.fc.conclusion == 'success' + with: + token: ${{ secrets.GITHUB_TOKEN}} + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + + + +
Latest commit: + ${{ github.event.pull_request.head.sha }} +
Status: ❌  Deploy failed!
+ + [View workflow logs](${{ github.repository.html_url }}/actions/runs/${{ github.run_id }}) + [View Cloudflare logs](https://dash.cloudflare.com/?to=/${{ env.CLOUDFLARE_ACCOUNT_ID }}/pages/view/${{ env.CLOUDFLARE_PROJECT_NAME }}/${{ steps.publish.outputs.id }}) + edit-mode: replace