diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 9f625b1..9d6c48d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -68,34 +68,43 @@ jobs: accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} command: versions upload --preview-alias=pr-${{ github.event.pull_request.number }} - - name: Publish GitHub preview deployment - if: github.event_name == 'pull_request' + - name: Deploy to Cloudflare Workers + if: github.event_name == 'push' + id: deploy_production + uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: deploy + + - name: Publish GitHub deployment env: + DEPLOYED_SHA: ${{ github.event.pull_request.head.sha || github.sha }} + DEPLOYMENT_ENVIRONMENT: ${{ github.event_name == 'pull_request' && 'preview' || 'production' }} + DEPLOYMENT_URL: ${{ steps.deploy_preview.outputs.deployment-url || 'https://hanji.sxzz.moe' }} GH_TOKEN: ${{ github.token }} - PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} PR_NUMBER: ${{ github.event.pull_request.number }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - VERSION_PREVIEW_URL: ${{ steps.deploy_preview.outputs.deployment-url }} run: | - version_host="${VERSION_PREVIEW_URL#https://}" - preview_host="${version_host#*-}" - test "$preview_host" != "$version_host" + if test "$DEPLOYMENT_ENVIRONMENT" = "preview"; then + version_host="${DEPLOYMENT_URL#https://}" + preview_host="${version_host#*-}" + test "$preview_host" != "$version_host" + DEPLOYMENT_URL="https://pr-$PR_NUMBER-$preview_host" + fi - preview_url="https://pr-$PR_NUMBER-$preview_host" - - environment="preview-pr-$PR_NUMBER" deployment_id="$( jq -n \ - --arg ref "$PR_HEAD_SHA" \ - --arg environment "$environment" \ + --arg ref "$DEPLOYED_SHA" \ + --arg environment "$DEPLOYMENT_ENVIRONMENT" \ '{ ref: $ref, environment: $environment, auto_merge: false, required_contexts: [], - transient_environment: true, - production_environment: false, - description: "Cloudflare Workers preview" + transient_environment: ($environment == "preview"), + production_environment: ($environment == "production"), + description: "Cloudflare Workers \($environment)" }' | gh api \ --method POST \ @@ -105,30 +114,21 @@ jobs: )" jq -n \ - --arg environment "$environment" \ - --arg environment_url "$preview_url" \ + --arg environment "$DEPLOYMENT_ENVIRONMENT" \ + --arg environment_url "$DEPLOYMENT_URL" \ --arg log_url "$RUN_URL" \ '{ state: "success", environment: $environment, environment_url: $environment_url, log_url: $log_url, - description: "Cloudflare Workers preview is ready" + description: "Cloudflare Workers \($environment) is ready" }' | gh api \ --method POST \ "repos/${GITHUB_REPOSITORY}/deployments/${deployment_id}/statuses" \ --input - - - name: Deploy to Cloudflare Workers - if: github.event_name == 'push' - id: deploy_production - uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0 - with: - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - command: deploy - - name: Fail Workflow on Unit Test Failure if: ${{ always() && github.event_name == 'pull_request' && steps.unit_test.outcome == 'failure' }} run: exit 1