Skip to content

Commit e60cc8d

Browse files
authored
optimize build workflow
1 parent c00d997 commit e60cc8d

File tree

1 file changed

+39
-64
lines changed

1 file changed

+39
-64
lines changed

.github/workflows/main.yml

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,95 +21,70 @@ jobs:
2121
steps:
2222
- name: Checkout repository
2323
uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 1
2426

25-
- name: Setup Node.js
27+
- name: Setup Node.js & pnpm
2628
uses: actions/setup-node@v3
2729
with:
2830
node-version: "lts/*"
2931

30-
- name: Install dependencies
31-
run: npm i -g oxlint oxfmt esbuild html-minifier-terser
32+
- name: Install pnpm
33+
run: corepack enable pnpm
3234

33-
- name: Beautify code
34-
run: |
35-
npm run fix || true
36-
npx oxfmt --write . "**/*.{js,md,html,css,yml}"
35+
- name: Cache pnpm global packages
36+
uses: actions/cache@v4
37+
with:
38+
path: |
39+
~/.local/share/pnpm/global
40+
~/.local/share/pnpm/store
41+
key: pnpm-global-${{ hashFiles('.github/workflows/deploy.yml') }}
42+
restore-keys: pnpm-global-
3743

38-
- name: Create v.html with commit hash
44+
- name: Install dependencies
45+
run: pnpm add -g oxlint oxfmt esbuild html-minifier-terser
46+
47+
- name: Lint, format & version stamp
3948
run: |
49+
pnpm run fix || true
50+
pnpm oxfmt --write . "**/*.{js,md,html,css,yml}"
4051
echo "${{ github.sha }}" > game/images/Zombies/CX/v.html
4152
42-
- name: Get commit message & author email
43-
id: get_commit_message
53+
- name: Commit changes
4454
run: |
4555
COMMIT_MSG=$(git log -1 --pretty=format:%s)
4656
AUTHOR_EMAIL=$(git log -1 --pretty=format:%ae)
47-
echo "message=$COMMIT_MSG" >> $GITHUB_OUTPUT
48-
echo "author_email=$AUTHOR_EMAIL" >> $GITHUB_OUTPUT
49-
50-
- name: Commit changes
51-
run: |
5257
git config user.name github-actions
5358
git config user.email github-actions@github.com
5459
git add .
55-
git commit -m "Format \"${{ steps.get_commit_message.outputs.message }}\"
60+
git commit -m "Format \"$COMMIT_MSG\"
5661
Original commit: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
5762
58-
Co-authored-by: ${{ github.actor }} <${{ steps.get_commit_message.outputs.author_email }}>" || exit 0
63+
Co-authored-by: ${{ github.actor }} <$AUTHOR_EMAIL>" || exit 0
5964
git push
6065
61-
- name: Bundle CPlants.js
66+
- name: Bundle & minify all assets
6267
run: |
6368
esbuild game/js/CPlants.js \
64-
--bundle \
65-
--minify \
66-
--sourcemap \
67-
--sources-content=true \
68-
--outfile=game/js/CPlants.js \
69-
--allow-overwrite
70-
71-
- name: Minify JavaScript & CSS files
72-
run: |
73-
esbuild $(find . -type f \( -name "*.js" -o -name "*.css" \) -not -path "./node_modules/*") \
74-
--minify \
75-
--sourcemap \
76-
--sources-content=true \
77-
--outdir=. \
78-
--allow-overwrite
79-
80-
- name: Minify HTML files
81-
run: |
82-
find . -type f -name "*.html" -not -path "./node_modules/*" | while read file; do
83-
html-minifier-terser \
84-
--collapse-whitespace \
85-
--remove-comments \
86-
--remove-tag-whitespace \
87-
--minify-css true \
88-
--minify-js true \
89-
-o "$file" \
90-
"$file"
91-
done
92-
93-
- name: Deploy to Cloudflare Pages (Direct Upload)
69+
--bundle --minify --sourcemap --sources-content=true \
70+
--outfile=game/js/CPlants.js --allow-overwrite
71+
72+
esbuild $(find . -type f \( -name "*.js" -o -name "*.css" \) \
73+
-not -path "./node_modules/*" -not -name "CPlants.js") \
74+
--minify --sourcemap --sources-content=true \
75+
--outdir=. --allow-overwrite &
76+
77+
find . -type f -name "*.html" -not -path "./node_modules/*" | \
78+
xargs -P4 -I{} html-minifier-terser \
79+
--collapse-whitespace --remove-comments --remove-tag-whitespace \
80+
--minify-css true --minify-js true -o {} {} &
81+
82+
wait
83+
84+
- name: Deploy to Cloudflare Pages
9485
uses: cloudflare/wrangler-action@v3
9586
with:
9687
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
9788
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
9889
command: pages deploy . --project-name=pvz
9990
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
100-
101-
# discord-notify:
102-
# runs-on: ubuntu-latest
103-
# needs: build-and-deploy
104-
# if: always()
105-
# steps:
106-
# - name: Notify Discord
107-
# uses: sarisia/actions-status-discord@v1
108-
# if: always()
109-
# with:
110-
# webhook: ${{ secrets.DISCORD_WEBHOOK }}
111-
# ack_no_webhook: true
112-
# status: ${{ (contains(needs.*.result, 'failure') || contains(needs.*.result, 'skipped')) && 'Failure' || contains(needs.*.result, 'cancelled') && 'Cancelled' || 'Success' }}
113-
# title: ${{ github.workflow }}
114-
# description: ${{ github.repository }} @ ${{ github.ref_name }}\n${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}
115-
# url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

0 commit comments

Comments
 (0)