|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const core = require("@actions/core"); |
| 4 | +const exec = require("@actions/exec"); |
| 5 | +const tc = require("@actions/tool-cache"); |
| 6 | + |
| 7 | +/* |
| 8 | + remember to run 'npm run package:cf' after changing this file to recreate the packed version 'index.js' |
| 9 | +*/ |
| 10 | +const SUPPORTED_VERSIONS = ["7", "8"]; |
| 11 | + |
| 12 | +async function main() { |
| 13 | + try { |
| 14 | + // get input |
| 15 | + const api = core.getInput("api"); |
| 16 | + const org = core.getInput("org", { required: true }); |
| 17 | + const space = core.getInput("space", { required: true }); |
| 18 | + const version = core.getInput("cf-version"); |
| 19 | + |
| 20 | + if (!SUPPORTED_VERSIONS.includes(version)) { |
| 21 | + throw new Error(`CF CLI version ${version} not supported. Use one of ${SUPPORTED_VERSIONS}`); |
| 22 | + } |
| 23 | + |
| 24 | + let cachedPath = tc.find("cf-cli", version); |
| 25 | + if (cachedPath) { |
| 26 | + core.info(`Found CF CLI version ${version} in tool cache`); |
| 27 | + } else { |
| 28 | + core.startGroup("Downloading CF CLI package..."); |
| 29 | + |
| 30 | + const downloadUrl = `https://packages.cloudfoundry.org/stable?release=linux64-binary&version=v${version}&source=github`; |
| 31 | + core.info(`Getting ${version} from ${downloadUrl}`); |
| 32 | + const downloadPath = await tc.downloadTool(downloadUrl); |
| 33 | + core.info(`CF CLI version ${version} was downloaded`); |
| 34 | + |
| 35 | + core.info("Extracting CF CLI package..."); |
| 36 | + const cfToolExtractedPath = await tc.extractTar(downloadPath); |
| 37 | + |
| 38 | + core.info("Adding to cache..."); |
| 39 | + cachedPath = await tc.cacheDir(cfToolExtractedPath, "cf-cli", version); |
| 40 | + |
| 41 | + core.endGroup(); |
| 42 | + } |
| 43 | + |
| 44 | + core.info(`Installing CF CLI in ${cachedPath}`); |
| 45 | + core.addPath(cachedPath); |
| 46 | + |
| 47 | + await exec.exec("cf", ["install-plugin", "multiapps", "-f"]); |
| 48 | + await exec.exec("cf", ["-v"]); |
| 49 | + await exec.exec("cf", ["api", api]); |
| 50 | + await exec.exec("cf", ["auth"]); |
| 51 | + await exec.exec("cf", ["target", "-o", org, "-s", space]); |
| 52 | + |
| 53 | + core.info(`Done`); |
| 54 | + } catch (error) { |
| 55 | + core.setFailed(error.message); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +(async () => await main())(); |
0 commit comments