Expose go env variables as action outputs (#791)

* feature implementation - output go env

* update action.yml

* fix action.yml

---------

Co-authored-by: mahabaleshwars <147705296+mahabaleshwars@users.noreply.github.com>
This commit is contained in:
v-mahabaleshwarsandmahabaleshwars authored and GitHub committed 2026-09-18 10:05:51 -05:00
1 parent 468e940882
commit e626ada899
8 files changed
+310 -2

No files matched your search

+10 -1
View File
@@ -4,5 +4,14 @@ export enum State {
}
export enum Outputs {
CacheHit = 'cache-hit'
CacheHit = 'cache-hit',
GoPath = 'go-path',
GoBin = 'go-bin',
GoBinPath = 'go-bin-path',
GoRoot = 'go-root',
GoCache = 'go-cache',
GoModCache = 'go-mod-cache',
GoOs = 'go-os',
GoArch = 'go-arch',
GoToolDir = 'go-tool-dir'
}
+57
View File
@@ -10,6 +10,7 @@ import cp from 'child_process';
import fs from 'fs';
import os from 'os';
import {Architecture} from './types.js';
import {Outputs} from './constants.js';
export async function run() {
try {
@@ -82,6 +83,11 @@ export async function run() {
const goPath = await io.which('go');
const goVersion = (cp.execSync(`${goPath} version`) || '').toString();
const goEnvJson = readGoEnv(goPath);
if (goEnvJson) {
setGoEnvOutputs(goEnvJson);
}
if (cache && isCacheFeatureAvailable()) {
const packageManager = 'default';
@@ -119,6 +125,57 @@ export async function run() {
}
}
export function readGoEnv(goPath: string): Record<string, string> | undefined {
try {
const rawGoEnv = cp.execFileSync(goPath, ['env', '-json'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe']
});
const parsed: unknown = JSON.parse(rawGoEnv);
if (
typeof parsed !== 'object' ||
parsed === null ||
Array.isArray(parsed)
) {
throw new Error("'go env -json' did not return a JSON object");
}
return parsed as Record<string, string>;
} catch (error) {
core.info(
`Unable to read 'go env -json', the Go environment outputs will not be set: ${
(error as Error).message
}`
);
return undefined;
}
}
const goEnvOutputs: ReadonlyArray<[Outputs, string]> = [
[Outputs.GoPath, 'GOPATH'],
[Outputs.GoBin, 'GOBIN'],
[Outputs.GoRoot, 'GOROOT'],
[Outputs.GoCache, 'GOCACHE'],
[Outputs.GoModCache, 'GOMODCACHE'],
[Outputs.GoOs, 'GOOS'],
[Outputs.GoArch, 'GOARCH'],
[Outputs.GoToolDir, 'GOTOOLDIR']
];
export function setGoEnvOutputs(goEnv: Record<string, string>): void {
for (const [output, variable] of goEnvOutputs) {
core.setOutput(output, goEnv[variable] ?? '');
}
core.setOutput(Outputs.GoBinPath, goEnv['GOBIN'] || goPathBin(goEnv));
}
function goPathBin(goEnv: Record<string, string>): string {
const goPath = (goEnv['GOPATH'] ?? '').split(path.delimiter)[0];
return goPath ? path.join(goPath, 'bin') : '';
}
export async function addBinToPath(): Promise<boolean> {
let added = false;
const g = await io.which('go');