Compare commits

...

12 Commits

Author SHA1 Message Date
1ef31b642a Merge pull request #10 from golangci/feature/fix-github-token
docs: recommend using GITHUB_TOKEN
2020-05-07 15:44:41 +03:00
fc9d1728df docs: recommend using GITHUB_TOKEN
Fixes: #9
2020-05-07 15:42:54 +03:00
1ad27ad153 s/@v1/@v0 2020-05-07 11:26:37 +03:00
1778ebaefa dev: shorten description 2020-05-05 18:17:02 +03:00
1dd9e0522b dev: improve description 2020-05-05 18:11:57 +03:00
66883b5fcf dev: change icon and color on GitHub marketplace 2020-05-05 17:57:46 +03:00
8ea3043ee4 docs: improve info about performance 2020-05-05 17:51:32 +03:00
95f6eefffa docs: add perf stat into README.md 2020-05-05 17:36:44 +03:00
977a01f96c Merge pull request #8 from golangci/fix-go.mod-hash
fix go.mod hashsum
2020-05-05 17:20:28 +03:00
13e2c1f984 fix go.mod hashsum 2020-05-05 17:17:46 +03:00
afc07fb790 Merge pull request #7 from golangci/feature/improve-cache
improve caching
2020-05-05 17:06:23 +03:00
b7926cae39 improve caching 2020-05-05 17:03:47 +03:00
9 changed files with 215 additions and 62 deletions

View File

@ -24,4 +24,4 @@ jobs:
with:
version: v1.26
args: --issues-exit-code=0 ./sample/...
github-token: ${{ secrets.GOLANGCI_LINT_GITHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}

View File

@ -1,5 +0,0 @@
FROM golangci/golangci-lint:v1.26
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

View File

@ -2,15 +2,14 @@
[![Build Status](https://github.com/golangci/golangci-lint-action/workflows/build-and-test/badge.svg)](https://github.com/golangci/golangci-lint-action/actions)
![GitHub Annotations](./static/annotations.png)
It's the official GitHub action for [golangci-lint](https://github.com/golangci/golangci-lint) from it's authors.
The action runs [golangci-lint](https://github.com/golangci/golangci-lint) and reports issues from linters.
The action that runs [golangci-lint](https://github.com/golangci/golangci-lint) and reports issues from linters.
![GitHub Annotations](./static/annotations.png)
## How to use
1. Create a [GitHub token](https://github.com/settings/tokens/new) with scope `repo.public_repo`.
2. Add it to a [repository secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#creating-encrypted-secrets): repository -> `Settings` -> `Secrets`.
3. Add `.github/workflows/golangci-lint.yml` with the following contents:
Add `.github/workflows/golangci-lint.yml` with the following contents:
```yaml
name: golangci-lint
@ -28,7 +27,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v1
uses: golangci/golangci-lint-action@v0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.26
@ -36,8 +35,11 @@ jobs:
# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...
# Required: GitHub token with scope `repo.public_repo`. Used for fetching a list of releases of golangci-lint.
github-token: ${{ secrets.GOLANGCI_LINT_GITHUB_TOKEN }}
# Required: the token is used for fetching a list of releases of golangci-lint.
# The secret `GITHUB_TOKEN` is automatically created by GitHub,
# no need to create it manually.
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret
github-token: ${{ secrets.GITHUB_TOKEN }}
```
## Comments and Annotations
@ -49,6 +51,21 @@ The restrictions of annotations are the following:
1. Currently, they don't support markdown formatting (see the [feature request](https://github.community/t5/GitHub-API-Development-and/Checks-Ability-to-include-Markdown-in-line-annotations/m-p/56704))
2. They aren't shown in list of comments like it was with [golangci.com](https://golangci.com). If you would like to have comments - please, up-vote [the issue](https://github.com/golangci/golangci-lint-action/issues/5).
## Performance
The action was implemented with performance in mind:
1. We cache data by [@actions/cache](https://github.com/actions/cache) between builds: Go build cache, Go modules cache, golangci-lint analysis cache.
2. We don't use Docker because image pulling is slow.
3. We do as much as we can in parallel, e.g. we download cache, go and golangci-lint binary in parallel.
For example, in a repository of [golangci-lint](https://github.com/golangci/golangci-lint) running this action without the cache takes 50s, but with cache takes 14s:
* in parallel:
* 13s to download Go
* 4s to restore 50 MB of cache
* 1s to find and install `golangci-lint`
* 1s to run `golangci-lint` (it takes 35s without cache)
## Internals
We use JavaScript-based action. We don't use Docker-based action because:
@ -59,17 +76,25 @@ We use JavaScript-based action. We don't use Docker-based action because:
Inside our action we perform 3 steps:
1. Setup environment running in parallel:
* restore [cache](https://github.com/actions/cache) of previous analyzes into `$HOME/.cache/golangci-lint`
* restore [cache](https://github.com/actions/cache) of previous analyzes
* list [releases of golangci-lint](https://github.com/golangci/golangci-lint/releases) and find the latest patch version
for needed version (users of this action can specify only minor version). After that install [golangci-lint](https://github.com/golangci/golangci-lint) using [@actions/tool-cache](https://github.com/actions/toolkit/tree/master/packages/tool-cache)
* install the latest Go 1.x version using [@actions/setup-go](https://github.com/actions/setup-go)
2. Run `golangci-lint` with specified by user `args`
3. Save cache from `$HOME/.cache/golangci-lint` for later builds
3. Save cache for later builds
### Caching internals
1. We save and restore the following directories: `~/.cache/golangci-lint`, `~/.cache/go-build`, `~/go/pkg`.
2. The primary caching key looks like `golangci-lint.cache-{interval_number}-{go.mod_hash}`. Interval number ensures that we periodically invalidate
our cache (every 7 days). `go.mod` hash ensures that we invalidate the cache early - as soon as dependencies have changed.
3. We use [restore keys](https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key): `golangci-lint.cache-{interval_number}-`, `golangci-lint.cache-`. GitHub matches keys by prefix if we have no exact match for the primary cache.
This scheme is basic and needs improvements. Pull requests and ideas are welcome.
## Development of this action
1. Install [act](https://github.com/nektos/act#installation)
2. Make a symlink for `act` to work properly: `ln -s . golangci-lint-action`
3. Get a [GitHub token](https://github.com/settings/tokens/new) with the scope `repo.public_repo`. Export it by `export GITHUB_TOKEN=YOUR_TOKEN`.
4. Prepare deps once: `npm run prepare-deps`
5. Run `npm run local` after any change to test it
3. Prepare deps once: `npm run prepare-deps`
4. Run `npm run local` after any change to test it

View File

@ -1,6 +1,6 @@
---
name: 'Run golangci-lint'
description: 'Run golangci-lint (WIP)'
description: 'Official golangci-lint action with line-attached annotations for found issues, caching and parallel execution. Beta version.'
author: 'golangci'
inputs:
version:
@ -19,5 +19,5 @@ runs:
main: 'dist/run/index.js'
post: 'dist/post_run/index.js'
branding:
icon: 'check-circle'
color: 'blue'
icon: 'shield'
color: 'yellow'

View File

@ -27870,29 +27870,73 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const restore_1 = __importDefault(__webpack_require__(206));
const save_1 = __importDefault(__webpack_require__(781));
// TODO: ensure dir exists, have access, etc
const getCacheDir = () => `${process.env.HOME}/.cache/golangci-lint`;
const setCacheInputs = () => {
process.env.INPUT_KEY = `golangci-lint.analysis-cache`;
process.env.INPUT_PATH = getCacheDir();
const crypto = __importStar(__webpack_require__(417));
const fs = __importStar(__webpack_require__(747));
function checksumFile(hashName, path) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName);
const stream = fs.createReadStream(path);
stream.on("error", err => reject(err));
stream.on("data", chunk => hash.update(chunk));
stream.on("end", () => resolve(hash.digest("hex")));
});
}
const pathExists = (path) => __awaiter(void 0, void 0, void 0, function* () { return !!(yield fs.promises.stat(path).catch(e => false)); });
const getLintCacheDir = () => `${process.env.HOME}/.cache/golangci-lint`;
const getCacheDirs = () => {
// Not existing dirs are ok here: it works.
return [getLintCacheDir(), `${process.env.HOME}/.cache/go-build`, `${process.env.HOME}/go/pkg`];
};
const getIntervalKey = (invalidationIntervalDays) => {
const now = new Date();
const secondsSinceEpoch = now.getTime() / 1000;
const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400));
return intervalNumber.toString();
};
function buildCacheKeys() {
return __awaiter(this, void 0, void 0, function* () {
const keys = [];
let cacheKey = `golangci-lint.cache-`;
keys.push(cacheKey);
// Periodically invalidate a cache because a new code being added.
// TODO: configure it via inputs.
cacheKey += `${getIntervalKey(7)}-`;
keys.push(cacheKey);
if (yield pathExists(`go.mod`)) {
// Add checksum to key to invalidate a cache when dependencies change.
cacheKey += yield checksumFile(`sha1`, `go.mod`);
}
else {
cacheKey += `nogomod`;
}
keys.push(cacheKey);
return keys;
});
}
function restoreCache() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
setCacheInputs();
const keys = yield buildCacheKeys();
const primaryKey = keys.pop();
const restoreKeys = keys.reverse();
core.info(`Primary analysis cache key is '${primaryKey}', restore keys are '${restoreKeys.join(` | `)}'`);
process.env[`INPUT_RESTORE-KEYS`] = restoreKeys.join(`\n`);
process.env.INPUT_KEY = primaryKey;
process.env.INPUT_PATH = getCacheDirs().join(`\n`);
// Tell golangci-lint to use our cache directory.
process.env.GOLANGCI_LINT_CACHE = getCacheDir();
process.env.GOLANGCI_LINT_CACHE = getLintCacheDir();
yield restore_1.default();
core.info(`Restored golangci-lint analysis cache in ${Date.now() - startedAt}ms`);
core.info(`Restored cache for golangci-lint from key '${primaryKey}' in ${Date.now() - startedAt}ms`);
});
}
exports.restoreCache = restoreCache;
function saveCache() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
setCacheInputs();
const cacheDirs = getCacheDirs();
process.env.INPUT_PATH = cacheDirs.join(`\n`);
yield save_1.default();
core.info(`Saved golangci-lint analysis cache in ${Date.now() - startedAt}ms`);
core.info(`Saved cache for golangci-lint from paths '${cacheDirs.join(`, `)}' in ${Date.now() - startedAt}ms`);
});
}
exports.saveCache = saveCache;

64
dist/run/index.js vendored
View File

@ -27882,29 +27882,73 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const restore_1 = __importDefault(__webpack_require__(206));
const save_1 = __importDefault(__webpack_require__(781));
// TODO: ensure dir exists, have access, etc
const getCacheDir = () => `${process.env.HOME}/.cache/golangci-lint`;
const setCacheInputs = () => {
process.env.INPUT_KEY = `golangci-lint.analysis-cache`;
process.env.INPUT_PATH = getCacheDir();
const crypto = __importStar(__webpack_require__(417));
const fs = __importStar(__webpack_require__(747));
function checksumFile(hashName, path) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName);
const stream = fs.createReadStream(path);
stream.on("error", err => reject(err));
stream.on("data", chunk => hash.update(chunk));
stream.on("end", () => resolve(hash.digest("hex")));
});
}
const pathExists = (path) => __awaiter(void 0, void 0, void 0, function* () { return !!(yield fs.promises.stat(path).catch(e => false)); });
const getLintCacheDir = () => `${process.env.HOME}/.cache/golangci-lint`;
const getCacheDirs = () => {
// Not existing dirs are ok here: it works.
return [getLintCacheDir(), `${process.env.HOME}/.cache/go-build`, `${process.env.HOME}/go/pkg`];
};
const getIntervalKey = (invalidationIntervalDays) => {
const now = new Date();
const secondsSinceEpoch = now.getTime() / 1000;
const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400));
return intervalNumber.toString();
};
function buildCacheKeys() {
return __awaiter(this, void 0, void 0, function* () {
const keys = [];
let cacheKey = `golangci-lint.cache-`;
keys.push(cacheKey);
// Periodically invalidate a cache because a new code being added.
// TODO: configure it via inputs.
cacheKey += `${getIntervalKey(7)}-`;
keys.push(cacheKey);
if (yield pathExists(`go.mod`)) {
// Add checksum to key to invalidate a cache when dependencies change.
cacheKey += yield checksumFile(`sha1`, `go.mod`);
}
else {
cacheKey += `nogomod`;
}
keys.push(cacheKey);
return keys;
});
}
function restoreCache() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
setCacheInputs();
const keys = yield buildCacheKeys();
const primaryKey = keys.pop();
const restoreKeys = keys.reverse();
core.info(`Primary analysis cache key is '${primaryKey}', restore keys are '${restoreKeys.join(` | `)}'`);
process.env[`INPUT_RESTORE-KEYS`] = restoreKeys.join(`\n`);
process.env.INPUT_KEY = primaryKey;
process.env.INPUT_PATH = getCacheDirs().join(`\n`);
// Tell golangci-lint to use our cache directory.
process.env.GOLANGCI_LINT_CACHE = getCacheDir();
process.env.GOLANGCI_LINT_CACHE = getLintCacheDir();
yield restore_1.default();
core.info(`Restored golangci-lint analysis cache in ${Date.now() - startedAt}ms`);
core.info(`Restored cache for golangci-lint from key '${primaryKey}' in ${Date.now() - startedAt}ms`);
});
}
exports.restoreCache = restoreCache;
function saveCache() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
setCacheInputs();
const cacheDirs = getCacheDirs();
process.env.INPUT_PATH = cacheDirs.join(`\n`);
yield save_1.default();
core.info(`Saved golangci-lint analysis cache in ${Date.now() - startedAt}ms`);
core.info(`Saved cache for golangci-lint from paths '${cacheDirs.join(`, `)}' in ${Date.now() - startedAt}ms`);
});
}
exports.saveCache = saveCache;

View File

@ -1,10 +0,0 @@
#!/bin/bash
echo 'golangci-lint-action: start'
echo " flags: ${INPUT_FLAGS}"
echo " format: ${INPUT_FORMAT}"
cd "${GITHUB_WORKSPACE}/${INPUT_DIRECTORY}" || exit 1
# shellcheck disable=SC2086
golangci-lint run --out-format ${INPUT_FORMAT} ${INPUT_FLAGS}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/golangci/golangci-lint-action
go 1.14

View File

@ -1,29 +1,81 @@
import * as core from "@actions/core"
import restore from "cache/lib/restore"
import save from "cache/lib/save"
import * as crypto from "crypto"
import * as fs from "fs"
// TODO: ensure dir exists, have access, etc
const getCacheDir = (): string => `${process.env.HOME}/.cache/golangci-lint`
function checksumFile(hashName: string, path: string) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName)
const stream = fs.createReadStream(path)
stream.on("error", err => reject(err))
stream.on("data", chunk => hash.update(chunk))
stream.on("end", () => resolve(hash.digest("hex")))
})
}
const setCacheInputs = (): void => {
process.env.INPUT_KEY = `golangci-lint.analysis-cache`
process.env.INPUT_PATH = getCacheDir()
const pathExists = async (path: string) => !!(await fs.promises.stat(path).catch(e => false))
const getLintCacheDir = (): string => `${process.env.HOME}/.cache/golangci-lint`
const getCacheDirs = (): string[] => {
// Not existing dirs are ok here: it works.
return [getLintCacheDir(), `${process.env.HOME}/.cache/go-build`, `${process.env.HOME}/go/pkg`]
}
const getIntervalKey = (invalidationIntervalDays: number): string => {
const now = new Date()
const secondsSinceEpoch = now.getTime() / 1000
const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400))
return intervalNumber.toString()
}
async function buildCacheKeys(): Promise<string[]> {
const keys = []
let cacheKey = `golangci-lint.cache-`
keys.push(cacheKey)
// Periodically invalidate a cache because a new code being added.
// TODO: configure it via inputs.
cacheKey += `${getIntervalKey(7)}-`
keys.push(cacheKey)
if (await pathExists(`go.mod`)) {
// Add checksum to key to invalidate a cache when dependencies change.
cacheKey += await checksumFile(`sha1`, `go.mod`)
} else {
cacheKey += `nogomod`
}
keys.push(cacheKey)
return keys
}
export async function restoreCache(): Promise<void> {
const startedAt = Date.now()
setCacheInputs()
const keys = await buildCacheKeys()
const primaryKey = keys.pop()
const restoreKeys = keys.reverse()
core.info(`Primary analysis cache key is '${primaryKey}', restore keys are '${restoreKeys.join(` | `)}'`)
process.env[`INPUT_RESTORE-KEYS`] = restoreKeys.join(`\n`)
process.env.INPUT_KEY = primaryKey
process.env.INPUT_PATH = getCacheDirs().join(`\n`)
// Tell golangci-lint to use our cache directory.
process.env.GOLANGCI_LINT_CACHE = getCacheDir()
process.env.GOLANGCI_LINT_CACHE = getLintCacheDir()
await restore()
core.info(`Restored golangci-lint analysis cache in ${Date.now() - startedAt}ms`)
core.info(`Restored cache for golangci-lint from key '${primaryKey}' in ${Date.now() - startedAt}ms`)
}
export async function saveCache(): Promise<void> {
const startedAt = Date.now()
setCacheInputs()
const cacheDirs = getCacheDirs()
process.env.INPUT_PATH = cacheDirs.join(`\n`)
await save()
core.info(`Saved golangci-lint analysis cache in ${Date.now() - startedAt}ms`)
core.info(`Saved cache for golangci-lint from paths '${cacheDirs.join(`, `)}' in ${Date.now() - startedAt}ms`)
}