Skip to content

Commit 2ff7f6c

Browse files
authored
Merge pull request #965 from git-for-windows/minimal-flavor-from-ci-artifacts
Get the `minimal` flavor from the `ci-artifacts` GitHub Release
2 parents 1e953bc + 8bfbac8 commit 2ff7f6c

File tree

4 files changed

+253
-9
lines changed

4 files changed

+253
-9
lines changed

dist/index.js

+132-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getViaGit,
99
gitForWindowsUsrBinPath
1010
} from './src/git'
11+
import {getViaCIArtifacts} from './src/ci_artifacts'
1112
import * as fs from 'fs'
1213

1314
const flavor = core.getInput('flavor')
@@ -44,11 +45,10 @@ async function run(): Promise<void> {
4445
const verbose = core.getInput('verbose')
4546
const msysMode = core.getInput('msys') === 'true'
4647

47-
const {artifactName, download, id} = await getViaGit(
48-
flavor,
49-
architecture,
50-
githubToken
51-
)
48+
const {artifactName, download, id} =
49+
flavor === 'minimal'
50+
? await getViaCIArtifacts(architecture, githubToken)
51+
: await getViaGit(flavor, architecture, githubToken)
5252
const outputDirectory =
5353
core.getInput('path') || `${getDriveLetterPrefix()}${artifactName}`
5454
core.setOutput('result', outputDirectory)
@@ -59,7 +59,7 @@ async function run(): Promise<void> {
5959
useCache = true
6060
break
6161
case 'auto':
62-
useCache = flavor !== 'full'
62+
useCache = !['full', 'minimal'].includes(flavor)
6363
break
6464
default:
6565
useCache = false

src/ci_artifacts.ts

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import * as core from '@actions/core'
2+
import {Octokit} from '@octokit/rest'
3+
import {getArtifactMetadata} from './git'
4+
import {spawn} from 'child_process'
5+
import * as fs from 'fs'
6+
7+
async function sleep(milliseconds: number): Promise<void> {
8+
return new Promise<void>((resolve, _reject) => {
9+
setTimeout(resolve, milliseconds)
10+
})
11+
}
12+
13+
export async function getViaCIArtifacts(
14+
architecture: string,
15+
githubToken?: string
16+
): Promise<{
17+
artifactName: string
18+
id: string
19+
download: (
20+
outputDirectory: string,
21+
verbose?: number | boolean
22+
) => Promise<void>
23+
}> {
24+
const owner = 'git-for-windows'
25+
26+
const {repo, artifactName} = getArtifactMetadata('minimal', architecture)
27+
28+
const octokit = githubToken ? new Octokit({auth: githubToken}) : new Octokit()
29+
30+
const {
31+
name,
32+
updated_at: updatedAt,
33+
browser_download_url: url
34+
} = await (async () => {
35+
let error: Error | undefined
36+
for (const seconds of [0, 5, 10, 15, 20, 40]) {
37+
if (seconds) await sleep(seconds)
38+
39+
const ciArtifactsResponse = await octokit.repos.getReleaseByTag({
40+
owner,
41+
repo,
42+
tag: 'ci-artifacts'
43+
})
44+
45+
if (ciArtifactsResponse.status !== 200) {
46+
error = new Error(
47+
`Failed to get ci-artifacts release from the ${owner}/${repo} repo: ${ciArtifactsResponse.status}`
48+
)
49+
continue
50+
}
51+
52+
core.info(
53+
`Found ci-artifacts release: ${ciArtifactsResponse.data.html_url}`
54+
)
55+
const tarGzArtifact = ciArtifactsResponse.data.assets.find(asset =>
56+
asset.name.endsWith('.tar.gz')
57+
)
58+
59+
if (!tarGzArtifact) {
60+
error = new Error(
61+
`Failed to find a .tar.gz artifact in the ci-artifacts release of the ${owner}/${repo} repo`
62+
)
63+
continue
64+
}
65+
66+
return tarGzArtifact
67+
}
68+
throw error
69+
})()
70+
core.info(`Found ${name} at ${url}`)
71+
72+
return {
73+
artifactName,
74+
id: `ci-artifacts-${updatedAt}`,
75+
download: async (
76+
outputDirectory: string,
77+
verbose: number | boolean = false
78+
): Promise<void> => {
79+
return new Promise<void>((resolve, reject) => {
80+
const curl = spawn(
81+
`${process.env.SYSTEMROOT}/system32/curl.exe`,
82+
[
83+
...(githubToken
84+
? ['-H', `Authorization: Bearer ${githubToken}`]
85+
: []),
86+
'-H',
87+
'Accept: application/octet-stream',
88+
`-${verbose === true ? '' : 's'}fL`,
89+
url
90+
],
91+
{
92+
stdio: ['ignore', 'pipe', process.stderr]
93+
}
94+
)
95+
curl.on('error', error => reject(error))
96+
97+
fs.mkdirSync(outputDirectory, {recursive: true})
98+
99+
const tar = spawn(
100+
`${process.env.SYSTEMROOT}/system32/tar.exe`,
101+
['-C', outputDirectory, `-x${verbose === true ? 'v' : ''}f`, '-'],
102+
{stdio: ['pipe', process.stdout, process.stderr]}
103+
)
104+
tar.on('error', error => reject(error))
105+
tar.on('close', code => {
106+
if (code === 0) resolve()
107+
else reject(new Error(`tar exited with code ${code}`))
108+
})
109+
110+
curl.stdout.pipe(tar.stdin)
111+
})
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)