|
1 |
| -name: Deploy Worker |
| 1 | +name: Deploy |
| 2 | + |
2 | 3 | on:
|
3 | 4 | push:
|
4 | 5 | branches:
|
5 |
| - - main |
| 6 | + - "*" |
| 7 | + |
6 | 8 | jobs:
|
7 | 9 | deploy:
|
8 | 10 | runs-on: ubuntu-latest
|
9 |
| - timeout-minutes: 60 |
10 |
| - needs: test |
| 11 | + name: Deploy |
11 | 12 | steps:
|
12 | 13 | - uses: actions/checkout@v4
|
13 |
| - - name: Build & Deploy Worker |
| 14 | + |
| 15 | + - name: Setup Node.js |
| 16 | + uses: actions/setup-node@v4 |
| 17 | + with: |
| 18 | + node-version: "20" |
| 19 | + |
| 20 | + - name: Install Dependencies |
| 21 | + run: npm install |
| 22 | + |
| 23 | + - name: Check wrangler.toml for environments |
| 24 | + id: check-env |
| 25 | + run: | |
| 26 | + BRANCH_NAME=${GITHUB_REF#refs/heads/} |
| 27 | + if [ ! -f "wrangler.toml" ]; then |
| 28 | + echo "env_flag=" >> $GITHUB_OUTPUT |
| 29 | + exit 0 |
| 30 | + fi |
| 31 | + if grep -q "\\[env\\.${BRANCH_NAME}\\]" wrangler.toml; then |
| 32 | + echo "env_flag=--env ${BRANCH_NAME}" >> $GITHUB_OUTPUT |
| 33 | + else |
| 34 | + echo "env_flag=" >> $GITHUB_OUTPUT |
| 35 | + fi |
| 36 | +
|
| 37 | + - name: Install wrangler |
| 38 | + run: npm install -g wrangler |
| 39 | + |
| 40 | + - name: Sync All Secrets to Cloudflare |
| 41 | + env: |
| 42 | + SECRETS_CONTEXT: ${{ toJSON(secrets) }} |
| 43 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 44 | + run: | |
| 45 | + # Create a temporary file with secret names and values |
| 46 | + echo "$SECRETS_CONTEXT" | jq -r 'to_entries | .[] | select(.key | startswith("github_") | not) | select(.key != "CLOUDFLARE_API_TOKEN") | [.key, .value] | @tsv' > secrets.txt |
| 47 | +
|
| 48 | + # Function to set a secret |
| 49 | + set_secret() { |
| 50 | + secret_name="$1" |
| 51 | + secret_value="$2" |
| 52 | + if [ -n "${{ steps.check-env.outputs.env_flag }}" ]; then |
| 53 | + echo "Setting $secret_name for environment ${{ steps.check-env.outputs.env_flag }}" |
| 54 | + echo "$secret_value" | wrangler secret put "$secret_name" ${{ steps.check-env.outputs.env_flag }} |
| 55 | + else |
| 56 | + echo "Setting $secret_name for default environment" |
| 57 | + echo "$secret_value" | wrangler secret put "$secret_name" |
| 58 | + fi |
| 59 | + } |
| 60 | + export -f set_secret |
| 61 | +
|
| 62 | + # Use parallel to set secrets (2 arguments per job) |
| 63 | + parallel --colsep '\t' set_secret {1} {2} :::: secrets.txt |
| 64 | +
|
| 65 | + # Clean up |
| 66 | + rm secrets.txt |
| 67 | +
|
| 68 | + - name: Deploy |
14 | 69 | uses: cloudflare/wrangler-action@v3
|
15 | 70 | with:
|
16 | 71 | apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
17 | 72 | accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
| 73 | + command: deploy ${{ steps.check-env.outputs.env_flag }} |
0 commit comments