Skip to content

Commit b661219

Browse files
dennisamelingdscho
authored andcommitted
Add support for cleaning up SDK files
On self-hosted runners, we need to do this, otherwise these files will be left in place after the workflow run has finished. Signed-off-by: Dennis Ameling <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent edf9275 commit b661219

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
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'

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+
}

0 commit comments

Comments
 (0)