From dcbcd9ccd4a21bbe3d1ab5b7f16f820b7859b69c Mon Sep 17 00:00:00 2001 From: "Thomas A. Christensen II" <25492070+MillironX@users.noreply.github.com> Date: Mon, 13 Jun 2022 15:07:53 -0500 Subject: [PATCH] Add more robust caching --- index.js | 68 +++++++++++++++++++++++++++++++++-------------- package-lock.json | 1 + package.json | 1 + 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index d155b91..50c5d18 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,10 @@ const core = require("@actions/core"); +const fs = require("fs"); const github = require("@actions/github"); +const io = require("@actions/io"); +const retry = require("async-retry"); const semver = require("semver"); const tc = require("@actions/tool-cache"); -const fs = require("fs"); -const retry = require("async-retry"); const NEXTFLOW_REPO = { owner: "nextflow-io", repo: "nextflow" }; @@ -73,11 +74,38 @@ function nextflow_bin_url(release, get_all) { return dl_asset.browser_download_url; } +async function install_nextflow(url, version) { + core.debug(`Downloading Nextflow from ${url}`); + const nf_dl_path = await retry( + async (bail) => { + return await tc.downloadTool(url); + }, + { + onRetry: (err) => { + core.debug(`Download of ${url} failed, trying again. Error ${err}`); + }, + } + ); + + const temp_install_dir = fs.mkdtempSync(`nxf-${version}`); + + const nf_path = await io.mv(nf_path, `${temp_install_dir}/nextflow`, { + force: true, + }); + fs.chmod(nf_path, "+x"); + + return temp_install_dir; +} + async function run() { + // Read in the arguments + const token = core.getInput("token"); + const version = core.getInput("version"); + const get_all = core.getBooleanInput("all"); + // Setup the API let octokit = {}; try { - const token = core.getInput("token"); octokit = github.getOctokit(token); } catch (e) { core.setFailed( @@ -88,7 +116,6 @@ async function run() { // Get the release info for the desired release let release = {}; try { - const version = core.getInput("version"); release = await release_data(version, octokit); core.info( `Input version '${version}' resolved to Nextflow ${release.name}` @@ -102,29 +129,30 @@ async function run() { // Get the download url for the desired release let url = ""; try { - const get_all = core.getBooleanInput("all"); url = nextflow_bin_url(release, get_all); core.info(`Preparing to download from ${url}`); } catch (e) { core.setFailed(`Could not parse the download URL\n${e.message}`); } - - // Download Nextflow and add it to path - let nf_path = ""; try { - const temp_install_dir = fs.mkdtempSync(`nextflow`); - nf_path = await retry( - async (bail) => { - return await tc.downloadTool(url, `${temp_install_dir}/nextflow`); - }, - { - onRetry: (err) => { - core.debug(`Download of ${url} failed, trying again. Error: ${err}`); - }, - } - ); + // Download Nextflow and add it to path + let nf_path = ""; + nf_path = tc.find("nextflow", version); + + if (!nf_path) { + core.debug(`Could not find Nextflow ${version} in cache`); + const nf_install_path = await install_nextflow(url, version); + + nf_path = await tc.cacheDir(nf_install_path, "nextflow", version); + core.debug(`Added Nextflow to cache: ${nf_path}`); + + io.rmRF(nf_install_path); + } else { + core.debug(`Using cached version of Nextflow: ${nf_path}`); + } + + core.addPath(nf_path); - core.addPath(temp_install_dir); core.info(`Downloaded \`nextflow\` to ${nf_path} and added to PATH`); } catch (e) { core.setFailed(e.message); diff --git a/package-lock.json b/package-lock.json index faf89ee..d5bbc45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@actions/core": "^1.8.2", "@actions/github": "^5.0.3", + "@actions/io": "^1.1.2", "@actions/tool-cache": "^2.0.1", "async-retry": "^1.3.3", "semver": "^7.3.7" diff --git a/package.json b/package.json index 8686df6..934a0c9 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "@actions/core": "^1.8.2", "@actions/github": "^5.0.3", + "@actions/io": "^1.1.2", "@actions/tool-cache": "^2.0.1", "async-retry": "^1.3.3", "semver": "^7.3.7"