11name : Auto Translate Docs
22
33on :
4+ workflow_run :
5+ workflows : ["Process Documentation"]
6+ types :
7+ - completed
8+ branches-ignore :
9+ - ' main'
410 push :
511 branches-ignore :
612 - ' main'
13+ paths-ignore :
14+ - ' .github/workflows/**'
715
816jobs :
917 translate :
1018 runs-on : ubuntu-latest
19+ # Only run if the workflow_run event was successful, or if it's a direct push
20+ if : github.event_name == 'push' || github.event.workflow_run.conclusion == 'success'
1121 permissions :
1222 contents : write
1323 steps :
1626 with :
1727 fetch-depth : 0 # Fetches all history for git diff
1828 token : ${{ secrets.GITHUB_TOKEN }}
29+ # For workflow_run events, checkout the head of the triggering workflow
30+ ref : ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }}
1931
2032 - name : Set up Python
2133 uses : actions/setup-python@v4
@@ -28,16 +40,57 @@ jobs:
2840 - name : Get changed markdown files
2941 id : changed-files
3042 run : |
31- # Get the list of changed files between the current and previous commit
43+ # Get the list of newly added files between the current and previous commit
3244 # We filter for .md and .mdx files that are inside the language directories
33- files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -E '^(en|zh-hans|ja-jp|plugin-dev-en|plugin-dev-zh|plugin-dev-ja)/.*(\.md|\.mdx)$' || true)
34- if [[ -z "$files" ]]; then
35- echo "No markdown files to translate."
45+ # Only include added (A) files, skip modified (M) and deleted (D) files
46+
47+ # Determine the commit SHA to use based on event type
48+ if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
49+ current_sha="${{ github.event.workflow_run.head_sha }}"
50+ echo "Using workflow_run head_sha: $current_sha"
51+ else
52+ current_sha="${{ github.sha }}"
53+ echo "Using github.sha: $current_sha"
54+ fi
55+
56+ # Try different approaches to get the diff
57+ if [[ -n "${{ github.event.before }}" && "${{ github.event_name }}" == "push" ]]; then
58+ echo "Using github.event.before: ${{ github.event.before }}"
59+ files=$(git diff --name-status ${{ github.event.before }} $current_sha | grep -E '^A\s+' | cut -f2 | grep -E '^(en|en-us|zh-hans|ja-jp|plugin-dev-en|plugin-dev-zh|plugin-dev-ja|versions)/.*(\.md|\.mdx)$' || true)
60+ else
61+ echo "Using HEAD~1 for comparison"
62+ files=$(git diff --name-status HEAD~1 $current_sha | grep -E '^A\s+' | cut -f2 | grep -E '^(en|en-us|zh-hans|ja-jp|plugin-dev-en|plugin-dev-zh|plugin-dev-ja|versions)/.*(\.md|\.mdx)$' || true)
63+ fi
64+
65+ echo "Detected files (Added only):"
66+ echo "$files"
67+
68+ # Filter out files that don't actually exist
69+ existing_files=""
70+ if [[ -n "$files" ]]; then
71+ while IFS= read -r file; do
72+ if [[ -n "$file" && -f "$file" ]]; then
73+ if [[ -z "$existing_files" ]]; then
74+ existing_files="$file"
75+ else
76+ existing_files="$existing_files"$'\n'"$file"
77+ fi
78+ else
79+ echo "Skipping non-existent file: $file"
80+ fi
81+ done <<< "$files"
82+ fi
83+
84+ echo "Final files to translate:"
85+ echo "$existing_files"
86+
87+ if [[ -z "$existing_files" ]]; then
88+ echo "No new markdown files to translate."
3689 echo "files=" >> $GITHUB_OUTPUT
3790 else
3891 # The script expects absolute paths, but we run it from the root, so relative is fine.
3992 echo "files<<EOF" >> $GITHUB_OUTPUT
40- echo "$files " >> $GITHUB_OUTPUT
93+ echo "$existing_files " >> $GITHUB_OUTPUT
4194 echo "EOF" >> $GITHUB_OUTPUT
4295 fi
4396
@@ -49,12 +102,35 @@ jobs:
49102 echo "Files to translate:"
50103 echo "${{ steps.changed-files.outputs.files }}"
51104
52- echo "${{ steps.changed-files.outputs.files }}" | while IFS= read -r file; do
105+ # Create temporary file list
106+ echo "${{ steps.changed-files.outputs.files }}" > /tmp/files_to_translate.txt
107+
108+ # Start all translation processes in parallel
109+ pids=()
110+ while IFS= read -r file; do
53111 if [[ -n "$file" ]]; then
54- echo "Translating $file..."
55- python tools/translate/main.py "$file" "$DIFY_API_KEY"
112+ echo "Starting translation for $file..."
113+ python tools/translate/main.py "$file" "$DIFY_API_KEY" &
114+ pids+=($!)
115+ fi
116+ done < /tmp/files_to_translate.txt
117+
118+ # Wait for all background processes to complete
119+ echo "Waiting for ${#pids[@]} translation processes to complete..."
120+ failed=0
121+ for pid in "${pids[@]}"; do
122+ if ! wait "$pid"; then
123+ echo "Translation process $pid failed"
124+ failed=1
56125 fi
57126 done
127+
128+ if [ $failed -eq 1 ]; then
129+ echo "Some translations failed"
130+ exit 1
131+ fi
132+
133+ echo "All translations completed successfully"
58134
59135 - name : Commit and push changes
60136 run : |
@@ -64,8 +140,17 @@ jobs:
64140 if [[ -n $(git status --porcelain) ]]; then
65141 git add .
66142 git commit -m "docs: auto-translate documentation"
67- # Push to the same branch the workflow was triggered from
68- git push origin HEAD:${{ github.ref_name }}
143+ # Push to the appropriate branch based on event type
144+ if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
145+ # For workflow_run events, push to the head branch of the triggering workflow
146+ branch_ref="${{ github.event.workflow_run.head_branch }}"
147+ echo "Pushing to workflow_run head branch: $branch_ref"
148+ git push origin HEAD:$branch_ref
149+ else
150+ # For push events, push to the same branch the workflow was triggered from
151+ echo "Pushing to current branch: ${{ github.ref_name }}"
152+ git push origin HEAD:${{ github.ref_name }}
153+ fi
69154 echo "Translated files have been pushed to the branch."
70155 else
71156 echo "No new translations to commit."
0 commit comments