Skip to content

Commit e5fda20

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 a3d889c commit e5fda20

File tree

5 files changed

+112
-16
lines changed

5 files changed

+112
-16
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 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+
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
@@ -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+
let goRoot = '/opt/hostedtoolcache/go/1.18.10/x64';
134+
let goPath = '/home/runner/go';
135+
let goModCache = '/home/runner/go/pkg/mod';
136+
let goCache = '/home/runner/.cache/go-build';
137+
let goVersion = 'go1.18.10';
138+
139+
let env = `
140+
GOROOT="${goRoot}"
141+
GOPATH="${goPath}"
142+
GOMODCACHE="${goModCache}"
143+
GOCACHE="${goCache}"
144+
GOVERSION="${goVersion}"
145+
`;
146+
let 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

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

dist/setup/index.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -63559,7 +63559,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6355963559
return (mod && mod.__esModule) ? mod : { "default": mod };
6356063560
};
6356163561
Object.defineProperty(exports, "__esModule", ({ value: true }));
63562-
exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
63562+
exports.convertEnvStringToJson = exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
6356363563
const core = __importStar(__nccwpck_require__(2186));
6356463564
const io = __importStar(__nccwpck_require__(7436));
6356563565
const installer = __importStar(__nccwpck_require__(2574));
@@ -63603,20 +63603,30 @@ function run() {
6360363603
core.info(`Successfully set up Go version ${versionSpec}`);
6360463604
}
6360563605
let goPath = yield io.which('go');
63606+
let goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
63607+
let goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
6360663608
let goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString();
63609+
let parsedGoVersion = parseGoVersion(goVersion);
63610+
// Go versions less that 1.16 do not have the GOVERSION environment variable
63611+
if (semver.lt(parsedGoVersion, '1.16.0')) {
63612+
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
63613+
}
63614+
core.info(goVersion);
6360763615
if (cache && cache_utils_1.isCacheFeatureAvailable()) {
6360863616
const packageManager = 'default';
6360963617
const cacheDependencyPath = core.getInput('cache-dependency-path');
63610-
yield cache_restore_1.restoreCache(parseGoVersion(goVersion), packageManager, cacheDependencyPath);
63618+
yield cache_restore_1.restoreCache(parsedGoVersion, packageManager, cacheDependencyPath);
6361163619
}
6361263620
// add problem matchers
6361363621
const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json');
6361463622
core.info(`##[add-matcher]${matchersPath}`);
63615-
// output the version actually being used
63616-
core.info(goVersion);
63617-
core.setOutput('go-version', parseGoVersion(goVersion));
63623+
core.setOutput('go-version', parsedGoVersion);
63624+
core.setOutput('go-path', goEnvJson['GOPATH']);
63625+
core.setOutput('go-root', goEnvJson['GOROOT']);
63626+
core.setOutput('go-cache', goEnvJson['GOCACHE']);
63627+
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
63628+
core.setOutput('go-env', goEnvJson);
6361863629
core.startGroup('go env');
63619-
let goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
6362063630
core.info(goEnv);
6362163631
core.endGroup();
6362263632
}
@@ -63664,6 +63674,16 @@ function parseGoVersion(versionString) {
6366463674
return versionString.split(' ')[2].slice('go'.length);
6366563675
}
6366663676
exports.parseGoVersion = parseGoVersion;
63677+
function convertEnvStringToJson(envString) {
63678+
const envArray = envString.split('\n');
63679+
const envObject = {};
63680+
envArray.forEach(envVar => {
63681+
const [key, value] = envVar.split(/=(?=")/);
63682+
envObject[key] = value === null || value === void 0 ? void 0 : value.replace(/"/g, '');
63683+
});
63684+
return JSON.stringify(envObject);
63685+
}
63686+
exports.convertEnvStringToJson = convertEnvStringToJson;
6366763687
function resolveVersionInput() {
6366863688
let version = core.getInput('go-version');
6366963689
const versionFilePath = core.getInput('go-version-file');

src/main.ts

+29-10
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,36 @@ export async function run() {
5757
}
5858

5959
let goPath = await io.which('go');
60+
let goEnv = (cp.execSync(`${goPath} env`) || '').toString();
61+
let goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
6062
let goVersion = (cp.execSync(`${goPath} version`) || '').toString();
63+
let parsedGoVersion = parseGoVersion(goVersion);
64+
65+
// Go versions less that 1.16 do not have the GOVERSION environment variable
66+
if (semver.lt(parsedGoVersion, '1.16.0')) {
67+
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
68+
}
69+
70+
core.info(goVersion);
6171

6272
if (cache && isCacheFeatureAvailable()) {
6373
const packageManager = 'default';
6474
const cacheDependencyPath = core.getInput('cache-dependency-path');
65-
await restoreCache(
66-
parseGoVersion(goVersion),
67-
packageManager,
68-
cacheDependencyPath
69-
);
75+
await restoreCache(parsedGoVersion, packageManager, cacheDependencyPath);
7076
}
7177

7278
// add problem matchers
7379
const matchersPath = path.join(__dirname, '../..', 'matchers.json');
7480
core.info(`##[add-matcher]${matchersPath}`);
7581

76-
// output the version actually being used
77-
core.info(goVersion);
78-
79-
core.setOutput('go-version', parseGoVersion(goVersion));
82+
core.setOutput('go-version', parsedGoVersion);
83+
core.setOutput('go-path', goEnvJson['GOPATH']);
84+
core.setOutput('go-root', goEnvJson['GOROOT']);
85+
core.setOutput('go-cache', goEnvJson['GOCACHE']);
86+
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
87+
core.setOutput('go-env', goEnvJson);
8088

8189
core.startGroup('go env');
82-
let goEnv = (cp.execSync(`${goPath} env`) || '').toString();
8390
core.info(goEnv);
8491
core.endGroup();
8592
} catch (error) {
@@ -126,6 +133,18 @@ export function parseGoVersion(versionString: string): string {
126133
return versionString.split(' ')[2].slice('go'.length);
127134
}
128135

136+
export function convertEnvStringToJson(envString: string): string {
137+
const envArray = envString.split('\n');
138+
const envObject: {[key: string]: string} = {};
139+
140+
envArray.forEach(envVar => {
141+
const [key, value] = envVar.split(/=(?=")/);
142+
envObject[key] = value?.replace(/"/g, '');
143+
});
144+
145+
return JSON.stringify(envObject);
146+
}
147+
129148
function resolveVersionInput(): string {
130149
let version = core.getInput('go-version');
131150
const versionFilePath = core.getInput('go-version-file');

0 commit comments

Comments
 (0)