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
This commit is contained in:
Luca Comellini 2023-02-15 16:16:09 -08:00
parent a3d889c34c
commit 50ed7a28b5
No known key found for this signature in database
GPG key ID: 4850B36E838507C6
5 changed files with 97 additions and 34 deletions

24
.github/workflows/outputs.yml vendored Normal file
View file

@ -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 }}'

View file

@ -124,9 +124,27 @@ describe('setup-go', () => {
jest.restoreAllMocks();
}, 100000);
it('can extract the major.minor.patch version from a given Go version string', async () => {
const goVersionOutput = 'go version go1.16.6 darwin/amd64';
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 () => {

View file

@ -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:

35
dist/setup/index.js vendored
View file

@ -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.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,25 @@ function run() {
core.info(`Successfully set up Go version ${versionSpec}`);
}
let goPath = yield io.which('go');
let goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString();
let goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
let goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
let goVersion = goEnvJson['GOVERSION'].replace('go', '');
core.info(`go version ${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(goVersion, 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', goVersion);
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();
}
@ -63656,14 +63661,16 @@ function addBinToPath() {
});
}
exports.addBinToPath = addBinToPath;
function parseGoVersion(versionString) {
// get the installed version as an Action output
// based on go/src/cmd/go/internal/version/version.go:
// fmt.Printf("go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
// expecting go<version> for runtime.Version()
return versionString.split(' ')[2].slice('go'.length);
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.parseGoVersion = parseGoVersion;
exports.convertEnvStringToJson = convertEnvStringToJson;
function resolveVersionInput() {
let version = core.getInput('go-version');
const versionFilePath = core.getInput('go-version-file');

View file

@ -57,29 +57,29 @@ export async function run() {
}
let goPath = await io.which('go');
let goVersion = (cp.execSync(`${goPath} version`) || '').toString();
let goEnv = (cp.execSync(`${goPath} env`) || '').toString();
let goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
let goVersion = goEnvJson['GOVERSION'].replace('go', '');
core.info(`go version ${goVersion}`);
if (cache && isCacheFeatureAvailable()) {
const packageManager = 'default';
const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(
parseGoVersion(goVersion),
packageManager,
cacheDependencyPath
);
await restoreCache(goVersion, 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', goVersion);
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) {
@ -118,12 +118,16 @@ export async function addBinToPath(): Promise<boolean> {
return added;
}
export function parseGoVersion(versionString: string): string {
// get the installed version as an Action output
// based on go/src/cmd/go/internal/version/version.go:
// fmt.Printf("go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
// expecting go<version> for runtime.Version()
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 {