From 9f9a262904119459396f7a223f570b60343e7cee Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Wed, 18 Dec 2024 22:43:12 +0900 Subject: [PATCH] fix: added worker deploy / delete --- .../{deploy.yml => update-configuration.yml} | 7 +- .github/workflows/worker-delete.yml | 45 ++++++++ .github/workflows/worker-deploy.yml | 107 ++++++++++++++++++ 3 files changed, 156 insertions(+), 3 deletions(-) rename .github/workflows/{deploy.yml => update-configuration.yml} (67%) create mode 100644 .github/workflows/worker-delete.yml create mode 100644 .github/workflows/worker-deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/update-configuration.yml similarity index 67% rename from .github/workflows/deploy.yml rename to .github/workflows/update-configuration.yml index 8e0a703..4307994 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/update-configuration.yml @@ -5,17 +5,18 @@ on: push: jobs: - update: + update-manifest: name: "Update Configuration & Build" runs-on: ubuntu-latest permissions: write-all steps: - - uses: ubiquity-os/action-deploy-plugin@main + - name: Update Manifest and Commit Changes + uses: ubiquity-os/action-deploy-plugin@main with: treatAsEsm: false sourcemap: false - pluginEntry: "${{ github.workspace }}/src/main.ts" + pluginEntry: ${{ github.workspace }}/src/main.ts env: APP_ID: ${{ secrets.APP_ID }} APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} diff --git a/.github/workflows/worker-delete.yml b/.github/workflows/worker-delete.yml new file mode 100644 index 0000000..482ae88 --- /dev/null +++ b/.github/workflows/worker-delete.yml @@ -0,0 +1,45 @@ +name: Delete Deployment + +on: + delete: + +jobs: + delete: + runs-on: ubuntu-latest + name: Delete Deployment + environment: ${{ github.ref == 'refs/heads/main' && 'main' || 'development' }} + steps: + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: "20.10.0" + + - name: Enable corepack + run: corepack enable + + - uses: actions/checkout@v4 + + - name: Get Deleted Branch Name + id: get_branch + run: | + branch_name=$(echo '${{ github.ref }}' | sed 's#refs/heads/##' | sed 's#[^a-zA-Z0-9]#-#g') + echo "branch_name=$branch_name" >> $GITHUB_ENV + - name: Retrieve and Construct Full Worker Name + id: construct_worker_name + run: | + base_name=$(grep '^name = ' wrangler.toml | sed 's/^name = "\(.*\)"$/\1/') + full_worker_name="${base_name}-${{ env.branch_name }}" + # Make sure that it doesnt exceed 63 characters or it will break RFC 1035 + full_worker_name=$(echo "${full_worker_name}" | cut -c 1-63) + echo "full_worker_name=$full_worker_name" >> $GITHUB_ENV + - name: Delete Deployment with Wrangler + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: delete --name ${{ env.full_worker_name }} + + - name: Output Deletion Result + run: | + echo "### Deployment URL" >> $GITHUB_STEP_SUMMARY + echo 'Deployment `${{ env.full_worker_name }}` has been deleted.' >> $GITHUB_STEP_SUMMARY diff --git a/.github/workflows/worker-deploy.yml b/.github/workflows/worker-deploy.yml new file mode 100644 index 0000000..49991a4 --- /dev/null +++ b/.github/workflows/worker-deploy.yml @@ -0,0 +1,107 @@ +name: Deploy Worker + +on: + workflow_dispatch: + workflow_run: + workflows: ["Update Configuration and Build"] + types: + - completed + +env: + APP_ID: ${{ secrets.APP_ID }} + APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} + +jobs: + deploy: + runs-on: ubuntu-latest + name: Deploy + environment: ${{ github.ref == 'refs/heads/main' && 'main' || 'development' }} + permissions: + contents: write + + steps: + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: "20.10.0" + + - uses: actions/checkout@v4 + + - name: Setup Bun + uses: oven-sh/setup-bun@v2 + + - name: Update wrangler.toml Name Field + run: | + branch_name=$(echo '${{ github.ref }}' | sed 's#refs/heads/##' | sed 's#[^a-zA-Z0-9]#-#g') + # Extract base name from wrangler.toml + base_name=$(grep '^name = ' wrangler.toml | sed 's/^name = "\(.*\)"$/\1/') + # Concatenate branch name with base name + new_name="${base_name}-${branch_name}" + # Truncate the new name to 63 characters for RFC 1035 + new_name=$(echo "$new_name" | cut -c 1-63) + # Update the wrangler.toml file + sed -i "s/^name = .*/name = \"$new_name\"/" wrangler.toml + echo "Updated wrangler.toml name to: $new_name" + + - name: Deploy with Wrangler + id: wrangler_deploy + uses: cloudflare/wrangler-action@v3 + with: + wranglerVersion: "3.87.0" + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + secrets: | + SUPABASE_URL + SUPABASE_KEY + VOYAGEAI_API_KEY + ${{ secrets.KERNEL_PUBLIC_KEY && secrets.KERNEL_PUBLIC_KEY != '' && 'KERNEL_PUBLIC_KEY' || '' }} + env: + SUPABASE_URL: ${{ secrets.SUPABASE_URL }} + SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }} + KERNEL_PUBLIC_KEY: ${{ secrets.KERNEL_PUBLIC_KEY }} + VOYAGEAI_API_KEY: ${{secrets.VOYAGEAI_API_KEY}} + + - name: Update manifest.json worker url + uses: actions/github-script@v7 + with: + script: | + const fs = require('fs'); + const path = require('path'); + + const manifestPath = path.resolve("${{ github.workspace }}", './manifest.json'); + const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); + + manifest["homepage_url"] = "${{ steps.wrangler_deploy.outputs.deployment-url }}"; + + const updatedManifest = JSON.stringify(manifest, null, 2); + fs.writeFileSync(manifestPath, updatedManifest); + console.log('Updated manifest:', updatedManifest); + + - name: Get GitHub App token + if: env.APP_ID != '' && env.APP_PRIVATE_KEY != '' + uses: actions/create-github-app-token@v1 + id: app-token + with: + app-id: ${{ env.APP_ID }} + private-key: ${{ env.APP_PRIVATE_KEY }} + + - name: Format manifest.json using Prettier + shell: bash + run: | + bun add -DE prettier + bun prettier --write . + + - name: Commit file + uses: swinton/commit@v2.x + env: + GH_TOKEN: ${{ steps.app-token.outputs.token || secrets.GITHUB_TOKEN }} + with: + files: | + manifest.json + commit-message: "chore: [skip ci] update manifest.json url" + ref: ${{ github.ref }} + + - name: Write Deployment URL to Summary + run: | + echo "### Deployment URL" >> $GITHUB_STEP_SUMMARY + echo "${{ steps.wrangler_deploy.outputs.deployment-url }}" >> $GITHUB_STEP_SUMMARY