Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 132 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getViaGit,
gitForWindowsUsrBinPath
} from './src/git'
import {getViaCIArtifacts} from './src/ci_artifacts'
import * as fs from 'fs'

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

const {artifactName, download, id} = await getViaGit(
flavor,
architecture,
githubToken
)
const {artifactName, download, id} =
flavor === 'minimal'
? await getViaCIArtifacts(architecture, githubToken)
: await getViaGit(flavor, architecture, githubToken)
const outputDirectory =
core.getInput('path') || `${getDriveLetterPrefix()}${artifactName}`
core.setOutput('result', outputDirectory)
Expand All @@ -59,7 +59,7 @@ async function run(): Promise<void> {
useCache = true
break
case 'auto':
useCache = flavor !== 'full'
useCache = !['full', 'minimal'].includes(flavor)
break
default:
useCache = false
Expand Down
114 changes: 114 additions & 0 deletions src/ci_artifacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as core from '@actions/core'
import {Octokit} from '@octokit/rest'
import {getArtifactMetadata} from './git'
import {spawn} from 'child_process'
import * as fs from 'fs'

async function sleep(milliseconds: number): Promise<void> {
return new Promise<void>((resolve, _reject) => {
setTimeout(resolve, milliseconds)
})
}

export async function getViaCIArtifacts(
architecture: string,
githubToken?: string
): Promise<{
artifactName: string
id: string
download: (
outputDirectory: string,
verbose?: number | boolean
) => Promise<void>
}> {
const owner = 'git-for-windows'

const {repo, artifactName} = getArtifactMetadata('minimal', architecture)

const octokit = githubToken ? new Octokit({auth: githubToken}) : new Octokit()

const {
name,
updated_at: updatedAt,
browser_download_url: url
} = await (async () => {
let error: Error | undefined
for (const seconds of [0, 5, 10, 15, 20, 40]) {
if (seconds) await sleep(seconds)

const ciArtifactsResponse = await octokit.repos.getReleaseByTag({
owner,
repo,
tag: 'ci-artifacts'
})

if (ciArtifactsResponse.status !== 200) {
error = new Error(
`Failed to get ci-artifacts release from the ${owner}/${repo} repo: ${ciArtifactsResponse.status}`
)
continue
}

core.info(
`Found ci-artifacts release: ${ciArtifactsResponse.data.html_url}`
)
const tarGzArtifact = ciArtifactsResponse.data.assets.find(asset =>
asset.name.endsWith('.tar.gz')
)

if (!tarGzArtifact) {
error = new Error(
`Failed to find a .tar.gz artifact in the ci-artifacts release of the ${owner}/${repo} repo`
)
continue
}

return tarGzArtifact
}
throw error
})()
core.info(`Found ${name} at ${url}`)

return {
artifactName,
id: `ci-artifacts-${updatedAt}`,
download: async (
outputDirectory: string,
verbose: number | boolean = false
): Promise<void> => {
return new Promise<void>((resolve, reject) => {
const curl = spawn(
`${process.env.SYSTEMROOT}/system32/curl.exe`,
[
...(githubToken
? ['-H', `Authorization: Bearer ${githubToken}`]
: []),
'-H',
'Accept: application/octet-stream',
`-${verbose === true ? '' : 's'}fL`,
url
],
{
stdio: ['ignore', 'pipe', process.stderr]
}
)
curl.on('error', error => reject(error))

fs.mkdirSync(outputDirectory, {recursive: true})

const tar = spawn(
`${process.env.SYSTEMROOT}/system32/tar.exe`,
['-C', outputDirectory, `-x${verbose === true ? 'v' : ''}f`, '-'],
{stdio: ['pipe', process.stdout, process.stderr]}
)
tar.on('error', error => reject(error))
tar.on('close', code => {
if (code === 0) resolve()
else reject(new Error(`tar exited with code ${code}`))
})

curl.stdout.pipe(tar.stdin)
})
}
}
}