diff --git a/__tests__/fs-helper.test.ts b/__tests__/fs-helper.test.ts index 46f63fa..89328af 100644 --- a/__tests__/fs-helper.test.ts +++ b/__tests__/fs-helper.test.ts @@ -10,12 +10,13 @@ describe('stageActionFiles', () => { let stagingDir: string beforeEach(() => { - sourceDir = fsHelper.createTempDir() + process.env.RUNNER_TEMP = '/tmp' + sourceDir = fsHelper.createTempDir('source') fs.mkdirSync(`${sourceDir}/src`) fs.writeFileSync(`${sourceDir}/src/main.js`, fileContent) fs.writeFileSync(`${sourceDir}/src/other.js`, fileContent) - stagingDir = fsHelper.createTempDir() + stagingDir = fsHelper.createTempDir('staging') }) afterEach(() => { @@ -67,36 +68,40 @@ describe('stageActionFiles', () => { }) describe('createArchives', () => { - let tmpDir: string - let distDir: string + let stageDir: string + let archiveDir: string beforeAll(() => { - distDir = fsHelper.createTempDir() - fs.writeFileSync(`${distDir}/hello.txt`, fileContent) - fs.writeFileSync(`${distDir}/world.txt`, fileContent) + process.env.RUNNER_TEMP = '/tmp' + stageDir = fsHelper.createTempDir('staging') + fs.writeFileSync(`${stageDir}/hello.txt`, fileContent) + fs.writeFileSync(`${stageDir}/world.txt`, fileContent) }) beforeEach(() => { - tmpDir = fsHelper.createTempDir() + archiveDir = fsHelper.createTempDir('archive') }) afterEach(() => { - fs.rmSync(tmpDir, { recursive: true }) + fs.rmSync(archiveDir, { recursive: true }) }) afterAll(() => { - fs.rmSync(distDir, { recursive: true }) + fs.rmSync(stageDir, { recursive: true }) }) it('creates archives', async () => { - const { zipFile, tarFile } = await fsHelper.createArchives(distDir, tmpDir) + const { zipFile, tarFile } = await fsHelper.createArchives( + stageDir, + archiveDir + ) - expect(zipFile.path).toEqual(`${tmpDir}/archive.zip`) + expect(zipFile.path).toEqual(`${archiveDir}/archive.zip`) expect(fs.existsSync(zipFile.path)).toEqual(true) expect(fs.statSync(zipFile.path).size).toBeGreaterThan(0) expect(zipFile.sha256.startsWith('sha256:')).toEqual(true) - expect(tarFile.path).toEqual(`${tmpDir}/archive.tar.gz`) + expect(tarFile.path).toEqual(`${archiveDir}/archive.tar.gz`) expect(fs.existsSync(tarFile.path)).toEqual(true) expect(fs.statSync(tarFile.path).size).toBeGreaterThan(0) expect(tarFile.sha256.startsWith('sha256:')).toEqual(true) @@ -150,20 +155,20 @@ describe('createTempDir', () => { } }) - it('creates a temporary directory in the OS temporary dir', () => { - const tmpDir = fsHelper.createTempDir() - dirs.push(tmpDir) + it('creates a temporary directory', () => { + process.env.RUNNER_TEMP = '/tmp' + const tmpDir = fsHelper.createTempDir('subdir') expect(fs.existsSync(tmpDir)).toEqual(true) expect(fs.statSync(tmpDir).isDirectory()).toEqual(true) - expect(tmpDir.startsWith(os.tmpdir())).toEqual(true) }) it('creates a unique temporary directory', () => { - const dir1 = fsHelper.createTempDir() + process.env.RUNNER_TEMP = '/tmp' + const dir1 = fsHelper.createTempDir('dir1') dirs.push(dir1) - const dir2 = fsHelper.createTempDir() + const dir2 = fsHelper.createTempDir('dir2') dirs.push(dir2) expect(dir1).not.toEqual(dir2) @@ -174,7 +179,8 @@ describe('isDirectory', () => { let dir: string beforeEach(() => { - dir = fsHelper.createTempDir() + process.env.RUNNER_TEMP = '/tmp' + dir = fsHelper.createTempDir('subdir') }) afterEach(() => { @@ -196,7 +202,8 @@ describe('readFileContents', () => { let dir: string beforeEach(() => { - dir = fsHelper.createTempDir() + process.env.RUNNER_TEMP = '/tmp' + dir = fsHelper.createTempDir('subdir') }) afterEach(() => { @@ -210,22 +217,3 @@ describe('readFileContents', () => { expect(fsHelper.readFileContents(tempFile).toString()).toEqual(fileContent) }) }) - -describe('removeDir', () => { - let dir: string - - beforeEach(() => { - dir = fsHelper.createTempDir() - }) - - afterEach(() => { - if (fs.existsSync(dir)) { - fs.rmSync(dir, { recursive: true }) - } - }) - - it('removes a directory', () => { - fsHelper.removeDir(dir) - expect(fs.existsSync(dir)).toEqual(false) - }) -}) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 97fc2d6..7e7883c 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -21,7 +21,6 @@ let setOutputMock: jest.SpyInstance // Mock the filesystem helper let createTempDirMock: jest.SpyInstance let createArchivesMock: jest.SpyInstance -let removeDirMock: jest.SpyInstance let stageActionFilesMock: jest.SpyInstance // Mock the GHCR Client @@ -46,7 +45,6 @@ describe('run', () => { createArchivesMock = jest .spyOn(fsHelper, 'createArchives') .mockImplementation() - removeDirMock = jest.spyOn(fsHelper, 'removeDir').mockImplementation() stageActionFilesMock = jest .spyOn(fsHelper, 'stageActionFiles') .mockImplementation() @@ -327,7 +325,7 @@ describe('run', () => { } } - createTempDirMock.mockImplementation(() => '/tmp/test') + createTempDirMock.mockImplementation(() => '/tmp/test/subdir') createArchivesMock.mockImplementation(() => { return { @@ -382,11 +380,5 @@ describe('run', () => { 'package-manifest-sha', 'sha256:my-test-digest' ) - - // Expect all the temp files to be cleaned up - expect(removeDirMock).toHaveBeenCalledWith('/tmp/test') - expect(removeDirMock).toHaveBeenCalledTimes( - createTempDirMock.mock.calls.length - ) }) }) diff --git a/badges/coverage.svg b/badges/coverage.svg index b52c8b0..8225784 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 94.25%Coverage94.25% \ No newline at end of file +Coverage: 93.96%Coverage93.96% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 967a81f..854a9ab 100644 --- a/dist/index.js +++ b/dist/index.js @@ -74379,32 +74379,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.stageActionFiles = exports.readFileContents = exports.isDirectory = exports.createArchives = exports.removeDir = exports.createTempDir = void 0; +exports.stageActionFiles = exports.readFileContents = exports.isDirectory = exports.createArchives = exports.createTempDir = void 0; const fs = __importStar(__nccwpck_require__(57147)); const fs_extra_1 = __importDefault(__nccwpck_require__(5630)); const path = __importStar(__nccwpck_require__(71017)); const tar = __importStar(__nccwpck_require__(74674)); const archiver = __importStar(__nccwpck_require__(43084)); const crypto = __importStar(__nccwpck_require__(6113)); -const os = __importStar(__nccwpck_require__(22037)); -function createTempDir() { - const randomDirName = crypto.randomBytes(4).toString('hex'); - const tempDir = path.join(os.tmpdir(), randomDirName); +function createTempDir(subDirName) { + const runnerTempDir = process.env.RUNNER_TEMP || ''; + const tempDir = path.join(runnerTempDir, subDirName); if (!fs.existsSync(tempDir)) { fs.mkdirSync(tempDir); } return tempDir; } exports.createTempDir = createTempDir; -function removeDir(dir) { - if (fs.existsSync(dir)) { - fs.rmSync(dir, { recursive: true }); - } -} -exports.removeDir = removeDir; // 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. -async function createArchives(distPath, archiveTargetPath = createTempDir()) { +async function createArchives(distPath, archiveTargetPath) { const zipPath = path.join(archiveTargetPath, `archive.zip`); const tarPath = path.join(archiveTargetPath, `archive.tar.gz`); const createZipPromise = new Promise((resolve, reject) => { @@ -74709,7 +74702,6 @@ const semver_1 = __importDefault(__nccwpck_require__(11383)); * @returns {Promise} Resolves when the action is complete. */ async function run() { - const tmpDirs = []; try { const repository = process.env.GITHUB_REPOSITORY || ''; if (repository === '') { @@ -74728,12 +74720,10 @@ async function run() { } 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); const { repoId, ownerId } = await api.getRepositoryMetadata(repository, token); const manifest = ociContainer.createActionPackageManifest(archives.tarFile, archives.zipFile, repository, repoId, ownerId, sourceCommit, semanticVersion.raw, new Date()); @@ -74749,14 +74739,6 @@ async function run() { 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); - } - } - } } exports.run = run; // This action can be triggered by release events or tag push events. diff --git a/src/fs-helper.ts b/src/fs-helper.ts index e2431de..651171d 100644 --- a/src/fs-helper.ts +++ b/src/fs-helper.ts @@ -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`) diff --git a/src/main.ts b/src/main.ts index b72ae7e..0d29c18 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,8 +11,6 @@ import semver from 'semver' * @returns {Promise} Resolves when the action is complete. */ export async function run(): Promise { - const tmpDirs: string[] = [] - try { const repository: string = process.env.GITHUB_REPOSITORY || '' if (repository === '') { @@ -34,13 +32,11 @@ export async function run(): Promise { 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 { } 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) - } - } } }