How to use @actions/tool-cache - 10 common examples

To help you get started, we’ve selected a few @actions/tool-cache examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github actions / setup-node / src / installer.ts View on Github external
version: string
): Promise {
  // Create temporary folder to download in to
  let tempDownloadFolder: string =
    'temp_' + Math.floor(Math.random() * 2000000000);
  let tempDir: string = path.join(tempDirectory, tempDownloadFolder);
  await io.mkdirP(tempDir);
  let exeUrl: string;
  let libUrl: string;
  try {
    exeUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.exe`;
    libUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.lib`;

    const exePath = await tc.downloadTool(exeUrl);
    await io.cp(exePath, path.join(tempDir, 'node.exe'));
    const libPath = await tc.downloadTool(libUrl);
    await io.cp(libPath, path.join(tempDir, 'node.lib'));
  } catch (err) {
    if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
      exeUrl = `https://nodejs.org/dist/v${version}/node.exe`;
      libUrl = `https://nodejs.org/dist/v${version}/node.lib`;

      const exePath = await tc.downloadTool(exeUrl);
      await io.cp(exePath, path.join(tempDir, 'node.exe'));
      const libPath = await tc.downloadTool(libUrl);
      await io.cp(libPath, path.join(tempDir, 'node.lib'));
    } else {
      throw err;
    }
  }
  return await tc.cacheDir(tempDir, 'node', version);
}
github jimhester / setup-r / src / installer.ts View on Github external
async function acquireRUbuntu(version: string): Promise {
  //
  // Download - a tool installer intimately knows how to get the tool (and construct urls)
  //
  let fileName: string = getFileNameUbuntu(version);
  let downloadUrl: string = getDownloadUrlUbuntu(fileName);
  let downloadPath: string | null = null;
  try {
    downloadPath = await tc.downloadTool(downloadUrl);
    await io.mv(downloadPath, path.join(tempDirectory, fileName));
  } catch (error) {
    core.debug(error);

    throw `Failed to download version ${version}: ${error}`;
  }

  //
  // Install
  //
  let extPath: string = tempDirectory;
  if (!extPath) {
    throw new Error("Temp directory not set");
  }

  try {
github arduino / setup-arduino-cli / src / installer.ts View on Github external
async function downloadRelease(version: string): Promise {
  // Download
  let fileName: string = getFileName(version);
  let downloadUrl: string = util.format(
    "https://github.com/Arduino/arduino-cli/releases/download/%s/%s",
    version,
    fileName
  );
  let downloadPath: string | null = null;
  try {
    downloadPath = await tc.downloadTool(downloadUrl);
  } catch (error) {
    core.debug(error);
    throw `Failed to download version ${version}: ${error}`;
  }

  // Extract
  let extPath: string | null = null;
  if (osPlat == "win32") {
    extPath = await tc.extractZip(downloadPath);
  } else {
    extPath = await tc.extractTar(downloadPath);
  }

  // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
  return await tc.cacheDir(extPath, "arduino-cli", version);
}
github jimhester / setup-r / src / installer.ts View on Github external
async function acquireRMacOS(version: string): Promise {
  //
  // Download - a tool installer intimately knows how to get the tool (and construct urls)
  //
  let fileName: string = getFileNameMacOS(version);
  let downloadUrl: string = getDownloadUrlMacOS(version);
  let downloadPath: string | null = null;
  try {
    downloadPath = await tc.downloadTool(downloadUrl);
    await io.mv(downloadPath, path.join(tempDirectory, fileName));
  } catch (error) {
    core.debug(error);

    throw `Failed to download version ${version}: ${error}`;
  }

  //
  // Extract
  //
  let extPath: string = tempDirectory;
  if (!extPath) {
    throw new Error("Temp directory not set");
  }

  try {
github warrenbuckley / Setup-Nuget / src / main.ts View on Github external
async function run() {
  try {

    // Tripple check it's Windows process
    // Can't install nuget.exe for Ubuntu image etc..
    const IS_WINDOWS = process.platform === 'win32';
    if(IS_WINDOWS === false){
      core.setFailed("Nuget.exe only works for Windows.");
      return;
    }

    // Try & find tool in cache
    let directoryToAddToPath:string;
    directoryToAddToPath = await tc.find("nuget", "latest");

    if(directoryToAddToPath){
      core.debug(`Found local cached tool at ${directoryToAddToPath} adding that to path`);
      await core.addPath(directoryToAddToPath);
      return;
    }

    // Download latest Nuget.exe
    core.debug("Downloading Nuget tool");
    const nugetPath = await tc.downloadTool("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe");

    // Rename the file which is a GUID without extension
    var folder = path.dirname(nugetPath);
    var fullPath = path.join(folder, "nuget.exe");
    fs.renameSync(nugetPath, fullPath);
github anchore / scan-action / index.js View on Github external
async function installInlineScan(version) {
    let scanScriptPath = cache.find(scanScript, version);
    if (!scanScriptPath) {
        // Not found, install it
        scanScriptPath = await downloadInlineScan(version);
    }

    // Add tool to path for this and future actions to use 
    core.addPath(scanScriptPath);
}
github matootie / dokube / index.js View on Github external
token: kubeconfigToken
          }
        }
      ]
    };

    // Save the kubeconfig object.
    const formattedConfig = JSON.stringify(kubeconfig, null, 4);
    fs.writeFileSync(`${workdir}/kubeconfig`, formattedConfig);

    // Download and install kubectl.
    const kubectl = await tc.downloadTool("https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl");
    await io.mv(kubectl, `${workdir}/kubectl`);

    // Cache kubectl and kubeconfig.
    const cachedPath = await tc.cacheDir(workdir, 'kubectl', 'v1.16.0');

    // Set KUBECONFIG environment variable.
    core.exportVariable('KUBECONFIG', `${cachedPath}/kubeconfig`);

    // Add kubectl to PATH.
    core.addPath(`${cachedPath}/kubectl`);
  }
  catch (error) {
    core.setFailed(error.message);
  }
}
github warrenbuckley / Setup-Nuget / src / main.ts View on Github external
core.debug(`Found local cached tool at ${directoryToAddToPath} adding that to path`);
      await core.addPath(directoryToAddToPath);
      return;
    }

    // Download latest Nuget.exe
    core.debug("Downloading Nuget tool");
    const nugetPath = await tc.downloadTool("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe");

    // Rename the file which is a GUID without extension
    var folder = path.dirname(nugetPath);
    var fullPath = path.join(folder, "nuget.exe");
    fs.renameSync(nugetPath, fullPath);

    //Cache the directory with Nuget in it - which returns a NEW cached location
    var cachedToolDir = await tc.cacheDir(folder, "nuget", "latest");
    core.debug(`Cached Tool Dir ${cachedToolDir}`);

    // Add Nuget.exe CLI tool to path for other steps to be able to access it
    await core.addPath(cachedToolDir);

  } catch (error) {
    core.setFailed(error.message);
  }
}
github arduino / setup-arduino-cli / src / installer.ts View on Github external
downloadPath = await tc.downloadTool(downloadUrl);
  } catch (error) {
    core.debug(error);
    throw `Failed to download version ${version}: ${error}`;
  }

  // Extract
  let extPath: string | null = null;
  if (osPlat == "win32") {
    extPath = await tc.extractZip(downloadPath);
  } else {
    extPath = await tc.extractTar(downloadPath);
  }

  // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
  return await tc.cacheDir(extPath, "arduino-cli", version);
}
github r-lib / actions / setup-pandoc / src / setup-pandoc.ts View on Github external
downloadPath = await tc.downloadTool(downloadUrl);
  } catch (error) {
    throw `Failed to download Pandoc ${version}: ${error}`;
  }

  //
  // Extract
  //
  let extPath: string = tempDirectory;
  if (!extPath) {
    throw new Error("Temp directory not set");
  }

  extPath = await tc.extractZip(downloadPath);

  const toolPath = await tc.cacheDir(extPath, "pandoc", version);

  // It extracts to this folder
  const toolRoot = path.join(
    toolPath,
    util.format("pandoc-%s-windows-x86_64", version)
  );

  core.addPath(toolRoot);
}