|
1 | 1 | import crowdin from "../api-client/crowdinClient"
|
2 | 2 |
|
3 |
| -async function awaitLatestBuild() { |
4 |
| - const FINISHED = "finished" |
5 |
| - const TIMEOUT = 2 * 60 * 60 * 1000 // 2 hours in milliseconds |
| 3 | +const FINISHED = "finished" |
| 4 | +const TIMEOUT = 2 * 60 * 60 * 1000 // Timeout after 2 hours |
| 5 | +const INTERVAL = 10 * 1000 // 10 seconds between checks |
6 | 6 |
|
| 7 | +async function awaitLatestBuild() { |
7 | 8 | const projectId = Number(process.env.CROWDIN_PROJECT_ID) || 363359
|
8 | 9 |
|
9 |
| - const initialResponse = await crowdin.translationsApi.listProjectBuilds( |
10 |
| - projectId |
11 |
| - ) |
| 10 | + // BUILD_ID is provided by the triggerBuild script run in the same workflow prior to this script |
| 11 | + const buildId = process.env.BUILD_ID |
12 | 12 |
|
| 13 | + console.log("Build ID provided:", buildId) |
| 14 | + const initialResponse = await crowdin.translationsApi.checkBuildStatus( |
| 15 | + projectId, |
| 16 | + Number(buildId) |
| 17 | + ) |
13 | 18 | let data = initialResponse.data
|
14 |
| - let isFinished = data[0].data.status === FINISHED |
| 19 | + |
| 20 | + let isFinished = data.status === FINISHED |
15 | 21 |
|
16 | 22 | const timeoutTime = Date.now() + TIMEOUT
|
| 23 | + let tryAgainTime = Date.now() - 1 |
17 | 24 | while (!isFinished && Date.now() < timeoutTime) {
|
18 |
| - const repeatCheck = await crowdin.translationsApi.listProjectBuilds( |
19 |
| - projectId |
| 25 | + if (Date.now() < tryAgainTime) continue |
| 26 | + tryAgainTime = Date.now() + INTERVAL |
| 27 | + |
| 28 | + const repeatCheck = await crowdin.translationsApi.checkBuildStatus( |
| 29 | + projectId, |
| 30 | + Number(buildId) |
20 | 31 | )
|
21 | 32 | data = repeatCheck.data
|
22 |
| - isFinished = data[0].data.status === FINISHED |
| 33 | + isFinished = data.status === FINISHED |
| 34 | + console.log( |
| 35 | + `id: ${buildId}, status: ${data.status}, progress ${data.progress}` |
| 36 | + ) |
23 | 37 | }
|
24 |
| - const latestBuildData = data[0].data |
25 |
| - if (latestBuildData.status !== FINISHED) |
| 38 | + |
| 39 | + if (data.status !== FINISHED) { |
| 40 | + console.error(`::set-output name=buildSuccess::false`) |
26 | 41 | throw new Error(
|
27 | 42 | `Timeout: Build did not finish in ${TIMEOUT / 1000 / 60} minutes`
|
28 | 43 | )
|
| 44 | + } |
| 45 | + |
| 46 | + console.log("Latest build data:", data) |
| 47 | + console.error(`::set-output name=buildSuccess::true`) |
29 | 48 | }
|
30 | 49 |
|
31 | 50 | awaitLatestBuild()
|
|
0 commit comments