From e58a9e5d14231715ece082f2068a0bd148cb72e6 Mon Sep 17 00:00:00 2001 From: Dawid Dziurla Date: Wed, 22 Jan 2025 10:47:03 +0100 Subject: [PATCH] Unzip (#325) * action: add use_unzip input * main: implement use_unzip input * workflows: test use_unzip * main: remove zip file at the end * workflows: test if zip is removed * main: fix * main: try this * main: debug * main: forgot await * workflows: debug * workflows: should be ok now * README: update --- .github/workflows/download.yml | 20 ++++++++++++++++++++ README.md | 3 +++ action.yml | 4 ++++ main.js | 25 ++++++++++++++++--------- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/.github/workflows/download.yml b/.github/workflows/download.yml index 01247428..64c1307b 100644 --- a/.github/workflows/download.yml +++ b/.github/workflows/download.yml @@ -155,6 +155,26 @@ jobs: ! test -d artifact/artifact ! test -f artifact.zip unzip -l artifact/artifact.zip + download-use-unzip: + runs-on: ubuntu-latest + needs: wait + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Download + uses: ./ + with: + workflow: upload.yml + name: artifact + path: artifact + use_unzip: true + - name: Test + run: | + test -d artifact + ! test -d artifact/artifact + ! test -f artifact.zip + ! test -f artifact/artifact.zip + cat artifact/sha | grep $GITHUB_SHA download-dry-run-exists: runs-on: ubuntu-latest needs: wait diff --git a/README.md b/README.md index 743caa2a..07f73d56 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,9 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar # Optional, include forks when searching for artifacts # default false allow_forks: true + # Optional, choose to unpack the downloaded artifact(s) using `unzip` system utility + # default false + use_unzip: false ``` ## Troubleshooting diff --git a/action.yml b/action.yml index e6994360..915f64f4 100644 --- a/action.yml +++ b/action.yml @@ -88,6 +88,10 @@ inputs: fail, warn or ignore default: fail + use_unzip: + description: Use system provided `unzip` utility instead of JS library for unpacking + required: false + default: false outputs: error_message: description: The error message, if an error occurs diff --git a/main.js b/main.js index 7ff54a54..d2eafd89 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,5 @@ const core = require('@actions/core') +const exec = require('@actions/exec') const github = require('@actions/github') const artifact = require('@actions/artifact') const AdmZip = require('adm-zip') @@ -37,6 +38,7 @@ async function main() { const nameIsRegExp = core.getBooleanInput("name_is_regexp") const skipUnpack = core.getBooleanInput("skip_unpack") const ifNoArtifactFound = core.getInput("if_no_artifact_found") + const useUnzip = core.getBooleanInput("use_unzip") let workflow = core.getInput("workflow") let workflowSearch = core.getBooleanInput("workflow_search") let workflowConclusion = core.getInput("workflow_conclusion") @@ -270,17 +272,22 @@ async function main() { fs.mkdirSync(dir, { recursive: true }) - const adm = new AdmZip(Buffer.from(zip.data)) - core.startGroup(`==> Extracting: ${artifact.name}.zip`) - adm.getEntries().forEach((entry) => { - const action = entry.isDirectory ? "creating" : "inflating" - const filepath = pathname.join(dir, entry.entryName) - - core.info(` ${action}: ${filepath}`) - }) + if (useUnzip) { + const zipPath = `${pathname.join(dir, artifact.name)}.zip` + fs.writeFileSync(zipPath, Buffer.from(zip.data), 'binary') + await exec.exec("unzip", [zipPath, "-d", dir]) + fs.rmSync(zipPath) + } else { + const adm = new AdmZip(Buffer.from(zip.data)) + adm.getEntries().forEach((entry) => { + const action = entry.isDirectory ? "creating" : "inflating" + const filepath = pathname.join(dir, entry.entryName) - adm.extractAllTo(dir, true) + core.info(` ${action}: ${filepath}`) + }) + adm.extractAllTo(dir, true) + } core.endGroup() } } catch (error) {