Skip to content

Commit c51a720

Browse files
authored
Enable caching by default with default input (#332)
1 parent 6b848af commit c51a720

File tree

9 files changed

+167
-6269
lines changed

9 files changed

+167
-6269
lines changed

README.md

+66-33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ This action sets up a go environment for use in actions by:
88
- Optionally downloading and caching a version of Go by version and adding to `PATH`.
99
- Registering problem matchers for error output.
1010

11+
# V4
12+
13+
The V4 edition of the action offers:
14+
15+
- Enabled caching by default
16+
17+
The action will try to enable caching unless the `cache` input is explicitly set to false.
18+
19+
Please see "[Caching dependency files and build outputs](https://github.com/actions/setup-go#caching-dependency-files-and-build-outputs)" for more information.
20+
1121
# V3
1222

1323
The V3 edition of the action offers:
@@ -19,9 +29,14 @@ The V3 edition of the action offers:
1929
- stable and oldstable aliases
2030
- Bug Fixes (including issues around version matching and semver)
2131

22-
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).
32+
The action will first check the local cache for a version match. If a version is not found locally, it will pull it from
33+
the `main` branch of the [go-versions](https://github.com/actions/go-versions/blob/main/versions-manifest.json)
34+
repository. On miss or failure, it will fall back to downloading directly
35+
from [go dist](https://storage.googleapis.com/golang). To change the default behavior, please use
36+
the [check-latest input](#check-latest-version).
2337

24-
**Note:** The `setup-go` action uses executable binaries which are built by Golang side. The action does not build golang from source code.
38+
**Note:** The `setup-go` action uses executable binaries which are built by Golang side. The action does not build
39+
golang from source code.
2540

2641
Matching by [semver spec](https://github.com/npm/node-semver):
2742

@@ -78,14 +93,17 @@ steps:
7893
- run: go run hello.go
7994
```
8095
81-
8296
## Check latest version
8397
84-
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.
98+
The `check-latest` flag defaults to `false`. Use the default or set `check-latest` to `false` if you prefer stability
99+
and if you want to ensure a specific Go version is always used.
85100

86-
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.
101+
If `check-latest` is set to `true`, the action first checks if the cached version is the latest one. If the locally
102+
cached version is not the most up-to-date, a Go version will then be downloaded. Set `check-latest` to `true` if you
103+
want the most up-to-date Go version to always be used.
87104

88-
> Setting `check-latest` to `true` has performance implications as downloading Go versions is slower than using cached versions.
105+
> Setting `check-latest` to `true` has performance implications as downloading Go versions is slower than using cached
106+
> versions.
89107

90108
```yaml
91109
steps:
@@ -99,11 +117,14 @@ steps:
99117

100118
## Using stable/oldstable aliases
101119

102-
If `stable` is provided, action will get the latest stable version from the [`go-versions`](https://github.com/actions/go-versions/blob/main/versions-manifest.json) repository manifest.
120+
If `stable` is provided, action will get the latest stable version from
121+
the [`go-versions`](https://github.com/actions/go-versions/blob/main/versions-manifest.json) repository manifest.
103122

104-
If `oldstable` is provided, when current release is 1.19.x, action will resolve version as 1.18.x, where x is the latest patch release.
123+
If `oldstable` is provided, when current release is 1.19.x, action will resolve version as 1.18.x, where x is the latest
124+
patch release.
105125

106-
**Note:** using these aliases will result in same version as using corresponding minor release with `check-latest` input set to `true`
126+
**Note:** using these aliases will result in same version as using corresponding minor release with `check-latest` input
127+
set to `true`
107128

108129
```yaml
109130
steps:
@@ -125,48 +146,47 @@ steps:
125146

126147
## Caching dependency files and build outputs:
127148

128-
The action has a built-in functionality for caching and restoring go modules and build outputs. It uses [actions/cache](https://github.com/actions/cache) under the hood but requires less configuration settings. The `cache` input is optional, and caching is turned off by default.
149+
The action has a built-in functionality for caching and restoring go modules and build outputs. It
150+
uses [toolkit/cache](https://github.com/actions/toolkit/tree/main/packages/cache) under the hood but requires less configuration settings.
151+
The `cache` input is optional, and caching is turned on by default.
129152

130-
The action defaults to search for the dependency file - go.sum in the repository root, and uses its hash as a part of the cache key. Use `cache-dependency-path` input for cases when multiple dependency files are used, or they are located in different subdirectories.
153+
The action defaults to search for the dependency file - go.sum in the repository root, and uses its hash as a part of
154+
the cache key. Use `cache-dependency-path` input for cases when multiple dependency files are used, or they are located
155+
in different subdirectories.
131156

132-
**Caching without specifying dependency file path**
133-
```yaml
134-
steps:
135-
- uses: actions/checkout@v3
136-
- uses: actions/setup-go@v3
137-
with:
138-
go-version: '1.17'
139-
check-latest: true
140-
cache: true
141-
- run: go run hello.go
142-
```
157+
If some problem that prevents success caching happens then the action issues the warning in the log and continues the execution of the pipeline.
143158

144159
**Caching in monorepos**
160+
145161
```yaml
146162
steps:
147163
- uses: actions/checkout@v3
148164
- uses: actions/setup-go@v3
149165
with:
150166
go-version: '1.17'
151167
check-latest: true
152-
cache: true
153168
cache-dependency-path: subdir/go.sum
154169
- run: go run hello.go
155170
```
171+
156172
## Getting go version from the go.mod file
157173

158-
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.
174+
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
175+
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
176+
latest available patch version sequentially in the runner's directory with the cached tools, in
177+
the [versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go
178+
servers.
159179

160180
If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used.
161181
> The action will search for the `go.mod` file relative to the repository root
162182

163183
```yaml
164184
steps:
165-
- uses: actions/checkout@v3
166-
- uses: actions/setup-go@v3
167-
with:
168-
go-version-file: 'path/to/go.mod'
169-
- run: go version
185+
- uses: actions/checkout@v3
186+
- uses: actions/setup-go@v3
187+
with:
188+
go-version-file: 'path/to/go.mod'
189+
- run: go version
170190
```
171191

172192
## Matrix testing
@@ -195,13 +215,23 @@ The `go-version` input supports the following syntax:
195215
- Specific versions: `1.15`, `1.16.1`, `1.17.0-rc.2`, `1.16.0-beta.1`
196216
- SemVer's version range syntax: `^1.13.1`, `>=1.18.0-rc.1`
197217

198-
For more information about semantic versioning, please refer to [semver](https://github.com/npm/node-semver) documentation.
218+
For more information about semantic versioning, please refer to [semver](https://github.com/npm/node-semver)
219+
documentation.
199220

200221
## Using `setup-go` on GHES
201222

202-
`setup-go` comes pre-installed on the appliance with GHES if Actions is enabled. When dynamically downloading Go distributions, `setup-go` downloads distributions from [`actions/go-versions`](https://github.com/actions/go-versions) on github.com (outside of the appliance). These calls to `actions/go-versions` are made via unauthenticated requests, which are limited to [60 requests per hour per IP](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting). If more requests are made within the time frame, then you will start to see rate-limit errors during downloading that looks like: `##[error]API rate limit exceeded for...`. After that error the action will try to download versions directly from https://storage.googleapis.com/golang, but it also can have rate limit so it's better to put token.
223+
`setup-go` comes pre-installed on the appliance with GHES if Actions is enabled. When dynamically downloading Go
224+
distributions, `setup-go` downloads distributions from [`actions/go-versions`](https://github.com/actions/go-versions)
225+
on github.com (outside of the appliance). These calls to `actions/go-versions` are made via unauthenticated requests,
226+
which are limited
227+
to [60 requests per hour per IP](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting). If
228+
more requests are made within the time frame, then you will start to see rate-limit errors during downloading that looks
229+
like: `##[error]API rate limit exceeded for...`. After that error the action will try to download versions directly
230+
from https://storage.googleapis.com/golang, but it also can have rate limit so it's better to put token.
203231

204-
To get a higher rate limit, you can [generate a personal access token on github.com](https://github.com/settings/tokens/new) and pass it as the `token` input for the action:
232+
To get a higher rate limit, you
233+
can [generate a personal access token on github.com](https://github.com/settings/tokens/new) and pass it as the `token`
234+
input for the action:
205235

206236
```yaml
207237
uses: actions/setup-go@v3
@@ -210,7 +240,10 @@ with:
210240
go-version: 1.18
211241
```
212242

213-
If the runner is not able to access github.com, any Go versions requested during a workflow run must come from the runner's tool cache. See "[Setting up the tool cache on self-hosted runners without internet access](https://docs.github.com/en/[email protected]/admin/github-actions/managing-access-to-actions-from-githubcom/setting-up-the-tool-cache-on-self-hosted-runners-without-internet-access)" for more information.
243+
If the runner is not able to access github.com, any Go versions requested during a workflow run must come from the
244+
runner's tool cache.
245+
See "[Setting up the tool cache on self-hosted runners without internet access](https://docs.github.com/en/[email protected]/admin/github-actions/managing-access-to-actions-from-githubcom/setting-up-the-tool-cache-on-self-hosted-runners-without-internet-access)"
246+
for more information.
214247

215248
# License
216249

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ inputs:
1414
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
1515
cache:
1616
description: Used to specify whether caching is needed. Set to true, if you'd like to enable caching.
17-
default: false
17+
default: true
1818
cache-dependency-path:
1919
description: 'Used to specify the path to a dependency file - go.sum'
2020
architecture:

dist/cache-save/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -60356,7 +60356,7 @@ function run() {
6035660356
if (typeof error === 'string') {
6035760357
message = error;
6035860358
}
60359-
core.setFailed(message);
60359+
core.warning(message);
6036060360
}
6036160361
});
6036260362
}
@@ -60379,6 +60379,10 @@ const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () {
6037960379
if (nonExistingPaths.length) {
6038060380
logWarning(`Cache folder path is retrieved but doesn't exist on disk: ${nonExistingPaths.join(', ')}`);
6038160381
}
60382+
if (!primaryKey) {
60383+
core.info('Primary key was not generated. Please check the log messages above for more errors or information');
60384+
return;
60385+
}
6038260386
if (primaryKey === state) {
6038360387
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
6038460388
return;
@@ -60457,8 +60461,17 @@ const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void
6045760461
});
6045860462
exports.getPackageManagerInfo = getPackageManagerInfo;
6045960463
const getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () {
60460-
const pathList = yield Promise.all(packageManagerInfo.cacheFolderCommandList.map((command) => __awaiter(void 0, void 0, void 0, function* () { return exports.getCommandOutput(command); })));
60461-
const cachePaths = pathList.filter(item => item);
60464+
const pathOutputs = yield Promise.allSettled(packageManagerInfo.cacheFolderCommandList.map((command) => __awaiter(void 0, void 0, void 0, function* () { return exports.getCommandOutput(command); })));
60465+
const results = pathOutputs.map(item => {
60466+
if (item.status === 'fulfilled') {
60467+
return item.value;
60468+
}
60469+
else {
60470+
core.info(`[warning]getting cache directory path failed: ${item.reason}`);
60471+
}
60472+
return '';
60473+
});
60474+
const cachePaths = results.filter(item => item);
6046260475
if (!cachePaths.length) {
6046360476
throw new Error(`Could not get cache folder paths.`);
6046460477
}

dist/setup/index.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -63130,8 +63130,17 @@ const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void
6313063130
});
6313163131
exports.getPackageManagerInfo = getPackageManagerInfo;
6313263132
const getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () {
63133-
const pathList = yield Promise.all(packageManagerInfo.cacheFolderCommandList.map((command) => __awaiter(void 0, void 0, void 0, function* () { return exports.getCommandOutput(command); })));
63134-
const cachePaths = pathList.filter(item => item);
63133+
const pathOutputs = yield Promise.allSettled(packageManagerInfo.cacheFolderCommandList.map((command) => __awaiter(void 0, void 0, void 0, function* () { return exports.getCommandOutput(command); })));
63134+
const results = pathOutputs.map(item => {
63135+
if (item.status === 'fulfilled') {
63136+
return item.value;
63137+
}
63138+
else {
63139+
core.info(`[warning]getting cache directory path failed: ${item.reason}`);
63140+
}
63141+
return '';
63142+
});
63143+
const cachePaths = results.filter(item => item);
6313563144
if (!cachePaths.length) {
6313663145
throw new Error(`Could not get cache folder paths.`);
6313763146
}
@@ -63605,7 +63614,12 @@ function run() {
6360563614
if (cache && cache_utils_1.isCacheFeatureAvailable()) {
6360663615
const packageManager = 'default';
6360763616
const cacheDependencyPath = core.getInput('cache-dependency-path');
63608-
yield cache_restore_1.restoreCache(parseGoVersion(goVersion), packageManager, cacheDependencyPath);
63617+
try {
63618+
yield cache_restore_1.restoreCache(parseGoVersion(goVersion), packageManager, cacheDependencyPath);
63619+
}
63620+
catch (error) {
63621+
core.warning(`Restore cache failed: ${error.message}`);
63622+
}
6360963623
}
6361063624
// add problem matchers
6361163625
const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json');

0 commit comments

Comments
 (0)