From e5fda2027a1f03fca7c35d8047e06120c046e3b9 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Wed, 15 Feb 2023 16:16:09 -0800 Subject: [PATCH] Add JSON output of `go env` and some env as strings Additional outputs are: - GOPATH as `go-path` string - GOROOT as `go-root` string - GOMOD as `go-mod` string - GOCACHE as `go-cache` string - GOMODCACHE as `go-mod-cache` string - `go env` as `go-env` JSON --- .github/workflows/outputs.yml | 24 +++++++++++++++++++++ __tests__/setup-go.test.ts | 23 +++++++++++++++++++++ action.yml | 10 +++++++++ dist/setup/index.js | 32 ++++++++++++++++++++++------ src/main.ts | 39 ++++++++++++++++++++++++++--------- 5 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/outputs.yml diff --git a/.github/workflows/outputs.yml b/.github/workflows/outputs.yml new file mode 100644 index 000000000..9c70a6942 --- /dev/null +++ b/.github/workflows/outputs.yml @@ -0,0 +1,24 @@ +name: Test outputs + +on: + push: + branches: + - main + pull_request: + +jobs: + setup-go-env: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - id: setup-go + uses: ./ + - run: | + echo GOPATH=${{ steps.setup-go.outputs.go-path }} + echo GOROOT=${{ steps.setup-go.outputs.go-root }} + echo GOMOD=${{ steps.setup-go.outputs.go-mod }} + echo GOMODCACHE=${{ steps.setup-go.outputs.go-mod-cache }} + echo GOVERSION=${{ steps.setup-go.outputs.go-version }} + + echo Go environment variables json: + jq . <<< '${{ steps.setup-go.outputs.go-env }}' diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 2188fc9b2..98b975e3b 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -129,6 +129,29 @@ describe('setup-go', () => { expect(main.parseGoVersion(goVersionOutput)).toBe('1.16.6'); }); + it('can read go env variables', async () => { + let goRoot = '/opt/hostedtoolcache/go/1.18.10/x64'; + let goPath = '/home/runner/go'; + let goModCache = '/home/runner/go/pkg/mod'; + let goCache = '/home/runner/.cache/go-build'; + let goVersion = 'go1.18.10'; + + let env = ` +GOROOT="${goRoot}" +GOPATH="${goPath}" +GOMODCACHE="${goModCache}" +GOCACHE="${goCache}" +GOVERSION="${goVersion}" + `; + let json = JSON.parse(main.convertEnvStringToJson(env)); + expect(json).toBeDefined(); + expect(json['GOROOT']).toBe(goRoot); + expect(json['GOPATH']).toBe(goPath); + expect(json['GOMODCACHE']).toBe(goModCache); + expect(json['GOCACHE']).toBe(goCache); + expect(json['GOVERSION']).toBe(goVersion); + }); + it('can find 1.9.7 from manifest on osx', async () => { os.platform = 'darwin'; os.arch = 'x64'; diff --git a/action.yml b/action.yml index 64e32717b..aea38244c 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,16 @@ inputs: outputs: go-version: description: 'The installed Go version. Useful when given a version range as input.' + go-path: + description: 'The GOPATH environment variable' + go-root: + description: 'The GOROOT environment variable' + go-mod: + description: 'The GOMOD environment variable' + go-mod-cache: + description: 'The GOMODCACHE environment variable' + go-env: + description: 'The Go environment variables in JSON format' cache-hit: description: 'A boolean value to indicate if a cache was hit' runs: diff --git a/dist/setup/index.js b/dist/setup/index.js index 40f31c570..4c222ff8e 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -63559,7 +63559,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.parseGoVersion = exports.addBinToPath = exports.run = void 0; +exports.convertEnvStringToJson = exports.parseGoVersion = exports.addBinToPath = exports.run = void 0; const core = __importStar(__nccwpck_require__(2186)); const io = __importStar(__nccwpck_require__(7436)); const installer = __importStar(__nccwpck_require__(2574)); @@ -63603,20 +63603,30 @@ function run() { core.info(`Successfully set up Go version ${versionSpec}`); } let goPath = yield io.which('go'); + let goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString(); + let goEnvJson = JSON.parse(convertEnvStringToJson(goEnv)); let goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString(); + let parsedGoVersion = parseGoVersion(goVersion); + // Go versions less that 1.16 do not have the GOVERSION environment variable + if (semver.lt(parsedGoVersion, '1.16.0')) { + goEnvJson['GOVERSION'] = 'go' + parsedGoVersion; + } + core.info(goVersion); if (cache && cache_utils_1.isCacheFeatureAvailable()) { const packageManager = 'default'; const cacheDependencyPath = core.getInput('cache-dependency-path'); - yield cache_restore_1.restoreCache(parseGoVersion(goVersion), packageManager, cacheDependencyPath); + yield cache_restore_1.restoreCache(parsedGoVersion, packageManager, cacheDependencyPath); } // add problem matchers const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json'); core.info(`##[add-matcher]${matchersPath}`); - // output the version actually being used - core.info(goVersion); - core.setOutput('go-version', parseGoVersion(goVersion)); + core.setOutput('go-version', parsedGoVersion); + core.setOutput('go-path', goEnvJson['GOPATH']); + core.setOutput('go-root', goEnvJson['GOROOT']); + core.setOutput('go-cache', goEnvJson['GOCACHE']); + core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']); + core.setOutput('go-env', goEnvJson); core.startGroup('go env'); - let goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString(); core.info(goEnv); core.endGroup(); } @@ -63664,6 +63674,16 @@ function parseGoVersion(versionString) { return versionString.split(' ')[2].slice('go'.length); } exports.parseGoVersion = parseGoVersion; +function convertEnvStringToJson(envString) { + const envArray = envString.split('\n'); + const envObject = {}; + envArray.forEach(envVar => { + const [key, value] = envVar.split(/=(?=")/); + envObject[key] = value === null || value === void 0 ? void 0 : value.replace(/"/g, ''); + }); + return JSON.stringify(envObject); +} +exports.convertEnvStringToJson = convertEnvStringToJson; function resolveVersionInput() { let version = core.getInput('go-version'); const versionFilePath = core.getInput('go-version-file'); diff --git a/src/main.ts b/src/main.ts index 6cdb2e614..edd330725 100644 --- a/src/main.ts +++ b/src/main.ts @@ -57,29 +57,36 @@ export async function run() { } let goPath = await io.which('go'); + let goEnv = (cp.execSync(`${goPath} env`) || '').toString(); + let goEnvJson = JSON.parse(convertEnvStringToJson(goEnv)); let goVersion = (cp.execSync(`${goPath} version`) || '').toString(); + let parsedGoVersion = parseGoVersion(goVersion); + + // Go versions less that 1.16 do not have the GOVERSION environment variable + if (semver.lt(parsedGoVersion, '1.16.0')) { + goEnvJson['GOVERSION'] = 'go' + parsedGoVersion; + } + + core.info(goVersion); if (cache && isCacheFeatureAvailable()) { const packageManager = 'default'; const cacheDependencyPath = core.getInput('cache-dependency-path'); - await restoreCache( - parseGoVersion(goVersion), - packageManager, - cacheDependencyPath - ); + await restoreCache(parsedGoVersion, packageManager, cacheDependencyPath); } // add problem matchers const matchersPath = path.join(__dirname, '../..', 'matchers.json'); core.info(`##[add-matcher]${matchersPath}`); - // output the version actually being used - core.info(goVersion); - - core.setOutput('go-version', parseGoVersion(goVersion)); + core.setOutput('go-version', parsedGoVersion); + core.setOutput('go-path', goEnvJson['GOPATH']); + core.setOutput('go-root', goEnvJson['GOROOT']); + core.setOutput('go-cache', goEnvJson['GOCACHE']); + core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']); + core.setOutput('go-env', goEnvJson); core.startGroup('go env'); - let goEnv = (cp.execSync(`${goPath} env`) || '').toString(); core.info(goEnv); core.endGroup(); } catch (error) { @@ -126,6 +133,18 @@ export function parseGoVersion(versionString: string): string { return versionString.split(' ')[2].slice('go'.length); } +export function convertEnvStringToJson(envString: string): string { + const envArray = envString.split('\n'); + const envObject: {[key: string]: string} = {}; + + envArray.forEach(envVar => { + const [key, value] = envVar.split(/=(?=")/); + envObject[key] = value?.replace(/"/g, ''); + }); + + return JSON.stringify(envObject); +} + function resolveVersionInput(): string { let version = core.getInput('go-version'); const versionFilePath = core.getInput('go-version-file');