version-file takes precedence

This commit is contained in:
Alexis Couvreur 2026-03-01 12:35:28 -05:00
parent 9307d0b584
commit a993e13330
5 changed files with 45 additions and 47 deletions

View file

@ -198,7 +198,7 @@ Gets the version of GoReleaser to use from a file.
The path must be relative to the root of the project, or the `workdir` if defined.
This parameter supports `.tool-versions` files.
This parameter only supports `.tool-versions` files for now.
<details>
<summary>Example</summary>

View file

@ -34,33 +34,7 @@ describe('getRequestedVersion', () => {
expect(v).toBe('v1.2.3');
});
it('warns when both version and version-file are provided and returns version', () => {
const inputs: Inputs = {
distribution: 'goreleaser',
version: 'v9.9.9',
versionFile: '.tool-versions',
args: '',
workdir: '',
installOnly: false
};
const v = getRequestedVersion(inputs);
expect(v).toBe('v9.9.9');
});
it('throws when version is empty and version-file does not exist', () => {
const inputs: Inputs = {
distribution: 'goreleaser',
version: '',
versionFile: 'nonexistent-file',
args: '',
workdir: '',
installOnly: false
};
expect(() => getRequestedVersion(inputs)).toThrow();
});
it('parses .tool-versions and returns v-prefixed version', () => {
const toolVersionsPath = path.join(tmpDir, '.tool-versions');
fs.writeFileSync(toolVersionsPath, 'goreleaser 1.2.3\n');
@ -78,17 +52,50 @@ describe('getRequestedVersion', () => {
expect(v).toBe('v1.2.3');
});
it('defaults to ~> v2 when nothing provided', () => {
it('when both version and version-file are provided, version-file takes precedence', () => {
const toolVersionsPath = path.join(tmpDir, '.tool-versions');
fs.writeFileSync(toolVersionsPath, 'goreleaser 1.2.3\n');
const inputs: Inputs = {
distribution: 'goreleaser',
version: 'v9.9.9',
versionFile: '.tool-versions',
args: '',
workdir: tmpDir,
installOnly: false
};
const v = getRequestedVersion(inputs);
expect(v).toBe('v1.2.3');
});
it('throws when version-file does not exist', () => {
const inputs: Inputs = {
distribution: 'goreleaser',
version: '',
versionFile: '',
versionFile: 'nonexistent-file',
args: '',
workdir: '',
installOnly: false
};
const v = getRequestedVersion(inputs);
expect(v).toBe('~> v2');
expect(() => getRequestedVersion(inputs)).toThrow();
});
it('throws when version-file is an unsupported type', () => {
const toolVersionsPath = path.join(tmpDir, 'unsupported-file');
fs.writeFileSync(toolVersionsPath, 'goreleaser 1.2.3\n');
const inputs: Inputs = {
distribution: 'goreleaser',
version: '',
versionFile: 'unsupported-file',
args: '',
workdir: tmpDir,
installOnly: false
};
expect(() => getRequestedVersion(inputs)).toThrow();
});
});

View file

@ -13,13 +13,12 @@ inputs:
required: false
version:
description: 'GoReleaser version'
required: false
default: '~> v2'
version-file:
description: |
Gets the version of GoReleaser to use from a file.
The path must be relative to the root of the project, or the `workdir` if defined.
This parameter supports `.tool-versions` files.
Only works with `install-mode: binary` (the default).
required: false
args:
description: 'Arguments to pass to GoReleaser'

View file

@ -16,7 +16,7 @@ export interface Inputs {
export async function getInputs(): Promise<Inputs> {
return {
distribution: core.getInput('distribution') || 'goreleaser',
version: core.getInput('version'),
version: core.getInput('version') || '~> v2',
versionFile: core.getInput('version-file'),
args: core.getInput('args'),
workdir: core.getInput('workdir') || '.',

View file

@ -7,13 +7,8 @@ export function getRequestedVersion(inputs: Inputs): string {
let requestedVersion = inputs.version;
let versionFilePath = inputs.versionFile;
if (requestedVersion && versionFilePath) {
core.warning(
`Both version (${requestedVersion}) and version-file (${versionFilePath}) inputs are specified, only version will be used`
);
}
if (requestedVersion == '' && versionFilePath) {
// If versionFile is provided, it takes precedence over version input.
if (versionFilePath) {
const workingDirectory = inputs.workdir;
if (workingDirectory) {
versionFilePath = path.join(workingDirectory, versionFilePath);
@ -29,13 +24,10 @@ export function getRequestedVersion(inputs: Inputs): string {
// asdf/mise file.
const match = content.match(/^goreleaser\s+([^\n#]+)/m);
requestedVersion = match ? 'v' + match[1].trim().replace(/^v/gi, '') : '';
} else {
throw new Error(`Unsupported version file: ${versionFilePath}. Only .tool-versions files are supported.`);
}
}
if (!requestedVersion) {
// default to latest v2 release
requestedVersion = '~> v2';
}
return requestedVersion;
}