mirror of https://github.com/actions/setup-go
Compare commits
3 Commits
e6f6fc5200
...
675c62e421
Author | SHA1 | Date |
---|---|---|
Luca Comellini | 675c62e421 | |
Masahiro Furudate | cdcb360436 | |
Luca Comellini | 98edb4ffc7 |
|
@ -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 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 GOCACHE=${{ steps.setup-go.outputs.go-cache }}
|
||||
|
||||
echo Go environment variables json:
|
||||
jq . <<< '${{ steps.setup-go.outputs.go-env }}'
|
15
README.md
15
README.md
|
@ -70,9 +70,10 @@ steps:
|
|||
>
|
||||
> ```yaml
|
||||
> go-version: '1.20'
|
||||
> ```
|
||||
> ```
|
||||
>
|
||||
> The recommendation is based on the YAML parser's behavior, which interprets non-wrapped values as numbers and, in the case of version 1.20, trims it down to 1.2, which may not be very obvious.
|
||||
|
||||
Matching an unstable pre-release:
|
||||
|
||||
```yaml
|
||||
|
@ -190,11 +191,13 @@ steps:
|
|||
|
||||
## Getting go version from the go.mod file
|
||||
|
||||
The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains the version of Go to be
|
||||
used by a project. As the `go.mod` file contains only major and minor (e.g. 1.18) tags, the action will search for the
|
||||
latest available patch version sequentially in the runner's directory with the cached tools, in
|
||||
the [versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go
|
||||
servers.
|
||||
The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains the version of Go to be used by a project.
|
||||
|
||||
The `go` directive in `go.mod` can specify a patch version or omit it altogether (e.g., `go 1.22.0` or `go 1.22`).
|
||||
If a patch version is specified, that specific patch version will be used.
|
||||
If no patch version is specified, it will search for the latest available patch version in the cache,
|
||||
[versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json), and the
|
||||
[official Go language website](https://golang.org/dl/?mode=json&include=all), in that order.
|
||||
|
||||
If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used.
|
||||
> The action will search for the `go.mod` file relative to the repository root
|
||||
|
|
|
@ -138,6 +138,29 @@ describe('setup-go', () => {
|
|||
expect(main.parseGoVersion(goVersionOutput)).toBe('1.16.6');
|
||||
});
|
||||
|
||||
it('can read go env variables', async () => {
|
||||
const goRoot = '/opt/hostedtoolcache/go/1.18.10/x64';
|
||||
const goPath = '/home/runner/go';
|
||||
const goModCache = '/home/runner/go/pkg/mod';
|
||||
const goCache = '/home/runner/.cache/go-build';
|
||||
const goVersion = 'go1.18.10';
|
||||
|
||||
const env = `
|
||||
GOROOT="${goRoot}"
|
||||
GOPATH="${goPath}"
|
||||
GOMODCACHE="${goModCache}"
|
||||
GOCACHE="${goCache}"
|
||||
GOVERSION="${goVersion}"
|
||||
`;
|
||||
const 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 () => {
|
||||
os.platform = 'darwin';
|
||||
os.arch = 'x64';
|
||||
|
|
12
action.yml
12
action.yml
|
@ -22,6 +22,18 @@ inputs:
|
|||
outputs:
|
||||
go-version:
|
||||
description: 'The installed Go version. Useful when given a version range as input.'
|
||||
go-cache:
|
||||
description: 'The GOCACHE environment variable'
|
||||
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:
|
||||
|
|
|
@ -88419,7 +88419,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.parseGoVersion = exports.addBinToPath = exports.run = void 0;
|
||||
const core = __importStar(__nccwpck_require__(2186));
|
||||
const io = __importStar(__nccwpck_require__(7436));
|
||||
const installer = __importStar(__nccwpck_require__(2574));
|
||||
|
@ -88467,11 +88467,19 @@ function run() {
|
|||
core.debug(`add bin ${added}`);
|
||||
const goPath = yield io.which('go');
|
||||
const goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString();
|
||||
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
|
||||
const goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
|
||||
const parsedGoVersion = parseGoVersion(goVersion);
|
||||
// Go versions less that 1.16 do not have the GOVERSION environment variable
|
||||
if (semver.lt(parsedGoVersion, '1.16.0')) {
|
||||
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
|
||||
}
|
||||
core.info(goVersion);
|
||||
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
|
||||
const packageManager = 'default';
|
||||
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
||||
try {
|
||||
yield (0, cache_restore_1.restoreCache)(parseGoVersion(goVersion), packageManager, cacheDependencyPath);
|
||||
yield (0, cache_restore_1.restoreCache)(parsedGoVersion, packageManager, cacheDependencyPath);
|
||||
}
|
||||
catch (error) {
|
||||
core.warning(`Restore cache failed: ${error.message}`);
|
||||
|
@ -88480,11 +88488,12 @@ function run() {
|
|||
// 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', parsedGoVersion);
|
||||
core.setOutput('go-path', goEnvJson['GOPATH']);
|
||||
core.setOutput('go-cache', goEnvJson['GOCACHE']);
|
||||
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
|
||||
core.setOutput('go-env', goEnvJson);
|
||||
core.startGroup('go env');
|
||||
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
|
||||
core.info(goEnv);
|
||||
core.endGroup();
|
||||
}
|
||||
|
@ -88532,6 +88541,16 @@ function parseGoVersion(versionString) {
|
|||
return versionString.split(' ')[2].slice('go'.length);
|
||||
}
|
||||
exports.parseGoVersion = parseGoVersion;
|
||||
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.convertEnvStringToJson = convertEnvStringToJson;
|
||||
function resolveVersionInput() {
|
||||
let version = core.getInput('go-version');
|
||||
const versionFilePath = core.getInput('go-version-file');
|
||||
|
|
|
@ -6,13 +6,13 @@ We have prepared a short guide so that the process of making your contribution i
|
|||
|
||||
## How can I contribute...
|
||||
|
||||
* [Contribute Documentation:green_book:](#contribute-documentation)
|
||||
* [Contribute Documentation :green_book:](#contribute-documentation)
|
||||
|
||||
* [Contribute Code :computer:](#contribute-code)
|
||||
|
||||
* [Provide Support on Issues:pencil:](#provide-support-on-issues)
|
||||
* [Provide Support on Issues :pencil:](#provide-support-on-issues)
|
||||
|
||||
* [Review Pull Requests:mag:](#review-pull-requests)
|
||||
* [Review Pull Requests :mag:](#review-pull-requests)
|
||||
|
||||
## Contribute documentation
|
||||
|
||||
|
@ -113,4 +113,4 @@ Another great way to contribute is pull request reviews. Please, be extra kind:
|
|||
- Make sure you're familiar with the code or documentation is updated, unless it's a minor change (spellchecking, minor formatting, etc.)
|
||||
- Review changes using the GitHub functionality. You can ask a clarifying question, point out an error or suggest an alternative.
|
||||
> Note: You may ask for minor changes - "nitpicks", but consider whether they are real blockers to merging or not
|
||||
- Submit your review, which may include comments, an approval, or a changes request
|
||||
- Submit your review, which may include comments, an approval, or a changes request
|
||||
|
|
34
src/main.ts
34
src/main.ts
|
@ -63,13 +63,23 @@ export async function run() {
|
|||
|
||||
const goPath = await io.which('go');
|
||||
const goVersion = (cp.execSync(`${goPath} version`) || '').toString();
|
||||
const goEnv = (cp.execSync(`${goPath} env`) || '').toString();
|
||||
const goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
|
||||
const parsedGoVersion = parseGoVersion(goVersion);
|
||||
|
||||
// Go versions less that 1.16 do not have the GOVERSION environment variable
|
||||
if (semver.lt(parsedGoVersion, '1.16.0')) {
|
||||
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
|
||||
}
|
||||
|
||||
core.info(goVersion);
|
||||
|
||||
if (cache && isCacheFeatureAvailable()) {
|
||||
const packageManager = 'default';
|
||||
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
||||
try {
|
||||
await restoreCache(
|
||||
parseGoVersion(goVersion),
|
||||
parsedGoVersion,
|
||||
packageManager,
|
||||
cacheDependencyPath
|
||||
);
|
||||
|
@ -82,13 +92,13 @@ export async function run() {
|
|||
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', parsedGoVersion);
|
||||
core.setOutput('go-path', goEnvJson['GOPATH']);
|
||||
core.setOutput('go-cache', goEnvJson['GOCACHE']);
|
||||
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
|
||||
core.setOutput('go-env', goEnvJson);
|
||||
|
||||
core.startGroup('go env');
|
||||
const goEnv = (cp.execSync(`${goPath} env`) || '').toString();
|
||||
core.info(goEnv);
|
||||
core.endGroup();
|
||||
} catch (error) {
|
||||
|
@ -135,6 +145,18 @@ export function parseGoVersion(versionString: string): string {
|
|||
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 {
|
||||
let version = core.getInput('go-version');
|
||||
const versionFilePath = core.getInput('go-version-file');
|
||||
|
|
Loading…
Reference in New Issue