Implement "check-latest" flag to check if pre-cached version is latest one (#186)

This commit is contained in:
Dmitry Shibanov 2022-02-09 14:59:04 +03:00 committed by GitHub
parent 44e221478f
commit bfdd3570ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 4778 additions and 3598 deletions

View file

@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v2
- name: Set Node.js 12.x
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: 12.x

View file

@ -18,7 +18,7 @@ jobs:
- name: Install licensed
run: |
cd $RUNNER_TEMP
curl -Lfs -o licensed.tar.gz https://github.com/github/licensed/releases/download/2.12.2/licensed-2.12.2-linux-x64.tar.gz
curl -Lfs -o licensed.tar.gz https://github.com/github/licensed/releases/download/3.3.1/licensed-3.3.1-linux-x64.tar.gz
sudo tar -xzf licensed.tar.gz
sudo mv licensed /usr/local/bin/licensed
- run: licensed status

View file

@ -33,6 +33,23 @@ jobs:
run: __tests__/verify-go.sh ${{ matrix.go }}
shell: bash
check-latest:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
go-version: [1.16, 1.17]
steps:
- uses: actions/checkout@v2
- name: Setup Go and check latest
uses: ./
with:
go-version: ${{ matrix.go-version }}
check-latest: true
- name: Verify Go
run: go version
setup-versions-from-manifest:
name: Setup ${{ matrix.go }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}

View file

@ -25,8 +25,8 @@ jobs:
node-version: 12
cache: npm
- name: npm install
run: npm install
- name: npm ci
run: npm ci
- name: Lint
run: npm run format-check

Binary file not shown.

Binary file not shown.

View file

@ -16,10 +16,11 @@ This action sets up a go environment for use in actions by:
The V2 offers:
- Adds GOBIN to the PATH
- Proxy Support
- stable input
- `stable` input
- Check latest version
- Bug Fixes (including issues around version matching and semver)
It will first check the local cache for a version match. If version is not found locally, It will pull it from `main` branch of [go-versions](https://github.com/actions/go-versions/blob/main/versions-manifest.json) repository and on miss or failure, it will fall back to the previous behavior of download directly from [go dist](https://storage.googleapis.com/golang).
The action will first check the local cache for a version match. If a version is not found locally, it will pull it from the `main` branch of the [go-versions](https://github.com/actions/go-versions/blob/main/versions-manifest.json) repository. On miss or failure, it will fall back to downloading directly from [go dist](https://storage.googleapis.com/golang). To change the default behavior, please use the [check-latest input](#check-latest-version).
Matching by [semver spec](https://github.com/npm/node-semver):
```yaml
@ -46,17 +47,36 @@ steps:
See [action.yml](action.yml)
Basic:
## Basic:
```yaml
steps:
- uses: actions/checkout@master
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.9.3' # The Go version to download (if necessary) and use.
go-version: '1.16.1' # The Go version to download (if necessary) and use.
- run: go run hello.go
```
Matrix Testing:
## Check latest version:
The `check-latest` flag defaults to `false`. Use the default or set `check-latest` to `false` if you prefer stability and if you want to ensure a specific Go version is always used.
If `check-latest` is set to `true`, the action first checks if the cached version is the latest one. If the locally cached version is not the most up-to-date, a Go version will then be downloaded. Set `check-latest` to `true` if you want the most up-to-date Go version to always be used.
> Setting `check-latest` to `true` has performance implications as downloading Go versions is slower than using cached versions.
```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.14'
check-latest: true
- run: go run hello.go
```
## Matrix Testing:
```yaml
jobs:
build:

View file

@ -1,4 +1,29 @@
[
{
"version": "1.17.6",
"stable": true,
"release_url": "https://github.com/actions/go-versions/releases/tag/1.17.6-1668090892",
"files": [
{
"filename": "go-1.17.6-darwin-x64.tar.gz",
"arch": "x64",
"platform": "darwin",
"download_url": "https://github.com/actions/go-versions/releases/download/1.17.6-1668090892/go-1.17.6-darwin-x64.tar.gz"
},
{
"filename": "go-1.17.6-linux-x64.tar.gz",
"arch": "x64",
"platform": "linux",
"download_url": "https://github.com/actions/go-versions/releases/download/1.17.6-1668090892/go-1.17.6-linux-x64.tar.gz"
},
{
"filename": "go-1.17.6-win32-x64.zip",
"arch": "x64",
"platform": "win32",
"download_url": "https://github.com/actions/go-versions/releases/download/1.17.6-1668090892/go-1.17.6-win32-x64.zip"
}
]
},
{
"version": "1.12.17",
"stable": true,

View file

@ -19,6 +19,7 @@ describe('setup-go', () => {
let os = {} as any;
let inSpy: jest.SpyInstance;
let getBooleanInputSpy: jest.SpyInstance;
let findSpy: jest.SpyInstance;
let cnSpy: jest.SpyInstance;
let logSpy: jest.SpyInstance;
@ -35,16 +36,19 @@ describe('setup-go', () => {
let execSpy: jest.SpyInstance;
let getManifestSpy: jest.SpyInstance;
beforeAll(() => {
process.env['GITHUB_PATH'] = ''; // Stub out ENV file functionality so we can verify it writes to standard out
console.log('::stop-commands::stoptoken'); // Disable executing of runner commands when running tests in actions
});
beforeAll(async () => {
process.env['GITHUB_ENV'] = ''; // Stub out Environment file functionality so we can verify it writes to standard out (toolkit is backwards compatible)
}, 100000);
beforeEach(() => {
process.env['GITHUB_PATH'] = ''; // Stub out ENV file functionality so we can verify it writes to standard out
// @actions/core
inputs = {};
inSpy = jest.spyOn(core, 'getInput');
inSpy.mockImplementation(name => inputs[name]);
getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
getBooleanInputSpy.mockImplementation(name => inputs[name]);
// node
os = {};
@ -81,7 +85,7 @@ describe('setup-go', () => {
});
logSpy.mockImplementation(line => {
// uncomment to debug
// process.stderr.write('log:' + line + '\n');
process.stderr.write('log:' + line + '\n');
});
dbgSpy.mockImplementation(msg => {
// uncomment to see debug output
@ -96,7 +100,7 @@ describe('setup-go', () => {
});
afterAll(async () => {
console.log('::stoptoken::'); // Re-enable executing of runner commands when running tests in actions
jest.restoreAllMocks();
}, 100000);
it('can find 1.9.7 from manifest on osx', async () => {
@ -299,7 +303,6 @@ describe('setup-go', () => {
os.platform = 'linux';
os.arch = 'x64';
// a version which is in the manifest
let versionSpec = '1.12.16';
inputs['go-version'] = versionSpec;
@ -337,7 +340,6 @@ describe('setup-go', () => {
os.platform = 'linux';
os.arch = 'x64';
// a version which is in the manifest
let versionSpec = '1.12';
inputs['go-version'] = versionSpec;
@ -375,7 +377,6 @@ describe('setup-go', () => {
os.platform = 'linux';
os.arch = 'x64';
// a version which is not in the manifest but is in node dist
let versionSpec = '1.12.14';
inputs['go-version'] = versionSpec;
@ -573,4 +574,166 @@ describe('setup-go', () => {
it('does not convert exact versions', async () => {
expect(im.makeSemver('1.13.1')).toBe('1.13.1');
});
describe('check-latest flag', () => {
it("use local version and don't check manifest if check-latest is not specified", async () => {
os.platform = 'linux';
os.arch = 'x64';
inputs['go-version'] = '1.16';
inputs['check-latest'] = false;
const toolPath = path.normalize('/cache/go/1.16.1/x64');
findSpy.mockReturnValue(toolPath);
await main.run();
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
expect(logSpy).not.toHaveBeenCalledWith(
'Attempting to resolve the latest version from the manifest...'
);
});
it('check latest version and resolve it from local cache', async () => {
os.platform = 'linux';
os.arch = 'x64';
inputs['go-version'] = '1.16';
inputs['check-latest'] = true;
const toolPath = path.normalize('/cache/go/1.16.1/x64');
findSpy.mockReturnValue(toolPath);
dlSpy.mockImplementation(async () => '/some/temp/path');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);
await main.run();
expect(logSpy).toHaveBeenCalledWith('Setup go stable version spec 1.16');
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
});
it('check latest version and install it from manifest', async () => {
os.platform = 'linux';
os.arch = 'x64';
const versionSpec = '1.17';
const patchVersion = '1.17.6';
inputs['go-version'] = versionSpec;
inputs['stable'] = 'true';
inputs['check-latest'] = true;
findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(async () => '/some/temp/path');
const toolPath = path.normalize('/cache/go/1.17.5/x64');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);
const expectedUrl =
'https://github.com/actions/go-versions/releases/download/1.17.6-1668090892/go-1.17.6-darwin-x64.tar.gz';
await main.run();
expect(logSpy).toHaveBeenCalledWith(
`Setup go stable version spec ${versionSpec}`
);
expect(logSpy).toHaveBeenCalledWith(
'Attempting to resolve the latest version from the manifest...'
);
expect(logSpy).toHaveBeenCalledWith(`Resolved as '${patchVersion}'`);
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${patchVersion}...`
);
expect(logSpy).toHaveBeenCalledWith('Extracting Go...');
expect(logSpy).toHaveBeenCalledWith('Adding to the cache ...');
expect(logSpy).toHaveBeenCalledWith('Added go to the path');
expect(logSpy).toHaveBeenCalledWith(
`Successfully setup go version ${versionSpec}`
);
});
it('fallback to dist if version is not found in manifest', async () => {
os.platform = 'linux';
os.arch = 'x64';
let versionSpec = '1.13';
inputs['go-version'] = versionSpec;
inputs['check-latest'] = true;
inputs['always-auth'] = false;
inputs['token'] = 'faketoken';
// ... but not in the local cache
findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(async () => '/some/temp/path');
let toolPath = path.normalize('/cache/go/1.13.7/x64');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);
await main.run();
let expPath = path.join(toolPath, 'bin');
expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(
'Attempting to resolve the latest version from the manifest...'
);
expect(logSpy).toHaveBeenCalledWith(
`Failed to resolve version ${versionSpec} from manifest`
);
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${versionSpec}...`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});
it('fallback to dist if manifest is not available', async () => {
os.platform = 'linux';
os.arch = 'x64';
let versionSpec = '1.13';
process.env['GITHUB_PATH'] = '';
inputs['go-version'] = versionSpec;
inputs['check-latest'] = true;
inputs['always-auth'] = false;
inputs['token'] = 'faketoken';
// ... but not in the local cache
findSpy.mockImplementation(() => '');
getManifestSpy.mockImplementation(() => {
throw new Error('Unable to download manifest');
});
dlSpy.mockImplementation(async () => '/some/temp/path');
let toolPath = path.normalize('/cache/go/1.13.7/x64');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);
await main.run();
let expPath = path.join(toolPath, 'bin');
expect(logSpy).toHaveBeenCalledWith(
`Failed to resolve version ${versionSpec} from manifest`
);
expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(
'Attempting to resolve the latest version from the manifest...'
);
expect(logSpy).toHaveBeenCalledWith(
'Unable to resolve a version from the manifest...'
);
expect(logSpy).toHaveBeenCalledWith(
`Failed to resolve version ${versionSpec} from manifest`
);
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${versionSpec}...`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
});
});
});

View file

@ -4,6 +4,9 @@ author: 'GitHub'
inputs:
go-version:
description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.'
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
stable:
description: 'Whether to download only stable versions'
default: 'true'

947
dist/index.js vendored

File diff suppressed because it is too large Load diff

7117
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -23,7 +23,7 @@
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/core": "^1.6.0",
"@actions/http-client": "^1.0.6",
"@actions/io": "^1.0.2",
"@actions/tool-cache": "^1.5.5",

View file

@ -31,11 +31,27 @@ export interface IGoVersionInfo {
export async function getGo(
versionSpec: string,
stable: boolean,
checkLatest: boolean,
auth: string | undefined
) {
let osPlat: string = os.platform();
let osArch: string = os.arch();
if (checkLatest) {
core.info('Attempting to resolve the latest version from the manifest...');
const resolvedVersion = await resolveVersionFromManifest(
versionSpec,
stable,
auth
);
if (resolvedVersion) {
versionSpec = resolvedVersion;
core.info(`Resolved as '${versionSpec}'`);
} else {
core.info(`Failed to resolve version ${versionSpec} from manifest`);
}
}
// check cache
let toolPath: string;
toolPath = tc.find('go', versionSpec);
@ -97,6 +113,20 @@ export async function getGo(
return downloadPath;
}
async function resolveVersionFromManifest(
versionSpec: string,
stable: boolean,
auth: string | undefined
): Promise<string | undefined> {
try {
const info = await getInfoFromManifest(versionSpec, stable, auth);
return info?.resolvedVersion;
} catch (err) {
core.info('Unable to resolve a version from the manifest...');
core.debug(err.message);
}
}
async function installGoVersion(
info: IGoVersionInfo,
auth: string | undefined

View file

@ -24,7 +24,13 @@ export async function run() {
let token = core.getInput('token');
let auth = !token || isGhes() ? undefined : `token ${token}`;
const installDir = await installer.getGo(versionSpec, stable, auth);
const checkLatest = core.getBooleanInput('check-latest');
const installDir = await installer.getGo(
versionSpec,
stable,
checkLatest,
auth
);
core.exportVariable('GOROOT', installDir);
core.addPath(path.join(installDir, 'bin'));

View file

@ -4,9 +4,6 @@
// "incremental": true, /* Enable incremental compilation */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es6"
],
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
@ -48,7 +45,8 @@
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"sourceMap": true,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */