Skip to content

Add support for cleaning up SDK files #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 27, 2022
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ To accelerate this Action, artifacts are cached once downloaded. This can be tur

In practice, caching the `full` artifacts does not provide much of a speed-up. Instead, it slows it down by spending extra minutes on caching the artifact. Therefore, caching is disabled for the `full` artifacts by default, corresponding to `cache: auto`.

### Clean-up

On self-hosted runners, the SDK files persist after the workflow run is done. To remove these files, set the input parameter `cleanup` to `true`.

## Developing _this_ Action

> First, you'll need to have a reasonably modern version of `node` handy, such as Node 12.
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ inputs:
required: false
description: 'Where to write the SDK files'
default: ''
cleanup:
required: false
description: 'Whether to clean up SDK files. This is only needed on self-hosted runners that are reused for multiple jobs.'
default: 'false'
verbose:
required: false
description: 'Whether to log files as they are extracted'
Expand All @@ -32,3 +36,4 @@ inputs:
runs:
using: 'node16'
main: 'dist/index.js'
post: 'dist/index.js'
74 changes: 64 additions & 10 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.

74 changes: 70 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import {mkdirp} from './src/downloader'
import {restoreCache, saveCache} from '@actions/cache'
import process from 'process'
import {spawnSync} from 'child_process'
import {getViaGit} from './src/git'
import {
getArtifactMetadata,
getViaGit,
gitForWindowsUsrBinPath
} from './src/git'
import * as fs from 'fs'
import * as coreCommand from '@actions/core/lib/command'

const flavor = core.getInput('flavor')
const architecture = core.getInput('architecture')

async function run(): Promise<void> {
try {
Expand All @@ -13,8 +22,6 @@ async function run(): Promise<void> {
)
return
}
const flavor = core.getInput('flavor')
const architecture = core.getInput('architecture')
const verbose = core.getInput('verbose')
const msysMode = core.getInput('msys') === 'true'

Expand Down Expand Up @@ -114,4 +121,63 @@ async function run(): Promise<void> {
}
}

run()
function cleanup(): void {
if (core.getInput('cleanup') !== 'true') {
core.info(
`Won't clean up SDK files as the 'cleanup' input was not provided or doesn't equal 'true'.`
)
return
}

const outputDirectory =
core.getInput('path') ||
`C:/${getArtifactMetadata(flavor, architecture).artifactName}`

/**
* Shelling out to `rm -rf` is more than twice as fast as Node's `fs.rmSync` method.
* Let's use it if it's available, and otherwise fall back to `fs.rmSync`.
*/
const cleanupMethod = fs.existsSync(`${gitForWindowsUsrBinPath}/bash.exe`)
? 'rm -rf'
: 'node'

core.info(
`Cleaning up ${outputDirectory} using the "${cleanupMethod}" method...`
)

if (cleanupMethod === 'rm -rf') {
const child = spawnSync(
`${gitForWindowsUsrBinPath}/bash.exe`,
['-c', `rm -rf "${outputDirectory}"`],
{
encoding: 'utf-8',
env: {PATH: '/usr/bin'}
}
)

if (child.error) throw child.error
if (child.stderr) core.error(child.stderr)
} else {
fs.rmSync(outputDirectory, {recursive: true, force: true})
}

core.info(`Finished cleaning up ${outputDirectory}.`)
}

/**
* Indicates whether the POST action is running
*/
export const isPost = !!process.env['STATE_isPost']

if (!isPost) {
run()
/*
* Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
* This is necessary since we don't have a separate entry point.
* Inspired by https://github.com/actions/checkout/blob/v3.0.2/src/state-helper.ts#L69-L71
*/
coreCommand.issueCommand('save-state', {name: 'isPost'}, 'true')
} else {
// If the POST action is running, we cleanup our artifacts
cleanup()
}
21 changes: 17 additions & 4 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Octokit} from '@octokit/rest'
import {delimiter} from 'path'
import * as fs from 'fs'

const gitForWindowsUsrBinPath = 'C:/Program Files/Git/usr/bin'
export const gitForWindowsUsrBinPath = 'C:/Program Files/Git/usr/bin'
const gitExePath = 'C:/Program Files/Git/cmd/git.exe'

/*
Expand All @@ -18,6 +18,17 @@ const gitExePath = 'C:/Program Files/Git/cmd/git.exe'
*/
const GIT_CONFIG_PARAMETERS = `'checkout.workers=56'`

export function getArtifactMetadata(
flavor: string,
architecture: string
): {bitness: string; repo: string; artifactName: string} {
const bitness = architecture === 'i686' ? '32' : '64'
const repo = `git-sdk-${bitness}`
const artifactName = `${repo}-${flavor}`

return {bitness, repo, artifactName}
}

async function clone(
url: string,
destination: string,
Expand Down Expand Up @@ -65,10 +76,12 @@ export async function getViaGit(
verbose?: number | boolean
) => Promise<void>
}> {
const bitness = architecture === 'i686' ? '32' : '64'
const owner = 'git-for-windows'
const repo = `git-sdk-${bitness}`
const artifactName = `${repo}-${flavor}`

const {bitness, repo, artifactName} = getArtifactMetadata(
flavor,
architecture
)

const octokit = new Octokit()
let head_sha: string
Expand Down