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
```
# Articles
[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 mri from "mri"
import untildify from "untildify"
import { isCI } from "./utils/env/isci"
import { isGitHubCI } from "./utils/env/isci"
import semverValid from "semver/functions/valid"
import { getVersion } from "./default_versions"
@ -79,7 +79,7 @@ const inputs: Array<Inputs> = ["compiler", "architecture", ...tools]
/** The main entry function */
export async function main(args: string[]): Promise<number> {
if (!isCI()) {
if (!isGitHubCI()) {
process.env.ACTIONS_ALLOW_UNSECURE_COMMANDS = "true"
}
@ -196,7 +196,7 @@ export async function main(args: string[]): Promise<number> {
core.info("setup_cpp finished")
if (!isCI()) {
if (!isGitHubCI()) {
switch (process.platform) {
case "win32": {
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 hasha from "hasha"
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) {
if (!isCI()) {
if (!isGitHubCI()) {
// TODO parse versoin
return setupPythonViaSystem("", setupDir, arch)
}

View File

@ -1,3 +1,7 @@
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 { isCI } from "../env/isci"
import { isGitHubCI } from "../env/isci"
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) {
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,14 +2,28 @@ import { addPath as ghAddPath } from "@actions/core"
import { delimiter } from "path"
import * as core from "@actions/core"
import execa from "execa"
import { isGitHubCI } from "../env/isci"
/** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) {
try {
if (isGitHubCI()) {
ghAddPath(path)
} else {
addPathSystem(path)
}
} catch (err) {
try {
core.error(err as Error)
return addPathSystem(path)
} catch (err2) {
core.error(err2 as Error)
}
core.error(`Failed to add ${path} to the percistent PATH. You should add it manually.`)
}
}
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
execa.sync(`setx PATH=${path};%PATH%`)
@ -26,10 +40,5 @@ export function addPath(path: string) {
// fall through shell path modification
}
}
} catch (err2) {
core.error(err2 as Error)
}
core.error(`Failed to add ${path} to the percistent PATH. You should add it manually.`)
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
}
}

View File

@ -4,7 +4,7 @@ import { addPath } from "../path/addPath"
import { join } from "path"
import { existsSync } from "fs"
import { tmpdir } from "os"
import { isCI } from "../env/isci"
import { isGitHubCI } from "../env/isci"
/** A type that describes a package */
export type PackageInfo = {
@ -52,7 +52,7 @@ export async function setupBin(
)
// Restore from cache (if found).
if (isCI()) {
if (isGitHubCI()) {
try {
const dir = find(name, version)
if (dir) {
@ -86,7 +86,7 @@ export async function setupBin(
addPath(binDir)
// 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)
}