Skip to content

Commit b68021e

Browse files
committed
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
1 parent 8dbf352 commit b68021e

File tree

5 files changed

+115
-12
lines changed

5 files changed

+115
-12
lines changed

.github/workflows/outputs.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 GOROOT=${{ steps.setup-go.outputs.go-root }}
19+
echo GOMOD=${{ steps.setup-go.outputs.go-mod }}
20+
echo GOMODCACHE=${{ steps.setup-go.outputs.go-mod-cache }}
21+
echo GOVERSION=${{ steps.setup-go.outputs.go-version }}
22+
echo GOCACHE=${{ steps.setup-go.outputs.go-cache }}
23+
24+
echo Go environment variables json:
25+
jq . <<< '${{ steps.setup-go.outputs.go-env }}'

__tests__/setup-go.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,29 @@ describe('setup-go', () => {
129129
expect(main.parseGoVersion(goVersionOutput)).toBe('1.16.6');
130130
});
131131

132+
it('can read go env variables', async () => {
133+
const goRoot = '/opt/hostedtoolcache/go/1.18.10/x64';
134+
const goPath = '/home/runner/go';
135+
const goModCache = '/home/runner/go/pkg/mod';
136+
const goCache = '/home/runner/.cache/go-build';
137+
const goVersion = 'go1.18.10';
138+
139+
const env = `
140+
GOROOT="${goRoot}"
141+
GOPATH="${goPath}"
142+
GOMODCACHE="${goModCache}"
143+
GOCACHE="${goCache}"
144+
GOVERSION="${goVersion}"
145+
`;
146+
const json = JSON.parse(main.convertEnvStringToJson(env));
147+
expect(json).toBeDefined();
148+
expect(json['GOROOT']).toBe(goRoot);
149+
expect(json['GOPATH']).toBe(goPath);
150+
expect(json['GOMODCACHE']).toBe(goModCache);
151+
expect(json['GOCACHE']).toBe(goCache);
152+
expect(json['GOVERSION']).toBe(goVersion);
153+
});
154+
132155
it('can find 1.9.7 from manifest on osx', async () => {
133156
os.platform = 'darwin';
134157
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

+26-6
Original file line numberDiff line numberDiff line change
@@ -63566,7 +63566,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6356663566
return (mod && mod.__esModule) ? mod : { "default": mod };
6356763567
};
6356863568
Object.defineProperty(exports, "__esModule", ({ value: true }));
63569-
exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
63569+
exports.convertEnvStringToJson = exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
6357063570
const core = __importStar(__nccwpck_require__(2186));
6357163571
const io = __importStar(__nccwpck_require__(7436));
6357263572
const installer = __importStar(__nccwpck_require__(2574));
@@ -63614,11 +63614,19 @@ function run() {
6361463614
core.debug(`add bin ${added}`);
6361563615
const goPath = yield io.which('go');
6361663616
const goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString();
63617+
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
63618+
const goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
63619+
const parsedGoVersion = parseGoVersion(goVersion);
63620+
// Go versions less that 1.16 do not have the GOVERSION environment variable
63621+
if (semver.lt(parsedGoVersion, '1.16.0')) {
63622+
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
63623+
}
63624+
core.info(goVersion);
6361763625
if (cache && cache_utils_1.isCacheFeatureAvailable()) {
6361863626
const packageManager = 'default';
6361963627
const cacheDependencyPath = core.getInput('cache-dependency-path');
6362063628
try {
63621-
yield cache_restore_1.restoreCache(parseGoVersion(goVersion), packageManager, cacheDependencyPath);
63629+
yield cache_restore_1.restoreCache(parsedGoVersion, packageManager, cacheDependencyPath);
6362263630
}
6362363631
catch (error) {
6362463632
core.warning(`Restore cache failed: ${error.message}`);
@@ -63627,11 +63635,13 @@ function run() {
6362763635
// add problem matchers
6362863636
const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json');
6362963637
core.info(`##[add-matcher]${matchersPath}`);
63630-
// output the version actually being used
63631-
core.info(goVersion);
63632-
core.setOutput('go-version', parseGoVersion(goVersion));
63638+
core.setOutput('go-version', parsedGoVersion);
63639+
core.setOutput('go-path', goEnvJson['GOPATH']);
63640+
core.setOutput('go-root', goEnvJson['GOROOT']);
63641+
core.setOutput('go-cache', goEnvJson['GOCACHE']);
63642+
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
63643+
core.setOutput('go-env', goEnvJson);
6363363644
core.startGroup('go env');
63634-
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
6363563645
core.info(goEnv);
6363663646
core.endGroup();
6363763647
}
@@ -63679,6 +63689,16 @@ function parseGoVersion(versionString) {
6367963689
return versionString.split(' ')[2].slice('go'.length);
6368063690
}
6368163691
exports.parseGoVersion = parseGoVersion;
63692+
function convertEnvStringToJson(envString) {
63693+
const envArray = envString.split('\n');
63694+
const envObject = {};
63695+
envArray.forEach(envVar => {
63696+
const [key, value] = envVar.split(/=(?=")/);
63697+
envObject[key] = value === null || value === void 0 ? void 0 : value.replace(/"/g, '');
63698+
});
63699+
return JSON.stringify(envObject);
63700+
}
63701+
exports.convertEnvStringToJson = convertEnvStringToJson;
6368263702
function resolveVersionInput() {
6368363703
let version = core.getInput('go-version');
6368463704
const versionFilePath = core.getInput('go-version-file');

src/main.ts

+29-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,14 @@ 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-root', goEnvJson['GOROOT']);
98+
core.setOutput('go-cache', goEnvJson['GOCACHE']);
99+
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
100+
core.setOutput('go-env', goEnvJson);
89101

90102
core.startGroup('go env');
91-
const goEnv = (cp.execSync(`${goPath} env`) || '').toString();
92103
core.info(goEnv);
93104
core.endGroup();
94105
} catch (error) {
@@ -135,6 +146,18 @@ export function parseGoVersion(versionString: string): string {
135146
return versionString.split(' ')[2].slice('go'.length);
136147
}
137148

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

0 commit comments

Comments
 (0)