diff --git a/.github/workflows/deno-deploy.yml b/.github/workflows/deno-deploy.yml deleted file mode 100644 index b2d5f13..0000000 --- a/.github/workflows/deno-deploy.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Deno Deploy - -on: - workflow_dispatch: - push: - delete: - -jobs: - deploy: - runs-on: ubuntu-latest - name: Deploy - environment: ${{ (github.event.ref == 'refs/heads/main' || github.ref == 'refs/heads/main' || github.event.workflow_run.head_branch == 'main') && 'main' || 'development' }} - permissions: - contents: write - id-token: write - - steps: - - uses: actions/checkout@v4 - - uses: oven-sh/setup-bun@v2 - - name: Install dependencies and generate manifest - run: bun install --frozen-lockfile - - uses: ubiquity-os/deno-plugin-adapter@main - id: adapter - with: - pluginEntry: "./worker" - - uses: ubiquity-os/deno-deploy@main - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - KERNEL_PUBLIC_KEY: ${{ secrets.KERNEL_PUBLIC_KEY }} - APP_ID: ${{ secrets.APP_ID }} - APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} - USER_GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }} - with: - token: ${{ secrets.DENO_DEPLOY_TOKEN }} - action: ${{ github.event_name == 'delete' && 'delete' || 'deploy' }} - organization: ${{ secrets.DENO_ORG_NAME }} - entrypoint: ${{ steps.adapter.outputs.entrypoint }} diff --git a/.github/workflows/update-configuration.yml b/.github/workflows/update-configuration.yml index 3f7fe30..50ae0e1 100644 --- a/.github/workflows/update-configuration.yml +++ b/.github/workflows/update-configuration.yml @@ -1,24 +1,152 @@ -name: "Update Configuration and Build" +name: "Deploy" on: workflow_dispatch: + inputs: + build_action_enabled: + description: "Publish GitHub Action artifacts to dist/." + required: false + default: "true" + type: choice + options: + - "true" + - "false" + deploy_deno_enabled: + description: "Deploy Worker to Deno Deploy." + required: false + default: "true" + type: choice + options: + - "true" + - "false" + skip_bot_events: + description: "manifest.skipBotEvents value for generated manifest." + required: false + default: "false" + exclude_supported_events: + description: "Comma-separated supported events to exclude from manifest listeners." + required: false + default: "" push: + branches-ignore: + - "dist/**" + tags-ignore: + - "*" delete: jobs: - update: - name: "Update Configuration & Build" + deploy: + name: "Deploy (Action / Deno)" runs-on: ubuntu-latest permissions: write-all + environment: ${{ (github.event.ref == 'refs/heads/main' || github.ref == 'refs/heads/main' || github.event.workflow_run.head_branch == 'main') && 'main' || 'development' }} + env: + BUILD_ACTION_ENABLED: ${{ github.event_name == 'workflow_dispatch' && inputs.build_action_enabled || vars.BUILD_ACTION_ENABLED || 'true' }} + DEPLOY_DENO_ENABLED: ${{ github.event_name == 'workflow_dispatch' && inputs.deploy_deno_enabled || vars.DEPLOY_DENO_ENABLED || 'true' }} + SKIP_BOT_EVENTS: ${{ github.event_name == 'workflow_dispatch' && inputs.skip_bot_events || vars.SKIP_BOT_EVENTS || 'false' }} + EXCLUDE_SUPPORTED_EVENTS: ${{ github.event_name == 'workflow_dispatch' && inputs.exclude_supported_events || vars.EXCLUDE_SUPPORTED_EVENTS || '' }} steps: - - uses: ubiquity-os/action-deploy-plugin@main + - name: Resolve source ref + id: refs + shell: bash + run: | + source_ref="$(echo '${{ github.event.ref || github.event.workflow_run.head_branch || github.ref }}' | sed 's#refs/heads/##' | sed 's#refs/tags/##')" + if [[ -z "$source_ref" ]]; then + source_ref="${GITHUB_REF_NAME}" + fi + + is_tag_ref="false" + if [[ "${{ github.event.ref_type || '' }}" == "tag" ]] || [[ "${GITHUB_REF:-}" == refs/tags/* ]]; then + is_tag_ref="true" + fi + + is_artifact_ref="false" + if [[ "$source_ref" == dist/* ]]; then + is_artifact_ref="true" + fi + + if [[ "$BUILD_ACTION_ENABLED" == "true" ]]; then + action_ref_branch="dist/${source_ref}" + else + action_ref_branch="${source_ref}" + fi + + echo "source_ref=$source_ref" >> "$GITHUB_OUTPUT" + echo "is_tag_ref=$is_tag_ref" >> "$GITHUB_OUTPUT" + echo "is_artifact_ref=$is_artifact_ref" >> "$GITHUB_OUTPUT" + echo "ACTION_REF=${GITHUB_REPOSITORY}@${action_ref_branch}" >> "$GITHUB_ENV" + + - name: Print deployment mode + shell: bash + run: | + echo "BUILD_ACTION_ENABLED=${BUILD_ACTION_ENABLED}" + echo "DEPLOY_DENO_ENABLED=${DEPLOY_DENO_ENABLED}" + echo "SKIP_BOT_EVENTS=${SKIP_BOT_EVENTS}" + echo "EXCLUDE_SUPPORTED_EVENTS=${EXCLUDE_SUPPORTED_EVENTS}" + echo "SOURCE_REF=${{ steps.refs.outputs.source_ref }}" + echo "IS_ARTIFACT_REF=${{ steps.refs.outputs.is_artifact_ref }}" + echo "IS_TAG_REF=${{ steps.refs.outputs.is_tag_ref }}" + echo "ACTION_REF=${ACTION_REF}" + + - name: Publish action artifact branch + if: ${{ env.BUILD_ACTION_ENABLED == 'true' }} + uses: ubiquity-os/action-deploy-plugin@main with: action: ${{ github.event_name == 'delete' && 'delete' || 'publish' }} treatAsEsm: true - sourcemap: false + sourcemap: true pluginEntry: "${{ github.workspace }}/src/action.ts" - skipBotEvents: false + excludeSupportedEvents: ${{ env.EXCLUDE_SUPPORTED_EVENTS }} + skipBotEvents: ${{ env.SKIP_BOT_EVENTS }} env: APP_ID: ${{ secrets.APP_ID }} APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} + + - name: Check out the repository + if: ${{ env.DEPLOY_DENO_ENABLED == 'true' && steps.refs.outputs.is_tag_ref != 'true' && steps.refs.outputs.is_artifact_ref != 'true' }} + uses: actions/checkout@v6 + + - name: Set up Deno + if: ${{ env.DEPLOY_DENO_ENABLED == 'true' && steps.refs.outputs.is_tag_ref != 'true' && steps.refs.outputs.is_artifact_ref != 'true' }} + uses: denoland/setup-deno@v2 + with: + deno-version: v2.x + + - name: Install dependencies + if: ${{ env.DEPLOY_DENO_ENABLED == 'true' && steps.refs.outputs.is_tag_ref != 'true' && steps.refs.outputs.is_artifact_ref != 'true' && github.event_name != 'delete' }} + run: deno install --node-modules-dir=auto --no-lock + + - name: Generate manifest for deploy + if: ${{ env.DEPLOY_DENO_ENABLED == 'true' && steps.refs.outputs.is_tag_ref != 'true' && steps.refs.outputs.is_artifact_ref != 'true' && github.event_name != 'delete' }} + run: deno x -A -y --no-lock npm:@ubiquity-os/plugin-manifest-tool@latest + env: + SKIP_BOT_EVENTS: ${{ env.SKIP_BOT_EVENTS }} + EXCLUDE_SUPPORTED_EVENTS: ${{ env.EXCLUDE_SUPPORTED_EVENTS }} + GITHUB_REF_NAME: ${{ steps.refs.outputs.source_ref }} + + - uses: ubiquity-os/deno-plugin-adapter@main + if: ${{ env.DEPLOY_DENO_ENABLED == 'true' && steps.refs.outputs.is_tag_ref != 'true' && steps.refs.outputs.is_artifact_ref != 'true' && github.event_name != 'delete' }} + id: adapter + with: + pluginEntry: "./worker" + + - uses: ubiquity-os/deno-deploy@main + if: ${{ env.DEPLOY_DENO_ENABLED == 'true' && steps.refs.outputs.is_tag_ref != 'true' && steps.refs.outputs.is_artifact_ref != 'true' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + KERNEL_PUBLIC_KEY: ${{ secrets.KERNEL_PUBLIC_KEY }} + APP_ID: ${{ secrets.APP_ID }} + APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} + APP_INSTALLATION_ID: ${{ secrets.APP_INSTALLATION_ID }} + ACTION_REF: ${{ env.ACTION_REF }} + LOG_LEVEL: ${{ secrets.LOG_LEVEL }} + USER_GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }} + with: + token: ${{ secrets.DENO_DEPLOY_TOKEN }} + action: ${{ github.event_name == 'delete' && 'delete' || 'deploy' }} + organization: ${{ secrets.DENO_ORG_NAME }} + entrypoint: ${{ github.event_name == 'delete' && 'src/deno.ts' || steps.adapter.outputs.entrypoint }} + project_name: ${{ secrets.DENO_PROJECT_NAME }} + sourceRef: ${{ steps.refs.outputs.source_ref }} + artifactPrefix: dist/ diff --git a/.github/workflows/worker-delete.yml b/.github/workflows/worker-delete.yml deleted file mode 100644 index 2a9b4b5..0000000 --- a/.github/workflows/worker-delete.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: Delete Deployment - -on: - delete: - -jobs: - delete: - runs-on: ubuntu-latest - name: Delete Deployment - environment: ${{ (github.ref == 'refs/heads/main' || github.event.workflow_run.head_branch == 'main') && 'main' || 'development' }} - steps: - - name: Setup Bun - uses: oven-sh/setup-bun@v2 - - - uses: actions/checkout@v4 - - - name: Get Deleted Branch Name - id: get_branch - run: | - branch_name=$(echo '${{ github.event.workflow_run.head_branch || 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 | head -n 1 | 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 deleted file mode 100644 index 5f17bf6..0000000 --- a/.github/workflows/worker-deploy.yml +++ /dev/null @@ -1,107 +0,0 @@ -name: Deploy Worker - -on: - workflow_dispatch: - workflow_run: - # We run this workflow after the configuration is updated to avoid conflicts - # while updating the manifest.json file - 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' || github.event.workflow_run.head_branch == 'main') && 'main' || 'development' }} - permissions: - contents: write - - steps: - - name: Setup Bun - uses: oven-sh/setup-bun@v2 - - - uses: actions/checkout@v4 - with: - ref: ${{ github.event.workflow_run.head_branch || github.ref }} - - - name: Update wrangler.toml Name Field - run: | - branch_name=$(echo '${{ github.event.workflow_run.head_branch || 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 | head -n 1 | 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 '0,/^name = .*/{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.79.0" - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - secrets: | - USER_GITHUB_TOKEN - CLOUDFLARE_ACCOUNT_ID - ${{ secrets.KERNEL_PUBLIC_KEY && secrets.KERNEL_PUBLIC_KEY != '' && 'KERNEL_PUBLIC_KEY' || '' }} - ${{ secrets.LOG_LEVEL && secrets.LOG_LEVEL != '' && 'LOG_LEVEL' || '' }} - - env: - USER_GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }} - KERNEL_PUBLIC_KEY: ${{ secrets.KERNEL_PUBLIC_KEY }} - CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - LOG_LEVEL: ${{ secrets.LOG_LEVEL }} - - - 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 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.event.workflow_run.head_branch || 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