use runner's RUNNER_TEMP for temp directory (#75)

* use runner tempdir

* fix tests etc

* feedback

* ran npm install before generating dist
This commit is contained in:
Edwin Sirko
2024-01-31 16:50:45 -05:00
committed by GitHub
parent d6635df138
commit df1bbcf10f
6 changed files with 49 additions and 105 deletions
+10 -17
View File
@@ -4,11 +4,16 @@ import * as path from 'path'
import * as tar from 'tar'
import * as archiver from 'archiver'
import * as crypto from 'crypto'
import * as os from 'os'
export function createTempDir(): string {
const randomDirName = crypto.randomBytes(4).toString('hex')
const tempDir = path.join(os.tmpdir(), randomDirName)
export interface FileMetadata {
path: string
size: number
sha256: string
}
export function createTempDir(subDirName: string): string {
const runnerTempDir: string = process.env.RUNNER_TEMP || ''
const tempDir = path.join(runnerTempDir, subDirName)
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir)
@@ -17,23 +22,11 @@ export function createTempDir(): string {
return tempDir
}
export function removeDir(dir: string): void {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true })
}
}
export interface FileMetadata {
path: string
size: number
sha256: string
}
// Creates both a tar.gz and zip archive of the given directory and returns the paths to both archives (stored in the provided target directory)
// as well as the size/sha256 hash of each file.
export async function createArchives(
distPath: string,
archiveTargetPath: string = createTempDir()
archiveTargetPath: string
): Promise<{ zipFile: FileMetadata; tarFile: FileMetadata }> {
const zipPath = path.join(archiveTargetPath, `archive.zip`)
const tarPath = path.join(archiveTargetPath, `archive.tar.gz`)
+2 -13
View File
@@ -11,8 +11,6 @@ import semver from 'semver'
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
const tmpDirs: string[] = []
try {
const repository: string = process.env.GITHUB_REPOSITORY || ''
if (repository === '') {
@@ -34,13 +32,11 @@ export async function run(): Promise<void> {
const semanticVersion = parseSourceSemanticVersion()
// Create a temporary directory to stage files for packaging in archives
const stagedActionFilesDir = fsHelper.createTempDir()
tmpDirs.push(stagedActionFilesDir)
const stagedActionFilesDir = fsHelper.createTempDir('staging')
fsHelper.stageActionFiles('.', stagedActionFilesDir)
// Create a temporary directory to store the archives
const archiveDir = fsHelper.createTempDir()
tmpDirs.push(archiveDir)
const archiveDir = fsHelper.createTempDir('archive')
const archives = await fsHelper.createArchives(
stagedActionFilesDir,
archiveDir
@@ -82,13 +78,6 @@ export async function run(): Promise<void> {
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
} finally {
// Clean up any temporary directories that exist
for (const tmpDir of tmpDirs) {
if (tmpDir !== '') {
fsHelper.removeDir(tmpDir)
}
}
}
}