-
Notifications
You must be signed in to change notification settings - Fork 572
224 lines (192 loc) · 7.11 KB
/
auto-translate.yaml
File metadata and controls
224 lines (192 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
name: Auto Translate
on:
push:
branches:
- main
workflow_dispatch: {}
concurrency:
group: auto-translate
cancel-in-progress: false
permissions:
id-token: write
contents: write
pull-requests: write
issues: write
jobs:
# Setup phase - prepare the translation branch with latest content
setup-branch:
name: setup-translation-branch
runs-on: ubuntu-latest
outputs:
branch-exists: ${{ steps.check-branch.outputs.exists }}
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Check if translation branch exists
id: check-branch
run: |
if git ls-remote --exit-code --heads origin automated-translations; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Setup and rebase translation branch
run: |
if [ "${{ steps.check-branch.outputs.exists }}" = "true" ]; then
echo "Branch exists, checking out and rebasing on main"
git checkout -b automated-translations origin/automated-translations
# Rebase on main to get latest content
git rebase origin/main
# Push the rebased branch
git push --force-with-lease origin automated-translations
else
echo "Branch doesn't exist, creating new branch from main"
git checkout -b automated-translations
git push origin automated-translations
fi
# Fork phase - parallel translation jobs (now working from rebased content)
translate-matrix:
name: translate-${{ matrix.language }}
runs-on: ubuntu-latest
needs: setup-branch
strategy:
matrix:
language: [ja] # Add more languages here as needed
steps:
- name: Check out rebased translation branch
uses: actions/checkout@v4
with:
ref: automated-translations
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Get AWS credentials
uses: aws-actions/configure-aws-credentials@v4.2.1
with:
role-to-assume: ${{ secrets.INFERENCE_AWS_ROLE_ARN }}
role-duration-seconds: 1800
aws-region: ${{ secrets.AWS_REGION }}
role-session-name: GithubActionsSession-translate-${{ matrix.language }}
- name: Install dependencies
run: yarn install
- name: Run translate for ${{ matrix.language }}
run: |
yarn toolkit-md translate \
--translation-dir website/i18n/${{ matrix.language }}/docusaurus-plugin-content-docs/current \
--to ${{ matrix.language }} --skip-file-suffix \
--write .
- name: Upload translation artifacts
uses: actions/upload-artifact@v4
with:
name: translations-${{ matrix.language }}
path: website/i18n/${{ matrix.language }}/
retention-days: 1
# Join phase - collect all translations and create PR
collect-and-pr:
name: collect-translations-and-pr
runs-on: ubuntu-latest
needs: [setup-branch, translate-matrix]
steps:
- name: Check out translation branch
uses: actions/checkout@v4
with:
ref: automated-translations
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Download all translation artifacts
uses: actions/download-artifact@v4
with:
path: artifacts/
- name: Apply all translations
run: |
# Copy all translation files from artifacts
for lang_dir in artifacts/translations-*/; do
if [ -d "$lang_dir" ]; then
lang=$(basename "$lang_dir" | sed 's/translations-//')
echo "Applying translations for language: $lang"
# Create target directory if it doesn't exist
mkdir -p "website/i18n/$lang"
# Copy translation files
cp -r "$lang_dir"* "website/i18n/$lang/"
fi
done
- name: Commit changes
run: |
git add website/i18n/
if git diff --staged --quiet; then
echo "No translation changes to commit"
echo "has_changes=false" >> $GITHUB_ENV
else
git commit -m "Automated translation updates
- Updated translations for: $(ls artifacts/ | sed 's/translations-//g' | tr '\n' ' ')
- Generated from commit: ${{ github.sha }}
- Workflow run: ${{ github.run_id }}"
echo "has_changes=true" >> $GITHUB_ENV
fi
- name: Push translation branch
if: env.has_changes == 'true'
run: |
git push origin automated-translations
- name: Create or Update Pull Request
if: env.has_changes == 'true'
uses: actions/github-script@v8
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const head = 'automated-translations';
const base = 'main';
const body = `## Automated Translation Updates
This PR contains automated translation updates generated from the latest changes in \`main\`.
### Changes
- Branch was rebased on latest \`main\` before translation
- Translations generated from latest content
- Previous translation work preserved through rebase
- Generated from commit: ${context.sha}
- Workflow run: ${context.runId}
Please review the translations before merging.`;
// Check for existing open PR
const { data: pulls } = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${head}`,
base,
state: 'open',
});
if (pulls.length > 0) {
const prNumber = pulls[0].number;
console.log(`PR #${prNumber} already exists, updating body`);
await github.rest.pulls.update({
owner,
repo,
pull_number: prNumber,
body,
});
} else {
console.log('Creating new PR');
const { data: pr } = await github.rest.pulls.create({
owner,
repo,
title: 'chore: Automated translation updates',
head,
base,
body,
});
// Add label after creation
await github.rest.issues.addLabels({
owner,
repo,
issue_number: pr.number,
labels: ['content/other'],
});
console.log(`Created PR #${pr.number}`);
}