fix: run GitHub specific code only on GitHub CI

This commit is contained in:
Amin Yahyaabadi 2022-01-17 14:23:33 -08:00
parent 54c51d7f14
commit c0e8c96665
9 changed files with 45 additions and 33 deletions

View File

@ -209,7 +209,6 @@ jobs:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true ACTIONS_ALLOW_UNSECURE_COMMANDS: true
``` ```
# Articles # Articles
[Setup-Cpp on Dev.to](https://dev.to/aminya/setup-cpp-3ia4) [Setup-Cpp on Dev.to](https://dev.to/aminya/setup-cpp-3ia4)

2
dist/setup_cpp.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,7 @@ import { setupOpencppcoverage } from "./opencppcoverage/opencppcoverage"
import { setupPython } from "./python/python" import { setupPython } from "./python/python"
import mri from "mri" import mri from "mri"
import untildify from "untildify" import untildify from "untildify"
import { isCI } from "./utils/env/isci" import { isGitHubCI } from "./utils/env/isci"
import semverValid from "semver/functions/valid" import semverValid from "semver/functions/valid"
import { getVersion } from "./default_versions" import { getVersion } from "./default_versions"
@ -79,7 +79,7 @@ const inputs: Array<Inputs> = ["compiler", "architecture", ...tools]
/** The main entry function */ /** The main entry function */
export async function main(args: string[]): Promise<number> { export async function main(args: string[]): Promise<number> {
if (!isCI()) { if (!isGitHubCI()) {
process.env.ACTIONS_ALLOW_UNSECURE_COMMANDS = "true" process.env.ACTIONS_ALLOW_UNSECURE_COMMANDS = "true"
} }
@ -196,7 +196,7 @@ export async function main(args: string[]): Promise<number> {
core.info("setup_cpp finished") core.info("setup_cpp finished")
if (!isCI()) { if (!isGitHubCI()) {
switch (process.platform) { switch (process.platform) {
case "win32": { case "win32": {
core.info("Run `RefreshEnv.cmd` or restart your shell to update the environment.") core.info("Run `RefreshEnv.cmd` or restart your shell to update the environment.")

View File

@ -5,10 +5,10 @@ import { setupBrewPack } from "../utils/setup/setupBrewPack"
import { setupChocoPack } from "../utils/setup/setupChocoPack" import { setupChocoPack } from "../utils/setup/setupChocoPack"
import hasha from "hasha" import hasha from "hasha"
import { join } from "path" import { join } from "path"
import { isCI } from "../utils/env/isci" import { isGitHubCI } from "../utils/env/isci"
export function setupPython(version: string, setupDir: string, arch: string) { export function setupPython(version: string, setupDir: string, arch: string) {
if (!isCI()) { if (!isGitHubCI()) {
// TODO parse versoin // TODO parse versoin
return setupPythonViaSystem("", setupDir, arch) return setupPythonViaSystem("", setupDir, arch)
} }

View File

@ -1,3 +1,7 @@
export function isCI() { export function isCI() {
return !(process.env.CI === undefined || process.env.CI === "" || process.env.CI === "false") return process.env.CI === "true"
}
export function isGitHubCI() {
return isCI() && process.env.GITHUB_ACTIONS == "true"
} }

View File

@ -1,10 +1,10 @@
import * as core from "@actions/core" import * as core from "@actions/core"
import { isCI } from "../env/isci" import { isGitHubCI } from "../env/isci"
export function error(err: string | Error) { export function error(err: string | Error) {
return isCI() ? core.error(err) : console.log(`\x1b[31m${err}\x1b[0m`) return isGitHubCI() ? core.error(err) : console.log(`\x1b[31m${err}\x1b[0m`)
} }
export function success(msg: string) { export function success(msg: string) {
return isCI() ? core.info(msg) : console.log(`\x1b[32m${msg}\x1b[0m`) return isGitHubCI() ? core.info(msg) : console.log(`\x1b[32m${msg}\x1b[0m`)
} }

View File

@ -2,34 +2,43 @@ import { addPath as ghAddPath } from "@actions/core"
import { delimiter } from "path" import { delimiter } from "path"
import * as core from "@actions/core" import * as core from "@actions/core"
import execa from "execa" import execa from "execa"
import { isGitHubCI } from "../env/isci"
/** An add path function that works locally or inside GitHub Actions */ /** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) { export function addPath(path: string) {
try { try {
ghAddPath(path) if (isGitHubCI()) {
ghAddPath(path)
} else {
addPathSystem(path)
}
} catch (err) { } catch (err) {
try { try {
core.error(err as Error) core.error(err as Error)
switch (process.platform) { return addPathSystem(path)
case "win32": {
execa.sync(`setx PATH=${path};%PATH%`)
return
}
case "linux":
case "darwin": {
execa.commandSync(`echo "export PATH=${path}:$PATH" >> ~/.profile`)
execa.commandSync(`source ~/.profile`)
core.info(`${path} was added to ~/.profile`)
return
}
default: {
// fall through shell path modification
}
}
} catch (err2) { } catch (err2) {
core.error(err2 as Error) core.error(err2 as Error)
} }
core.error(`Failed to add ${path} to the percistent PATH. You should add it manually.`) core.error(`Failed to add ${path} to the percistent PATH. You should add it manually.`)
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
} }
} }
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
execa.sync(`setx PATH=${path};%PATH%`)
return
}
case "linux":
case "darwin": {
execa.commandSync(`echo "export PATH=${path}:$PATH" >> ~/.profile`)
execa.commandSync(`source ~/.profile`)
core.info(`${path} was added to ~/.profile`)
return
}
default: {
// fall through shell path modification
}
}
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
}

View File

@ -4,7 +4,7 @@ import { addPath } from "../path/addPath"
import { join } from "path" import { join } from "path"
import { existsSync } from "fs" import { existsSync } from "fs"
import { tmpdir } from "os" import { tmpdir } from "os"
import { isCI } from "../env/isci" import { isGitHubCI } from "../env/isci"
/** A type that describes a package */ /** A type that describes a package */
export type PackageInfo = { export type PackageInfo = {
@ -52,7 +52,7 @@ export async function setupBin(
) )
// Restore from cache (if found). // Restore from cache (if found).
if (isCI()) { if (isGitHubCI()) {
try { try {
const dir = find(name, version) const dir = find(name, version)
if (dir) { if (dir) {
@ -86,7 +86,7 @@ export async function setupBin(
addPath(binDir) addPath(binDir)
// check if inside Github Actions. If so, cache the installation // check if inside Github Actions. If so, cache the installation
if (isCI() && typeof process.env.RUNNER_TOOL_CACHE === "string") { if (isGitHubCI() && typeof process.env.RUNNER_TOOL_CACHE === "string") {
await cacheDir(setupDir, name, version) await cacheDir(setupDir, name, version)
} }