Skip to content

Commit 5eda7e9

Browse files
committed
improvements
1 parent 3c4a248 commit 5eda7e9

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

.dev.vars.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CLOUDFLARE_API_TOKEN=

.github/workflows/wrangler.yaml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- "*"
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
name: Deploy
12+
steps:
13+
- uses: actions/checkout@v4
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
69+
uses: cloudflare/wrangler-action@v3
70+
with:
71+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
72+
command: deploy ${{ steps.check-env.outputs.env_flag }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.wrangler
3-
.DS_Store
3+
.DS_Store
4+
.dev.vars

set-secrets.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Export local vars to GitHub secrets
2+
cat .dev.vars | while read line; do
3+
KEY=$(echo $line | cut -d'=' -f1)
4+
VALUE=$(echo $line | cut -d'=' -f2)
5+
gh secret set "$KEY" -b"$VALUE"
6+
done

0 commit comments

Comments
 (0)