Skip to content

Commit 31deb86

Browse files
committed
chore: streamline GitHub Actions workflow for client generation
- Simplified event types for pull requests and push triggers in the workflow configuration. - Updated Git user configuration to use global settings for better consistency. - Enhanced change detection logic before committing and pushing generated client files. - Removed unnecessary comments and improved overall readability of the workflow file.
1 parent 03b230e commit 31deb86

File tree

1 file changed

+18
-49
lines changed

1 file changed

+18
-49
lines changed

.github/workflows/generate-client.yml

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,28 @@ name: Generate Client
33

44
on:
55
pull_request:
6-
types:
7-
- opened
8-
- synchronize
9-
push: # Also consider running on push to main/main to ensure consistency
10-
branches:
11-
- main # Or your default branch
6+
types: [opened, synchronize]
7+
push:
8+
branches: [main]
129

1310
permissions:
14-
contents: write # Needed for pushing commits on same-repo events
11+
contents: write
1512

1613
jobs:
1714
generate-client:
1815
runs-on: ubuntu-latest
16+
1917
steps:
20-
# 1. Checkout Code - Single step handles both fork and same-repo PRs/pushes
2118
- name: Checkout Code
2219
uses: actions/checkout@v4
23-
# Fetch depth 0 is needed for accurate diff/commit history if required elsewhere
24-
# For pushing back, fetch the specific ref for same-repo PRs
2520
with:
2621
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository && github.head_ref || '' }}
27-
# Use a PAT for same-repo events if you need to trigger other workflows
28-
# Standard GITHUB_TOKEN is often sufficient for basic pushes
29-
# token: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && secrets.YOUR_PAT_OR_GITHUB_TOKEN || '' }}
3022

31-
# 2. Setup Environment
3223
- name: Setup pnpm
3324
uses: pnpm/action-setup@v3
3425
with:
3526
version: 9.9.0
36-
27+
3728
- name: Setup Node.js
3829
uses: actions/setup-node@v4
3930
with:
@@ -46,34 +37,27 @@ jobs:
4637
with:
4738
python-version: "3.10"
4839

49-
- name: Install uv (Python Package Installer)
50-
uses: astral-sh/setup-uv@v1 # Use v1 for stable release
40+
- name: Install uv
41+
uses: astral-sh/setup-uv@v1
5142
with:
52-
# version: "0.4.15" # Specifying version is optional, can use latest stable
5343
enable-cache: true
5444

55-
# 3. Install Dependencies
5645
- name: Install Frontend Dependencies
5746
run: pnpm install
5847
working-directory: frontend
59-
48+
6049
- name: Install Admin Dependencies
6150
run: pnpm install
6251
working-directory: admin
6352

64-
# 安装所有后端依赖项
6553
- name: Install Backend Dependencies
6654
run: |
6755
uv sync
6856
source .venv/bin/activate
6957
pip install .
7058
working-directory: backend
7159

72-
# 4. Run Generation Script with Error Handling
7360
- name: Generate Client
74-
# Use `uv run` to execute within the backend context if needed,
75-
# or just run the script directly if VIRTUAL_ENV isn't strictly necessary
76-
# for the script itself (it might just need python)
7761
run: |
7862
make generate-client || {
7963
echo "❌ Failed to run generate-client.sh script."
@@ -87,54 +71,39 @@ jobs:
8771
SENTRY_DSN: ""
8872
POSTHOG_API_KEY: ""
8973
POSTHOG_HOST: ""
90-
# VIRTUAL_ENV might not be needed if uv sync --system is used
9174
VIRTUAL_ENV: .venv
9275

93-
# 5. Configure Git User
9476
- name: Configure Git User
9577
run: |
96-
git config --local user.email "github-actions[bot]@users.noreply.github.com"
97-
git config --local user.name "github-actions[bot]"
78+
git config --global user.email "github-actions@github.com"
79+
git config --global user.name "GitHub Actions"
9880
99-
# 6. Stage Generated Files
100-
- name: Stage Generated Client Files
81+
- name: Stage Generated Files
10182
run: |
10283
git add frontend/app/openapi-client
10384
git add admin/src/client
10485
105-
# 7. Handle Changes for Same-Repo Events (Push/PR from same repo)
10686
- name: Commit and Push Changes (Same Repo)
107-
# Run only if it's NOT a PR from a fork
10887
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)
10988
run: |
110-
# Check if there are staged changes
111-
if ! git diff --staged --quiet; then
89+
BRANCH_NAME="${{ github.head_ref || github.ref_name }}"
90+
if ! git diff --quiet || ! git diff --staged --quiet; then
11291
echo "✅ Changes detected in generated client. Committing and pushing..."
113-
# Pull before pushing to avoid conflicts
114-
git pull --rebase origin ${{ github.head_ref || github.ref_name }}
92+
git add .
93+
git pull --rebase origin "$BRANCH_NAME" || true
11594
git commit -m "ci: ✨ Autogenerate frontend client"
116-
git push origin HEAD:${{ github.head_ref || github.ref_name }}
95+
git push origin HEAD:"$BRANCH_NAME"
11796
else
11897
echo "✅ No changes detected in generated client."
11998
fi
12099
env:
121-
# GITHUB_TOKEN has write permissions for same-repo events by default
122100
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
123-
# Use a PAT if you need to trigger subsequent workflows:
124-
# GITHUB_TOKEN: ${{ secrets.YOUR_PAT_WITH_WRITE_ACCESS }}
125101

126-
# 8. Handle Changes for Fork PRs (Warn, Don't Fail)
127102
- name: Check for Uncommitted Changes (Fork PRs)
128-
# Run only if it IS a PR from a fork
129103
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
130104
run: |
131-
# Check if there are staged changes that were generated
132105
if ! git diff --staged --quiet; then
133-
echo "⚠️ Changes detected in generated client."
134-
echo "➡️ Please run 'bash scripts/generate-client.sh' locally and commit the changes to this PR."
135-
# Optionally, use GitHub annotations for better visibility in the PR
136106
echo "::warning title=Generated Client Changes Detected::Please run 'bash scripts/generate-client.sh' locally and commit the changes to this PR."
137-
# DO NOT exit 1 - Allow the workflow to continue
138107
else
139108
echo "✅ No changes detected in generated client."
140-
fi
109+
fi

0 commit comments

Comments
 (0)