Skip to content

Commit 98edb4f

Browse files
committed
Add JSON output of go env and some env as strings
Additional outputs are: - GOPATH as `go-path` string - GOMOD as `go-mod` string - GOCACHE as `go-cache` string - GOMODCACHE as `go-mod-cache` string - `go env` as `go-env` JSON
1 parent 0c52d54 commit 98edb4f

File tree

5 files changed

+112
-12
lines changed

5 files changed

+112
-12
lines changed

.github/workflows/outputs.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Test outputs
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
setup-go-env:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- id: setup-go
15+
uses: ./
16+
- run: |
17+
echo GOPATH=${{ steps.setup-go.outputs.go-path }}
18+
echo GOMOD=${{ steps.setup-go.outputs.go-mod }}
19+
echo GOMODCACHE=${{ steps.setup-go.outputs.go-mod-cache }}
20+
echo GOVERSION=${{ steps.setup-go.outputs.go-version }}
21+
echo GOCACHE=${{ steps.setup-go.outputs.go-cache }}
22+
23+
echo Go environment variables json:
24+
jq . <<< '${{ steps.setup-go.outputs.go-env }}'

__tests__/setup-go.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ describe('setup-go', () => {
138138
expect(main.parseGoVersion(goVersionOutput)).toBe('1.16.6');
139139
});
140140

141+
it('can read go env variables', async () => {
142+
const goRoot = '/opt/hostedtoolcache/go/1.18.10/x64';
143+
const goPath = '/home/runner/go';
144+
const goModCache = '/home/runner/go/pkg/mod';
145+
const goCache = '/home/runner/.cache/go-build';
146+
const goVersion = 'go1.18.10';
147+
148+
const env = `
149+
GOROOT="${goRoot}"
150+
GOPATH="${goPath}"
151+
GOMODCACHE="${goModCache}"
152+
GOCACHE="${goCache}"
153+
GOVERSION="${goVersion}"
154+
`;
155+
const json = JSON.parse(main.convertEnvStringToJson(env));
156+
expect(json).toBeDefined();
157+
expect(json['GOROOT']).toBe(goRoot);
158+
expect(json['GOPATH']).toBe(goPath);
159+
expect(json['GOMODCACHE']).toBe(goModCache);
160+
expect(json['GOCACHE']).toBe(goCache);
161+
expect(json['GOVERSION']).toBe(goVersion);
162+
});
163+
141164
it('can find 1.9.7 from manifest on osx', async () => {
142165
os.platform = 'darwin';
143166
os.arch = 'x64';

action.yml

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ inputs:
2222
outputs:
2323
go-version:
2424
description: 'The installed Go version. Useful when given a version range as input.'
25+
go-cache:
26+
description: 'The GOCACHE environment variable'
27+
go-path:
28+
description: 'The GOPATH environment variable'
29+
go-root:
30+
description: 'The GOROOT environment variable'
31+
go-mod:
32+
description: 'The GOMOD environment variable'
33+
go-mod-cache:
34+
description: 'The GOMODCACHE environment variable'
35+
go-env:
36+
description: 'The Go environment variables in JSON format'
2537
cache-hit:
2638
description: 'A boolean value to indicate if a cache was hit'
2739
runs:

dist/setup/index.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -88408,7 +88408,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
8840888408
return (mod && mod.__esModule) ? mod : { "default": mod };
8840988409
};
8841088410
Object.defineProperty(exports, "__esModule", ({ value: true }));
88411-
exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
88411+
exports.convertEnvStringToJson = exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
8841288412
const core = __importStar(__nccwpck_require__(2186));
8841388413
const io = __importStar(__nccwpck_require__(7436));
8841488414
const installer = __importStar(__nccwpck_require__(2574));
@@ -88456,11 +88456,19 @@ function run() {
8845688456
core.debug(`add bin ${added}`);
8845788457
const goPath = yield io.which('go');
8845888458
const goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString();
88459+
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
88460+
const goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
88461+
const parsedGoVersion = parseGoVersion(goVersion);
88462+
// Go versions less that 1.16 do not have the GOVERSION environment variable
88463+
if (semver.lt(parsedGoVersion, '1.16.0')) {
88464+
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
88465+
}
88466+
core.info(goVersion);
8845988467
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
8846088468
const packageManager = 'default';
8846188469
const cacheDependencyPath = core.getInput('cache-dependency-path');
8846288470
try {
88463-
yield (0, cache_restore_1.restoreCache)(parseGoVersion(goVersion), packageManager, cacheDependencyPath);
88471+
yield (0, cache_restore_1.restoreCache)(parsedGoVersion, packageManager, cacheDependencyPath);
8846488472
}
8846588473
catch (error) {
8846688474
core.warning(`Restore cache failed: ${error.message}`);
@@ -88469,11 +88477,12 @@ function run() {
8846988477
// add problem matchers
8847088478
const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json');
8847188479
core.info(`##[add-matcher]${matchersPath}`);
88472-
// output the version actually being used
88473-
core.info(goVersion);
88474-
core.setOutput('go-version', parseGoVersion(goVersion));
88480+
core.setOutput('go-version', parsedGoVersion);
88481+
core.setOutput('go-path', goEnvJson['GOPATH']);
88482+
core.setOutput('go-cache', goEnvJson['GOCACHE']);
88483+
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
88484+
core.setOutput('go-env', goEnvJson);
8847588485
core.startGroup('go env');
88476-
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
8847788486
core.info(goEnv);
8847888487
core.endGroup();
8847988488
}
@@ -88521,6 +88530,16 @@ function parseGoVersion(versionString) {
8852188530
return versionString.split(' ')[2].slice('go'.length);
8852288531
}
8852388532
exports.parseGoVersion = parseGoVersion;
88533+
function convertEnvStringToJson(envString) {
88534+
const envArray = envString.split('\n');
88535+
const envObject = {};
88536+
envArray.forEach(envVar => {
88537+
const [key, value] = envVar.split(/=(?=")/);
88538+
envObject[key] = value === null || value === void 0 ? void 0 : value.replace(/"/g, '');
88539+
});
88540+
return JSON.stringify(envObject);
88541+
}
88542+
exports.convertEnvStringToJson = convertEnvStringToJson;
8852488543
function resolveVersionInput() {
8852588544
let version = core.getInput('go-version');
8852688545
const versionFilePath = core.getInput('go-version-file');

src/main.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,23 @@ export async function run() {
6363

6464
const goPath = await io.which('go');
6565
const goVersion = (cp.execSync(`${goPath} version`) || '').toString();
66+
const goEnv = (cp.execSync(`${goPath} env`) || '').toString();
67+
const goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
68+
const parsedGoVersion = parseGoVersion(goVersion);
69+
70+
// Go versions less that 1.16 do not have the GOVERSION environment variable
71+
if (semver.lt(parsedGoVersion, '1.16.0')) {
72+
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
73+
}
74+
75+
core.info(goVersion);
6676

6777
if (cache && isCacheFeatureAvailable()) {
6878
const packageManager = 'default';
6979
const cacheDependencyPath = core.getInput('cache-dependency-path');
7080
try {
7181
await restoreCache(
72-
parseGoVersion(goVersion),
82+
parsedGoVersion,
7383
packageManager,
7484
cacheDependencyPath
7585
);
@@ -82,13 +92,13 @@ export async function run() {
8292
const matchersPath = path.join(__dirname, '../..', 'matchers.json');
8393
core.info(`##[add-matcher]${matchersPath}`);
8494

85-
// output the version actually being used
86-
core.info(goVersion);
87-
88-
core.setOutput('go-version', parseGoVersion(goVersion));
95+
core.setOutput('go-version', parsedGoVersion);
96+
core.setOutput('go-path', goEnvJson['GOPATH']);
97+
core.setOutput('go-cache', goEnvJson['GOCACHE']);
98+
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
99+
core.setOutput('go-env', goEnvJson);
89100

90101
core.startGroup('go env');
91-
const goEnv = (cp.execSync(`${goPath} env`) || '').toString();
92102
core.info(goEnv);
93103
core.endGroup();
94104
} catch (error) {
@@ -135,6 +145,18 @@ export function parseGoVersion(versionString: string): string {
135145
return versionString.split(' ')[2].slice('go'.length);
136146
}
137147

148+
export function convertEnvStringToJson(envString: string): string {
149+
const envArray = envString.split('\n');
150+
const envObject: {[key: string]: string} = {};
151+
152+
envArray.forEach(envVar => {
153+
const [key, value] = envVar.split(/=(?=")/);
154+
envObject[key] = value?.replace(/"/g, '');
155+
});
156+
157+
return JSON.stringify(envObject);
158+
}
159+
138160
function resolveVersionInput(): string {
139161
let version = core.getInput('go-version');
140162
const versionFilePath = core.getInput('go-version-file');

0 commit comments

Comments
 (0)