feat: add install-mode (#768)

This commit is contained in:
Ludovic Fernandez 2023-06-11 19:16:14 +02:00 committed by GitHub
parent 5be60c708e
commit 185e7a2f8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 316 additions and 32 deletions

View File

@ -69,6 +69,35 @@ jobs:
args: --timeout=3m --issues-exit-code=0 ./sample/...
only-new-issues: true
test-go-install: # make sure the action works on a clean machine without building (go-install mode)
needs: [ build ]
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
version:
- ""
- "latest"
- "v1.53.2"
- "b5093688c0d3008eaacd6066773a1a52e689252f"
runs-on: ${{ matrix.os }}
permissions:
contents: read
pull-requests: read
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
cache: false # setup-go v4 caches by default
- uses: ./
with:
version: ${{ matrix.version }}
args: --timeout=3m --issues-exit-code=0 ./sample/...
only-new-issues: true
install-mode: goinstall
test-go-mod-version:
needs: [ build ]
strategy:

View File

@ -70,6 +70,9 @@ jobs:
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
# Optional:The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
```
We recommend running this action in a job separate from other jobs (`go test`, etc)
@ -124,6 +127,9 @@ jobs:
# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
# Optional:The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
```
You will also likely need to add the following `.gitattributes` file to ensure that line endings for windows builds are properly formatted:

View File

@ -34,6 +34,10 @@ inputs:
description: "if set to true then the action doesn't cache or restore ~/.cache/go-build."
default: false
required: false
install-mode:
description: "The mode to install golangci-lint. It can be 'binary' or 'goinstall'."
default: "binary"
required: false
runs:
using: "node16"
main: "dist/run/index.js"

View File

@ -66370,11 +66370,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.installLint = void 0;
exports.installBin = exports.goInstall = exports.installLint = exports.InstallMode = void 0;
const core = __importStar(__nccwpck_require__(2186));
const tc = __importStar(__nccwpck_require__(7784));
const child_process_1 = __nccwpck_require__(2081);
const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017));
const util_1 = __nccwpck_require__(3837);
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download";
const getAssetURL = (versionConfig) => {
let ext = "tar.gz";
@ -66398,13 +66401,74 @@ const getAssetURL = (versionConfig) => {
const noPrefix = versionConfig.TargetVersion.slice(1);
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`;
};
// The installLint returns path to installed binary of golangci-lint.
function installLint(versionConfig) {
var InstallMode;
(function (InstallMode) {
InstallMode["Binary"] = "binary";
InstallMode["GoInstall"] = "goinstall";
})(InstallMode = exports.InstallMode || (exports.InstallMode = {}));
const printOutput = (res) => {
if (res.stdout) {
core.info(res.stdout);
}
if (res.stderr) {
core.info(res.stderr);
}
};
/**
* Install golangci-lint.
*
* @param versionConfig information about version to install.
* @param mode installation mode.
* @returns path to installed binary of golangci-lint.
*/
function installLint(versionConfig, mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installation mode: ${mode}`);
switch (mode) {
case InstallMode.Binary:
return installBin(versionConfig);
case InstallMode.GoInstall:
return goInstall(versionConfig);
default:
return installBin(versionConfig);
}
});
}
exports.installLint = installLint;
/**
* Install golangci-lint via `go install`.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function goInstall(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const options = { env: Object.assign(Object.assign({}, process.env), { CGO_ENABLED: "1" }) };
const exres = yield execShellCommand(`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
printOutput(exres);
const res = yield execShellCommand(`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
printOutput(res);
// The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`.
const lintPath = res.stderr.trimStart().trimEnd().split(` `, 2)[1];
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`);
return lintPath;
});
}
exports.goInstall = goInstall;
/**
* Install golangci-lint via the precompiled binary.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function installBin(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const assetURL = getAssetURL(versionConfig);
core.info(`Downloading ${assetURL} ...`);
core.info(`Downloading binary ${assetURL} ...`);
const archivePath = yield tc.downloadTool(assetURL);
let extractedDir = "";
let repl = /\.tar\.gz$/;
@ -66427,7 +66491,7 @@ function installLint(versionConfig) {
return lintPath;
});
}
exports.installLint = installLint;
exports.installBin = installBin;
/***/ }),
@ -66486,8 +66550,9 @@ const writeFile = (0, util_1.promisify)(fs.writeFile);
const createTempDir = (0, util_1.promisify)(tmp_1.dir);
function prepareLint() {
return __awaiter(this, void 0, void 0, function* () {
const versionConfig = yield (0, version_1.findLintVersion)();
return yield (0, install_1.installLint)(versionConfig);
const mode = core.getInput("install-mode").toLowerCase();
const versionConfig = yield (0, version_1.findLintVersion)(mode);
return yield (0, install_1.installLint)(versionConfig, mode);
});
}
function fetchPatch() {
@ -66548,11 +66613,10 @@ function prepareEnv() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
// Prepare cache, lint and go in parallel.
const restoreCachePromise = (0, cache_1.restoreCache)();
yield (0, cache_1.restoreCache)();
const prepareLintPromise = prepareLint();
const patchPromise = fetchPatch();
const lintPath = yield prepareLintPromise;
yield restoreCachePromise;
const patchPath = yield patchPromise;
core.info(`Prepared env in ${Date.now() - startedAt}ms`);
return { lintPath, patchPath };
@ -66609,7 +66673,7 @@ function runLint(lintPath, patchPath) {
}
cmdArgs.cwd = path.resolve(workingDirectory);
}
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight();
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd();
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`);
const startedAt = Date.now();
try {
@ -66774,6 +66838,7 @@ const core = __importStar(__nccwpck_require__(2186));
const httpm = __importStar(__nccwpck_require__(6255));
const fs = __importStar(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const install_1 = __nccwpck_require__(1649);
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/;
const parseVersion = (s) => {
@ -66858,9 +66923,13 @@ const getConfig = () => __awaiter(void 0, void 0, void 0, function* () {
throw new Error(`failed to get action config: ${exc.message}`);
}
});
function findLintVersion() {
function findLintVersion(mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Finding needed golangci-lint version...`);
if (mode == install_1.InstallMode.GoInstall) {
const v = core.getInput(`version`);
return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" };
}
const reqLintVersion = getRequestedLintVersion();
// if the patched version is passed, just use it
if ((reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.major) !== null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.minor) != null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.patch) !== null) {

91
dist/run/index.js vendored
View File

@ -66370,11 +66370,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.installLint = void 0;
exports.installBin = exports.goInstall = exports.installLint = exports.InstallMode = void 0;
const core = __importStar(__nccwpck_require__(2186));
const tc = __importStar(__nccwpck_require__(7784));
const child_process_1 = __nccwpck_require__(2081);
const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017));
const util_1 = __nccwpck_require__(3837);
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download";
const getAssetURL = (versionConfig) => {
let ext = "tar.gz";
@ -66398,13 +66401,74 @@ const getAssetURL = (versionConfig) => {
const noPrefix = versionConfig.TargetVersion.slice(1);
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`;
};
// The installLint returns path to installed binary of golangci-lint.
function installLint(versionConfig) {
var InstallMode;
(function (InstallMode) {
InstallMode["Binary"] = "binary";
InstallMode["GoInstall"] = "goinstall";
})(InstallMode = exports.InstallMode || (exports.InstallMode = {}));
const printOutput = (res) => {
if (res.stdout) {
core.info(res.stdout);
}
if (res.stderr) {
core.info(res.stderr);
}
};
/**
* Install golangci-lint.
*
* @param versionConfig information about version to install.
* @param mode installation mode.
* @returns path to installed binary of golangci-lint.
*/
function installLint(versionConfig, mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installation mode: ${mode}`);
switch (mode) {
case InstallMode.Binary:
return installBin(versionConfig);
case InstallMode.GoInstall:
return goInstall(versionConfig);
default:
return installBin(versionConfig);
}
});
}
exports.installLint = installLint;
/**
* Install golangci-lint via `go install`.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function goInstall(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const options = { env: Object.assign(Object.assign({}, process.env), { CGO_ENABLED: "1" }) };
const exres = yield execShellCommand(`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
printOutput(exres);
const res = yield execShellCommand(`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
printOutput(res);
// The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`.
const lintPath = res.stderr.trimStart().trimEnd().split(` `, 2)[1];
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`);
return lintPath;
});
}
exports.goInstall = goInstall;
/**
* Install golangci-lint via the precompiled binary.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function installBin(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const assetURL = getAssetURL(versionConfig);
core.info(`Downloading ${assetURL} ...`);
core.info(`Downloading binary ${assetURL} ...`);
const archivePath = yield tc.downloadTool(assetURL);
let extractedDir = "";
let repl = /\.tar\.gz$/;
@ -66427,7 +66491,7 @@ function installLint(versionConfig) {
return lintPath;
});
}
exports.installLint = installLint;
exports.installBin = installBin;
/***/ }),
@ -66486,8 +66550,9 @@ const writeFile = (0, util_1.promisify)(fs.writeFile);
const createTempDir = (0, util_1.promisify)(tmp_1.dir);
function prepareLint() {
return __awaiter(this, void 0, void 0, function* () {
const versionConfig = yield (0, version_1.findLintVersion)();
return yield (0, install_1.installLint)(versionConfig);
const mode = core.getInput("install-mode").toLowerCase();
const versionConfig = yield (0, version_1.findLintVersion)(mode);
return yield (0, install_1.installLint)(versionConfig, mode);
});
}
function fetchPatch() {
@ -66548,11 +66613,10 @@ function prepareEnv() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
// Prepare cache, lint and go in parallel.
const restoreCachePromise = (0, cache_1.restoreCache)();
yield (0, cache_1.restoreCache)();
const prepareLintPromise = prepareLint();
const patchPromise = fetchPatch();
const lintPath = yield prepareLintPromise;
yield restoreCachePromise;
const patchPath = yield patchPromise;
core.info(`Prepared env in ${Date.now() - startedAt}ms`);
return { lintPath, patchPath };
@ -66609,7 +66673,7 @@ function runLint(lintPath, patchPath) {
}
cmdArgs.cwd = path.resolve(workingDirectory);
}
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight();
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd();
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`);
const startedAt = Date.now();
try {
@ -66774,6 +66838,7 @@ const core = __importStar(__nccwpck_require__(2186));
const httpm = __importStar(__nccwpck_require__(6255));
const fs = __importStar(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const install_1 = __nccwpck_require__(1649);
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/;
const parseVersion = (s) => {
@ -66858,9 +66923,13 @@ const getConfig = () => __awaiter(void 0, void 0, void 0, function* () {
throw new Error(`failed to get action config: ${exc.message}`);
}
});
function findLintVersion() {
function findLintVersion(mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Finding needed golangci-lint version...`);
if (mode == install_1.InstallMode.GoInstall) {
const v = core.getInput(`version`);
return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" };
}
const reqLintVersion = getRequestedLintVersion();
// if the patched version is passed, just use it
if ((reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.major) !== null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.minor) != null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.patch) !== null) {

View File

@ -1,10 +1,14 @@
import * as core from "@actions/core"
import * as tc from "@actions/tool-cache"
import { exec, ExecOptions } from "child_process"
import os from "os"
import path from "path"
import { promisify } from "util"
import { VersionConfig } from "./version"
const execShellCommand = promisify(exec)
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download"
const getAssetURL = (versionConfig: VersionConfig): string => {
@ -31,13 +35,95 @@ const getAssetURL = (versionConfig: VersionConfig): string => {
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`
}
// The installLint returns path to installed binary of golangci-lint.
export async function installLint(versionConfig: VersionConfig): Promise<string> {
export enum InstallMode {
Binary = "binary",
GoInstall = "goinstall",
}
type ExecRes = {
stdout: string
stderr: string
}
const printOutput = (res: ExecRes): void => {
if (res.stdout) {
core.info(res.stdout)
}
if (res.stderr) {
core.info(res.stderr)
}
}
/**
* Install golangci-lint.
*
* @param versionConfig information about version to install.
* @param mode installation mode.
* @returns path to installed binary of golangci-lint.
*/
export async function installLint(versionConfig: VersionConfig, mode: InstallMode): Promise<string> {
core.info(`Installation mode: ${mode}`)
switch (mode) {
case InstallMode.Binary:
return installBin(versionConfig)
case InstallMode.GoInstall:
return goInstall(versionConfig)
default:
return installBin(versionConfig)
}
}
/**
* Install golangci-lint via `go install`.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
export async function goInstall(versionConfig: VersionConfig): Promise<string> {
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`)
const startedAt = Date.now()
const options: ExecOptions = { env: { ...process.env, CGO_ENABLED: "1" } }
const exres = await execShellCommand(
`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`,
options
)
printOutput(exres)
const res = await execShellCommand(
`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`,
options
)
printOutput(res)
// The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`.
const lintPath = res.stderr.trimStart().trimEnd().split(` `, 2)[1]
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`)
return lintPath
}
/**
* Install golangci-lint via the precompiled binary.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
export async function installBin(versionConfig: VersionConfig): Promise<string> {
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`)
const startedAt = Date.now()
const assetURL = getAssetURL(versionConfig)
core.info(`Downloading ${assetURL} ...`)
core.info(`Downloading binary ${assetURL} ...`)
const archivePath = await tc.downloadTool(assetURL)
let extractedDir = ""
let repl = /\.tar\.gz$/
if (assetURL.endsWith("zip")) {
@ -55,6 +141,8 @@ export async function installLint(versionConfig: VersionConfig): Promise<string>
const urlParts = assetURL.split(`/`)
const dirName = urlParts[urlParts.length - 1].replace(repl, ``)
const lintPath = path.join(extractedDir, dirName, `golangci-lint`)
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`)
return lintPath
}

View File

@ -7,7 +7,7 @@ import { dir } from "tmp"
import { promisify } from "util"
import { restoreCache, saveCache } from "./cache"
import { installLint } from "./install"
import { installLint, InstallMode } from "./install"
import { findLintVersion } from "./version"
const execShellCommand = promisify(exec)
@ -15,8 +15,10 @@ const writeFile = promisify(fs.writeFile)
const createTempDir = promisify(dir)
async function prepareLint(): Promise<string> {
const versionConfig = await findLintVersion()
return await installLint(versionConfig)
const mode = core.getInput("install-mode").toLowerCase()
const versionConfig = await findLintVersion(<InstallMode>mode)
return await installLint(versionConfig, <InstallMode>mode)
}
async function fetchPatch(): Promise<string> {
@ -83,15 +85,15 @@ async function prepareEnv(): Promise<Env> {
const startedAt = Date.now()
// Prepare cache, lint and go in parallel.
const restoreCachePromise = restoreCache()
await restoreCache()
const prepareLintPromise = prepareLint()
const patchPromise = fetchPatch()
const lintPath = await prepareLintPromise
await restoreCachePromise
const patchPath = await patchPromise
core.info(`Prepared env in ${Date.now() - startedAt}ms`)
return { lintPath, patchPath }
}
@ -159,8 +161,10 @@ async function runLint(lintPath: string, patchPath: string): Promise<void> {
cmdArgs.cwd = path.resolve(workingDirectory)
}
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight()
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd()
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`)
const startedAt = Date.now()
try {
const res = await execShellCommand(cmd, cmdArgs)

View File

@ -3,6 +3,8 @@ import * as httpm from "@actions/http-client"
import * as fs from "fs"
import path from "path"
import { InstallMode } from "./install"
// TODO: make a class
export type Version = {
major: number
@ -17,6 +19,7 @@ const parseVersion = (s: string): Version => {
if (s == "latest" || s == "") {
return null
}
const match = s.match(versionRe)
if (!match) {
throw new Error(`invalid version string '${s}', expected format v1.2 or v1.2.3`)
@ -61,6 +64,7 @@ const isLessVersion = (a: Version, b: Version): boolean => {
const getRequestedLintVersion = (): Version => {
let requestedLintVersion = core.getInput(`version`)
const workingDirectory = core.getInput(`working-directory`)
let goMod = "go.mod"
if (workingDirectory) {
goMod = path.join(workingDirectory, goMod)
@ -79,6 +83,7 @@ const getRequestedLintVersion = (): Version => {
if (parsedRequestedLintVersion == null) {
return null
}
if (isLessVersion(parsedRequestedLintVersion, minVersion)) {
throw new Error(
`requested golangci-lint version '${requestedLintVersion}' isn't supported: we support only ${stringifyVersion(
@ -86,6 +91,7 @@ const getRequestedLintVersion = (): Version => {
)} and later versions`
)
}
return parsedRequestedLintVersion
}
@ -120,9 +126,16 @@ const getConfig = async (): Promise<Config> => {
}
}
export async function findLintVersion(): Promise<VersionConfig> {
export async function findLintVersion(mode: InstallMode): Promise<VersionConfig> {
core.info(`Finding needed golangci-lint version...`)
if (mode == InstallMode.GoInstall) {
const v: string = core.getInput(`version`)
return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" }
}
const reqLintVersion = getRequestedLintVersion()
// if the patched version is passed, just use it
if (reqLintVersion?.major !== null && reqLintVersion?.minor != null && reqLintVersion?.patch !== null) {
return new Promise((resolve) => {
@ -133,6 +146,7 @@ export async function findLintVersion(): Promise<VersionConfig> {
})
})
}
const startedAt = Date.now()
const config = await getConfig()
@ -155,5 +169,6 @@ export async function findLintVersion(): Promise<VersionConfig> {
Date.now() - startedAt
}ms`
)
return versionConfig
}