Skip to content

Commit 4eeb49d

Browse files
authored
Merge pull request #485 from dennisameling/add-cleanup-logic
Add support for cleaning up SDK files
2 parents 2a6d045 + e0979af commit 4eeb49d

File tree

6 files changed

+161
-19
lines changed

6 files changed

+161
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ To accelerate this Action, artifacts are cached once downloaded. This can be tur
7272

7373
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`.
7474

75+
### Clean-up
76+
77+
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`.
78+
7579
## Developing _this_ Action
7680

7781
> First, you'll need to have a reasonably modern version of `node` handy, such as Node 12.

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ inputs:
2121
required: false
2222
description: 'Where to write the SDK files'
2323
default: ''
24+
cleanup:
25+
required: false
26+
description: 'Whether to clean up SDK files. This is only needed on self-hosted runners that are reused for multiple jobs.'
27+
default: 'false'
2428
verbose:
2529
required: false
2630
description: 'Whether to log files as they are extracted'
@@ -32,3 +36,4 @@ inputs:
3236
runs:
3337
using: 'node16'
3438
main: 'dist/index.js'
39+
post: 'dist/index.js'

dist/index.js

Lines changed: 64 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.ts

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import {mkdirp} from './src/downloader'
33
import {restoreCache, saveCache} from '@actions/cache'
44
import process from 'process'
55
import {spawnSync} from 'child_process'
6-
import {getViaGit} from './src/git'
6+
import {
7+
getArtifactMetadata,
8+
getViaGit,
9+
gitForWindowsUsrBinPath
10+
} from './src/git'
11+
import * as fs from 'fs'
12+
import * as coreCommand from '@actions/core/lib/command'
13+
14+
const flavor = core.getInput('flavor')
15+
const architecture = core.getInput('architecture')
716

817
async function run(): Promise<void> {
918
try {
@@ -13,8 +22,6 @@ async function run(): Promise<void> {
1322
)
1423
return
1524
}
16-
const flavor = core.getInput('flavor')
17-
const architecture = core.getInput('architecture')
1825
const verbose = core.getInput('verbose')
1926
const msysMode = core.getInput('msys') === 'true'
2027

@@ -114,4 +121,63 @@ async function run(): Promise<void> {
114121
}
115122
}
116123

117-
run()
124+
function cleanup(): void {
125+
if (core.getInput('cleanup') !== 'true') {
126+
core.info(
127+
`Won't clean up SDK files as the 'cleanup' input was not provided or doesn't equal 'true'.`
128+
)
129+
return
130+
}
131+
132+
const outputDirectory =
133+
core.getInput('path') ||
134+
`C:/${getArtifactMetadata(flavor, architecture).artifactName}`
135+
136+
/**
137+
* Shelling out to `rm -rf` is more than twice as fast as Node's `fs.rmSync` method.
138+
* Let's use it if it's available, and otherwise fall back to `fs.rmSync`.
139+
*/
140+
const cleanupMethod = fs.existsSync(`${gitForWindowsUsrBinPath}/bash.exe`)
141+
? 'rm -rf'
142+
: 'node'
143+
144+
core.info(
145+
`Cleaning up ${outputDirectory} using the "${cleanupMethod}" method...`
146+
)
147+
148+
if (cleanupMethod === 'rm -rf') {
149+
const child = spawnSync(
150+
`${gitForWindowsUsrBinPath}/bash.exe`,
151+
['-c', `rm -rf "${outputDirectory}"`],
152+
{
153+
encoding: 'utf-8',
154+
env: {PATH: '/usr/bin'}
155+
}
156+
)
157+
158+
if (child.error) throw child.error
159+
if (child.stderr) core.error(child.stderr)
160+
} else {
161+
fs.rmSync(outputDirectory, {recursive: true, force: true})
162+
}
163+
164+
core.info(`Finished cleaning up ${outputDirectory}.`)
165+
}
166+
167+
/**
168+
* Indicates whether the POST action is running
169+
*/
170+
export const isPost = !!process.env['STATE_isPost']
171+
172+
if (!isPost) {
173+
run()
174+
/*
175+
* Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
176+
* This is necessary since we don't have a separate entry point.
177+
* Inspired by https://github.com/actions/checkout/blob/v3.0.2/src/state-helper.ts#L69-L71
178+
*/
179+
coreCommand.issueCommand('save-state', {name: 'isPost'}, 'true')
180+
} else {
181+
// If the POST action is running, we cleanup our artifacts
182+
cleanup()
183+
}

src/git.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Octokit} from '@octokit/rest'
44
import {delimiter} from 'path'
55
import * as fs from 'fs'
66

7-
const gitForWindowsUsrBinPath = 'C:/Program Files/Git/usr/bin'
7+
export const gitForWindowsUsrBinPath = 'C:/Program Files/Git/usr/bin'
88
const gitExePath = 'C:/Program Files/Git/cmd/git.exe'
99

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

21+
export function getArtifactMetadata(
22+
flavor: string,
23+
architecture: string
24+
): {bitness: string; repo: string; artifactName: string} {
25+
const bitness = architecture === 'i686' ? '32' : '64'
26+
const repo = `git-sdk-${bitness}`
27+
const artifactName = `${repo}-${flavor}`
28+
29+
return {bitness, repo, artifactName}
30+
}
31+
2132
async function clone(
2233
url: string,
2334
destination: string,
@@ -65,10 +76,12 @@ export async function getViaGit(
6576
verbose?: number | boolean
6677
) => Promise<void>
6778
}> {
68-
const bitness = architecture === 'i686' ? '32' : '64'
6979
const owner = 'git-for-windows'
70-
const repo = `git-sdk-${bitness}`
71-
const artifactName = `${repo}-${flavor}`
80+
81+
const {bitness, repo, artifactName} = getArtifactMetadata(
82+
flavor,
83+
architecture
84+
)
7285

7386
const octokit = new Octokit()
7487
let head_sha: string

0 commit comments

Comments
 (0)