diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 79683ecaf..12c711e3f 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -50,6 +50,22 @@ jobs: - name: Verify Go run: go version + go-version-file: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + steps: + - uses: actions/checkout@v3 + - name: Setup Go and check latest + uses: ./ + with: + go-version-file: __tests__/data/go.mod + - name: verify go + run: __tests__/verify-go.sh 1.14 + shell: bash + setup-versions-from-manifest: name: Setup ${{ matrix.go }} ${{ matrix.os }} runs-on: ${{ matrix.os }} diff --git a/README.md b/README.md index f73ff49de..3b0da7b95 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,21 @@ steps: cache: true cache-dependency-path: subdir/go.sum - run: go run hello.go + ``` +## Getting go version from the go.mod file + +The `go-version-file` input accepts a path to a `go.mod` file containing 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 [version-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go servers. + +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 + +```yaml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-go@v3 + with: + go-version-file: 'path/to/go.mod' +- run: go version ``` ## Matrix testing diff --git a/__tests__/data/go.mod b/__tests__/data/go.mod new file mode 100644 index 000000000..dea3480ee --- /dev/null +++ b/__tests__/data/go.mod @@ -0,0 +1,12 @@ +module example.com/mymodule + +go 1.14 + +require ( + example.com/othermodule v1.2.3 + example.com/thismodule v1.2.3 + example.com/thatmodule v1.2.3 +) + +replace example.com/thatmodule => ../thatmodule +exclude example.com/thismodule v1.3.0 diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 40be08076..13370e69e 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -33,6 +33,7 @@ describe('setup-go', () => { let dbgSpy: jest.SpyInstance; let whichSpy: jest.SpyInstance; let existsSpy: jest.SpyInstance; + let readFileSpy: jest.SpyInstance; let mkdirpSpy: jest.SpyInstance; let execSpy: jest.SpyInstance; let getManifestSpy: jest.SpyInstance; @@ -71,6 +72,7 @@ describe('setup-go', () => { // io whichSpy = jest.spyOn(io, 'which'); existsSpy = jest.spyOn(fs, 'existsSync'); + readFileSpy = jest.spyOn(fs, 'readFileSync'); mkdirpSpy = jest.spyOn(io, 'mkdirP'); // gets @@ -774,4 +776,68 @@ describe('setup-go', () => { expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); }); }); + + describe('go-version-file', () => { + const goModContents = `module example.com/mymodule + +go 1.14 + +require ( + example.com/othermodule v1.2.3 + example.com/thismodule v1.2.3 + example.com/thatmodule v1.2.3 +) + +replace example.com/thatmodule => ../thatmodule +exclude example.com/thismodule v1.3.0 +`; + + it('reads version from go.mod', async () => { + inputs['go-version-file'] = 'go.mod'; + existsSpy.mockImplementation(path => true); + readFileSpy.mockImplementation(() => Buffer.from(goModContents)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.14'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.14...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.14...'); + }); + + it('reads version from .go-version', async () => { + inputs['go-version-file'] = '.go-version'; + existsSpy.mockImplementation(path => true); + readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.0'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.0...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.13.0...'); + }); + + it('is overwritten by go-version', async () => { + inputs['go-version'] = '1.13.1'; + inputs['go-version-file'] = 'go.mod'; + existsSpy.mockImplementation(path => true); + readFileSpy.mockImplementation(() => Buffer.from(goModContents)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.1'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.1...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.13.1...'); + }); + + it('reports a read failure', async () => { + inputs['go-version-file'] = 'go.mod'; + existsSpy.mockImplementation(path => false); + + await main.run(); + + expect(cnSpy).toHaveBeenCalledWith( + `::error::The specified go version file at: go.mod does not exist${osm.EOL}` + ); + }); + }); }); diff --git a/action.yml b/action.yml index 4d99467f5..a8d7548f1 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,8 @@ author: 'GitHub' inputs: go-version: description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.' + go-version-file: + description: 'Path to the go.mod file.' check-latest: description: 'Set this option to true if you want the action to always check for the latest available version that satisfies the version spec' default: false diff --git a/package-lock.json b/package-lock.json index 934240e81..469fb81d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "@types/jest": "^27.0.2", "@types/node": "^16.11.25", "@types/semver": "^6.0.0", - "@zeit/ncc": "^0.21.0", + "@vercel/ncc": "^0.33.4", "jest": "^27.2.5", "jest-circus": "^27.2.5", "nock": "^10.0.6", @@ -1355,11 +1355,10 @@ "integrity": "sha512-iO9ZQHkZxHn4mSakYV0vFHAVDyEOIJQrV2uZ06HxEPcx+mt8swXoZHIbaaJ2crJYFfErySgktuTZ3BeLz+XmFA==", "dev": true }, - "node_modules/@zeit/ncc": { - "version": "0.21.1", - "resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.21.1.tgz", - "integrity": "sha512-M9WzgquSOt2nsjRkYM9LRylBLmmlwNCwYbm3Up3PDEshfvdmIfqpFNSK8EJvR18NwZjGHE5z2avlDtYQx2JQnw==", - "deprecated": "@zeit/ncc is no longer maintained. Please use @vercel/ncc instead.", + "node_modules/@vercel/ncc": { + "version": "0.33.4", + "resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.33.4.tgz", + "integrity": "sha512-ln18hs7dMffelP47tpkaR+V5Tj6coykNyxJrlcmCormPqRQjB/Gv4cu2FfBG+PMzIfdZp2CLDsrrB1NPU22Qhg==", "dev": true, "bin": { "ncc": "dist/ncc/cli.js" @@ -6053,10 +6052,10 @@ "integrity": "sha512-iO9ZQHkZxHn4mSakYV0vFHAVDyEOIJQrV2uZ06HxEPcx+mt8swXoZHIbaaJ2crJYFfErySgktuTZ3BeLz+XmFA==", "dev": true }, - "@zeit/ncc": { - "version": "0.21.1", - "resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.21.1.tgz", - "integrity": "sha512-M9WzgquSOt2nsjRkYM9LRylBLmmlwNCwYbm3Up3PDEshfvdmIfqpFNSK8EJvR18NwZjGHE5z2avlDtYQx2JQnw==", + "@vercel/ncc": { + "version": "0.33.4", + "resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.33.4.tgz", + "integrity": "sha512-ln18hs7dMffelP47tpkaR+V5Tj6coykNyxJrlcmCormPqRQjB/Gv4cu2FfBG+PMzIfdZp2CLDsrrB1NPU22Qhg==", "dev": true }, "abab": { diff --git a/package.json b/package.json index 345f3af35..34e28209d 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "@types/jest": "^27.0.2", "@types/node": "^16.11.25", "@types/semver": "^6.0.0", - "@zeit/ncc": "^0.21.0", + "@vercel/ncc": "^0.33.4", "jest": "^27.2.5", "jest-circus": "^27.2.5", "nock": "^10.0.6", diff --git a/src/installer.ts b/src/installer.ts index 46eecc35a..c1a1e3959 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -4,6 +4,7 @@ import * as path from 'path'; import * as semver from 'semver'; import * as httpm from '@actions/http-client'; import * as sys from './system'; +import fs from 'fs'; import os from 'os'; type InstallationType = 'dist' | 'manifest'; @@ -298,3 +299,14 @@ export function makeSemver(version: string): string { } return fullVersion; } + +export function parseGoVersionFile(versionFilePath: string): string { + const contents = fs.readFileSync(versionFilePath).toString(); + + if (path.basename(versionFilePath) === 'go.mod') { + const match = contents.match(/^go (\d+(\.\d+)*)/m); + return match ? match[1] : ''; + } + + return contents.trim(); +} diff --git a/src/main.ts b/src/main.ts index 881d4c171..0649dec5b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,7 +14,7 @@ export async function run() { // versionSpec is optional. If supplied, install / use from the tool cache // If not supplied then problem matchers will still be setup. Useful for self-hosted. // - let versionSpec = core.getInput('go-version'); + const versionSpec = resolveVersionInput(); const cache = core.getBooleanInput('cache'); core.info(`Setup go version spec ${versionSpec}`); @@ -105,3 +105,29 @@ export function parseGoVersion(versionString: string): string { // expecting go for runtime.Version() return versionString.split(' ')[2].slice('go'.length); } + +function resolveVersionInput(): string { + let version = core.getInput('go-version'); + const versionFilePath = core.getInput('go-version-file'); + + if (version && versionFilePath) { + core.warning( + 'Both go-version and go-version-file inputs are specified, only go-version will be used' + ); + } + + if (version) { + return version; + } + + if (versionFilePath) { + if (!fs.existsSync(versionFilePath)) { + throw new Error( + `The specified go version file at: ${versionFilePath} does not exist` + ); + } + version = installer.parseGoVersionFile(versionFilePath); + } + + return version; +}