Deploy Preview & Publish Production #61
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| name: Deploy Preview & Publish Production | |
| on: | |
| workflow_dispatch: | |
| pull_request: | |
| push: | |
| branches: [main] | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 5 | |
| outputs: | |
| success: ${{ steps.deploy-preview.outputs.command-stderr == '' }} | |
| version_tag: ${{ steps.get-version.outputs.version }} | |
| steps: | |
| - name: Setup | Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup | Bun | |
| uses: oven-sh/setup-bun@v2 | |
| with: | |
| bun-version-file: ".bun-version" | |
| - name: Setup | Install dependencies | |
| run: bun install --frozen-lockfile | |
| - name: Setup | Post comment | |
| if: github.event_name == 'pull_request' | |
| run: | | |
| gh pr comment ${{ github.event.pull_request.number }} --edit-last --create-if-none --body-file - <<EOF | |
| ### <span aria-hidden="true">๐ง</span> Deploy Preview building... | |
| | Name | Link | | |
| |---------------------------------|------------------------| | |
| |<span aria-hidden="true">๐จ</span> Latest commit | ${{ github.sha }} | | |
| |<span aria-hidden="true">๐</span> Latest deploy log | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | | |
| --- | |
| EOF | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| - name: Setup | Get version | |
| id: get-version | |
| run: | | |
| version="$(git rev-parse --short ${{ github.sha }})" | |
| echo "version=${version}" >> $GITHUB_OUTPUT | |
| - name: Cache content repo | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| content/.git | |
| content | |
| key: content-${{ hashFiles('package.json') }}-${{ runner.os }} | |
| restore-keys: | | |
| content- | |
| - name: Run | Build | |
| run: bun run build | |
| env: | |
| GOOGLE_ANALYTICS_ID: ${{ secrets.GOOGLE_ANALYTICS_ID }} | |
| - name: Run | Deploy | |
| uses: cloudflare/wrangler-action@v3 | |
| id: deploy-preview | |
| with: | |
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| command: >- | |
| versions | |
| upload | |
| --tag "${{ steps.get-version.outputs.version }}" | |
| --message "${{ github.event_name == 'pull_request' && format('Preview deployment for PR #{0}', github.event.pull_request.number) || 'Release deployment' }}" | |
| - name: Finish | Post comment | |
| if: github.event_name == 'pull_request' | |
| run: | | |
| gh pr comment ${{ github.event.pull_request.number }} --edit-last --create-if-none --body-file - <<EOF | |
| ### <span aria-hidden="true">โ </span> Deploy Preview ready! | |
| | Name | Link | | |
| |---------------------------------|------------------------| | |
| |<span aria-hidden="true">๐จ</span> Latest commit | ${{ github.sha }} | | |
| |<span aria-hidden="true">๐</span> Latest deploy log | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | | |
| |<span aria-hidden="true">๐</span> Preview link | [${{ steps.deploy-preview.outputs.deployment-url }}](${{ steps.deploy-preview.outputs.deployment-url }}) | | |
| --- | |
| EOF | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| publish: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 5 | |
| needs: deploy | |
| if: needs.deploy.outputs.success == 'true' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') | |
| environment: | |
| name: production | |
| url: ${{ steps.publish.outputs.deployment-url }} | |
| steps: | |
| - name: Setup | Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup | Bun | |
| uses: oven-sh/setup-bun@v2 | |
| with: | |
| bun-version-file: ".bun-version" | |
| - name: Setup | Install dependencies | |
| run: bun install --frozen-lockfile | |
| - name: Setup | Get version ID | |
| id: get-version-id | |
| env: | |
| CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| VERSION_TAG: ${{ needs.deploy.outputs.version_tag }} | |
| run: | | |
| set -euo pipefail | |
| # versions list ใฏ้ ๅใ่ฟใใฎใงใๆๆฐใฎไธ่ฆใๅๅพใใ | |
| version_id="$(bun wrangler versions list --json \ | |
| | jq -r --arg tag "$VERSION_TAG" 'map(select(.annotations["workers/tag"] == $tag)) | sort_by(.created_on) | (.[-1].id // empty)')" | |
| if [ -z "$version_id" ]; then | |
| echo "No version found for tag: ${VERSION_TAG}" >&2 | |
| exit 1 | |
| fi | |
| echo "version_id=${version_id}" >> "$GITHUB_OUTPUT" | |
| - name: Run | Publish | |
| uses: cloudflare/wrangler-action@v3 | |
| id: publish | |
| with: | |
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| command: versions deploy ${{ steps.get-version-id.outputs.version_id }} -y |