|
| 1 | +# Reusable CI/CD workflow for Cloudflare Worker services |
| 2 | +# Called by individual service repos |
| 3 | +name: Worker Service CI/CD |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_call: |
| 7 | + inputs: |
| 8 | + service_name: |
| 9 | + description: "Name of the service (e.g., core-auth-service)" |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + environment: |
| 13 | + description: "Deployment environment (dev or production)" |
| 14 | + required: false |
| 15 | + type: string |
| 16 | + default: "dev" |
| 17 | + run_lint: |
| 18 | + description: "Whether to run linting" |
| 19 | + required: false |
| 20 | + type: boolean |
| 21 | + default: true |
| 22 | + run_typecheck: |
| 23 | + description: "Whether to run typecheck" |
| 24 | + required: false |
| 25 | + type: boolean |
| 26 | + default: true |
| 27 | + node_version: |
| 28 | + description: "Node.js version" |
| 29 | + required: false |
| 30 | + type: string |
| 31 | + default: "22" |
| 32 | + bun_version: |
| 33 | + description: "Bun version" |
| 34 | + required: false |
| 35 | + type: string |
| 36 | + default: "latest" |
| 37 | + skip_deploy: |
| 38 | + description: "Skip deployment (CI only)" |
| 39 | + required: false |
| 40 | + type: boolean |
| 41 | + default: false |
| 42 | + secrets: |
| 43 | + CLOUDFLARE_API_TOKEN: |
| 44 | + required: false |
| 45 | + |
| 46 | +jobs: |
| 47 | + lint: |
| 48 | + name: Lint & Typecheck |
| 49 | + runs-on: ubuntu-latest |
| 50 | + if: inputs.run_lint || inputs.run_typecheck |
| 51 | + steps: |
| 52 | + - name: Checkout |
| 53 | + uses: actions/checkout@v4 |
| 54 | + |
| 55 | + - name: Setup Bun |
| 56 | + uses: oven-sh/setup-bun@v2 |
| 57 | + with: |
| 58 | + bun-version: ${{ inputs.bun_version }} |
| 59 | + |
| 60 | + - name: Install dependencies |
| 61 | + run: bun install --frozen-lockfile |
| 62 | + |
| 63 | + - name: Generate Wrangler Types |
| 64 | + if: inputs.run_typecheck |
| 65 | + run: bunx wrangler types --env-interface CloudflareBindings |
| 66 | + |
| 67 | + - name: Lint |
| 68 | + if: inputs.run_lint |
| 69 | + run: bun run lint |
| 70 | + |
| 71 | + - name: Typecheck |
| 72 | + if: inputs.run_typecheck |
| 73 | + run: bunx tsc --noEmit |
| 74 | + |
| 75 | + deploy: |
| 76 | + name: Deploy to ${{ inputs.environment }} |
| 77 | + runs-on: ubuntu-latest |
| 78 | + needs: [lint] |
| 79 | + if: | |
| 80 | + always() && |
| 81 | + !inputs.skip_deploy && |
| 82 | + (needs.lint.result == 'success' || needs.lint.result == 'skipped') && |
| 83 | + secrets.CLOUDFLARE_API_TOKEN != '' |
| 84 | + environment: ${{ inputs.environment }} |
| 85 | + steps: |
| 86 | + - name: Checkout |
| 87 | + uses: actions/checkout@v4 |
| 88 | + |
| 89 | + - name: Setup Bun |
| 90 | + uses: oven-sh/setup-bun@v2 |
| 91 | + with: |
| 92 | + bun-version: ${{ inputs.bun_version }} |
| 93 | + |
| 94 | + - name: Setup Node.js |
| 95 | + uses: actions/setup-node@v4 |
| 96 | + with: |
| 97 | + node-version: ${{ inputs.node_version }} |
| 98 | + |
| 99 | + - name: Install dependencies |
| 100 | + run: bun install --frozen-lockfile |
| 101 | + |
| 102 | + - name: Deploy to dev |
| 103 | + if: inputs.environment == 'dev' |
| 104 | + run: bunx wrangler deploy --env dev --minify |
| 105 | + env: |
| 106 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 107 | + CLOUDFLARE_ACCOUNT_ID: 8f0203259905d8923687286c84921e6c |
| 108 | + |
| 109 | + - name: Deploy to production |
| 110 | + if: inputs.environment == 'production' |
| 111 | + run: bunx wrangler deploy --minify |
| 112 | + env: |
| 113 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 114 | + CLOUDFLARE_ACCOUNT_ID: 8f0203259905d8923687286c84921e6c |
| 115 | + |
| 116 | + - name: Deployment summary |
| 117 | + run: | |
| 118 | + echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY |
| 119 | + echo "- **Service:** ${{ inputs.service_name }}" >> $GITHUB_STEP_SUMMARY |
| 120 | + echo "- **Environment:** ${{ inputs.environment }}" >> $GITHUB_STEP_SUMMARY |
| 121 | + echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY |
| 122 | + echo "- **Triggered by:** ${{ github.actor }}" >> $GITHUB_STEP_SUMMARY |
0 commit comments