delete temporary files after import

pull/79/head
Alexander Bentkamp 2 years ago
parent cf5783f2e4
commit 161c88d58f

@ -55,7 +55,8 @@ async function download(id, url, dest) {
} }
async function doImport (owner, repo, id) { async function doImport (owner, repo, id) {
let artifactId = null
try {
const artifacts = await octokit.request('GET /repos/{owner}/{repo}/actions/artifacts', { const artifacts = await octokit.request('GET /repos/{owner}/{repo}/actions/artifacts', {
owner, owner,
repo, repo,
@ -66,25 +67,41 @@ async function doImport (owner, repo, id) {
// choose latest artifact // choose latest artifact
const artifact = artifacts.data.artifacts const artifact = artifacts.data.artifacts
.reduce((acc, cur) => acc.created_at < cur.created_at ? cur : acc) .reduce((acc, cur) => acc.created_at < cur.created_at ? cur : acc)
artifactId = artifact.id
const url = artifact.archive_download_url const url = artifact.archive_download_url
if (!fs.existsSync("tmp")){
fs.mkdirSync("tmp");
}
progress[id].output += `Download from ${url}\n` progress[id].output += `Download from ${url}\n`
await download(id, url, `artifact_${artifact.id}.zip`) await download(id, url, `tmp/artifact_${artifactId}.zip`)
progress[id].output += `Download finished.\n` progress[id].output += `Download finished.\n`
progress[id].output += `Unpacking ZIP.\n` progress[id].output += `Unpacking ZIP.\n`
const files = await decompress(`artifact_${artifact.id}.zip`, `artifact_${artifact.id}`) const files = await decompress(`tmp/artifact_${artifactId}.zip`, `tmp/artifact_${artifactId}`)
if (files.length != 1) { throw Error(`Unexpected number of files in ZIP: ${files.length}`) }
progress[id].output += `Unpacking TAR.\n` progress[id].output += `Unpacking TAR.\n`
const files_inner = await decompress(`artifact_${artifact.id}/${files[0].path}`, `artifact_${artifact.id}_inner`) const files_inner = await decompress(`tmp/artifact_${artifactId}/${files[0].path}`, `tmp/artifact_${artifactId}_inner`)
let manifest = fs.readFileSync(`artifact_${artifact.id}_inner/manifest.json`); let manifest = fs.readFileSync(`tmp/artifact_${artifactId}_inner/manifest.json`);
manifest = JSON.parse(manifest); manifest = JSON.parse(manifest);
if (manifest.length !== 1) { if (manifest.length !== 1) {
throw `Unexpected manifest: ${JSON.stringify(manifest)}` throw `Unexpected manifest: ${JSON.stringify(manifest)}`
} }
manifest[0].RepoTags = [`github-${owner}:${repo}`] manifest[0].RepoTags = [`github-${owner}:${repo}`]
fs.writeFileSync(`artifact_${artifact.id}_inner/manifest.json`, JSON.stringify(manifest)); fs.writeFileSync(`tmp/artifact_${artifactId}_inner/manifest.json`, JSON.stringify(manifest));
await runProcess(id, "tar", ["-cvf", `../archive_${artifact.id}.tar`, "."], `artifact_${artifact.id}_inner/`) await runProcess(id, "tar", ["-cvf", `../archive_${artifactId}.tar`, "."], `tmp/artifact_${artifactId}_inner/`)
await runProcess(id, "docker", ["load", "-i", `archive_${artifact.id}.tar`]) await runProcess(id, "docker", ["load", "-i", `tmp/archive_${artifactId}.tar`])
progress[id].done = true progress[id].done = true
progress[id].output += `Done.\n` progress[id].output += `Done.\n`
} catch (e) {
progress[id].output += `Error: ${e.toString()}\n${e.stack}`
} finally {
if (artifactId) {
fs.rmSync(`tmp/artifact_${artifactId}.zip`, {force: true, recursive: true});
fs.rmSync(`tmp/artifact_${artifactId}`, {force: true, recursive: true});
fs.rmSync(`tmp/artifact_${artifactId}_inner`, {force: true, recursive: true});
fs.rmSync(`tmp/archive_${artifactId}.tar`, {force: true, recursive: true});
}
progress[id].done = true
}
} }
export const importTrigger = (req, res) => { export const importTrigger = (req, res) => {

Loading…
Cancel
Save