improvement: Review snapshot behavior (#95)

* Improve git tag detection (#77)
* Only handle snapshot flag for release cmd (#94)
* Use core.info instead of console.log
* Update gitattributes
This commit is contained in:
CrazyMax 2020-02-11 13:52:06 +01:00 committed by GitHub
parent 81d25e3b66
commit e198786300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 123 additions and 30 deletions

2
.gitattributes vendored
View File

@ -1,2 +1,2 @@
/lib/** linguist-detectable=false /lib/** linguist-generated=true
/node_modules/** linguist-detectable=false /node_modules/** linguist-detectable=false

View File

@ -35,6 +35,12 @@ jobs:
uses: actions/setup-go@v1 uses: actions/setup-go@v1
with: with:
go-version: 1.13.x go-version: 1.13.x
-
name: Check
uses: ./
with:
version: ${{ matrix.version }}
args: check --debug
- -
name: GoReleaser name: GoReleaser
uses: ./ uses: ./

49
lib/git.js generated Normal file
View File

@ -0,0 +1,49 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process = __importStar(require("child_process"));
const git = (args = []) => __awaiter(void 0, void 0, void 0, function* () {
const stdout = child_process.execSync(`git ${args.join(' ')}`, {
encoding: 'utf8'
});
return stdout.trim();
});
function isTagDirty(currentTag) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield git(['describe', '--exact-match', '--tags', '--match', currentTag]);
}
catch (err) {
return true;
}
return false;
});
}
exports.isTagDirty = isTagDirty;
function getTag() {
return __awaiter(this, void 0, void 0, function* () {
return yield git(['describe', '--tags', '--abbrev=0']);
});
}
exports.getTag = getTag;
function getShortCommit() {
return __awaiter(this, void 0, void 0, function* () {
return yield git(['show', "--format='%h'", 'HEAD', '--quiet']);
});
}
exports.getShortCommit = getShortCommit;

10
lib/installer.js generated
View File

@ -16,6 +16,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const tc = __importStar(require("@actions/tool-cache")); const tc = __importStar(require("@actions/tool-cache"));
const download = __importStar(require("download")); const download = __importStar(require("download"));
const fs = __importStar(require("fs")); const fs = __importStar(require("fs"));
@ -31,13 +32,13 @@ function getGoReleaser(version) {
if (selected) { if (selected) {
version = selected; version = selected;
} }
console.log(`✅ GoReleaser version found: ${version}`); core.info(`✅ GoReleaser version found: ${version}`);
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'goreleaser-')); const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'goreleaser-'));
const fileName = getFileName(); const fileName = getFileName();
const downloadUrl = util.format('https://github.com/goreleaser/goreleaser/releases/download/%s/%s', version, fileName); const downloadUrl = util.format('https://github.com/goreleaser/goreleaser/releases/download/%s/%s', version, fileName);
console.log(`⬇️ Downloading ${downloadUrl}...`); core.info(`⬇️ Downloading ${downloadUrl}...`);
yield download.default(downloadUrl, tmpdir, { filename: fileName }); yield download.default(downloadUrl, tmpdir, { filename: fileName });
console.log('📦 Extracting GoReleaser...'); core.info('📦 Extracting GoReleaser...');
let extPath = tmpdir; let extPath = tmpdir;
if (osPlat == 'win32') { if (osPlat == 'win32') {
extPath = yield tc.extractZip(`${tmpdir}/${fileName}`); extPath = yield tc.extractZip(`${tmpdir}/${fileName}`);
@ -53,8 +54,7 @@ function getFileName() {
const platform = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux'; const platform = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux';
const arch = osArch == 'x64' ? 'x86_64' : 'i386'; const arch = osArch == 'x64' ? 'x86_64' : 'i386';
const ext = osPlat == 'win32' ? 'zip' : 'tar.gz'; const ext = osPlat == 'win32' ? 'zip' : 'tar.gz';
const filename = util.format('goreleaser_%s_%s.%s', platform, arch, ext); return util.format('goreleaser_%s_%s.%s', platform, arch, ext);
return filename;
} }
function determineVersion(version) { function determineVersion(version) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {

18
lib/main.js generated
View File

@ -16,6 +16,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const git = __importStar(require("./git"));
const installer = __importStar(require("./installer")); const installer = __importStar(require("./installer"));
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
@ -28,29 +29,34 @@ function run(silent) {
const key = core.getInput('key'); const key = core.getInput('key');
const workdir = core.getInput('workdir') || '.'; const workdir = core.getInput('workdir') || '.';
const goreleaser = yield installer.getGoReleaser(version); const goreleaser = yield installer.getGoReleaser(version);
const commit = yield git.getShortCommit();
const tag = yield git.getTag();
const isTagDirty = yield git.isTagDirty(tag);
if (workdir && workdir !== '.') { if (workdir && workdir !== '.') {
console.log(`📂 Using ${workdir} as working directory...`); core.info(`📂 Using ${workdir} as working directory...`);
process.chdir(workdir); process.chdir(workdir);
} }
let snapshot = ''; let snapshot = '';
if (!process.env.GITHUB_REF || !process.env.GITHUB_REF.startsWith('refs/tags/')) { if (args.split(' ').indexOf('release') > -1) {
console.log(`⚠️ No tag found. Snapshot forced`); if (isTagDirty) {
core.info(`⚠️ No tag found for commit ${commit}. Snapshot forced`);
if (!args.includes('--snapshot')) { if (!args.includes('--snapshot')) {
snapshot = ' --snapshot'; snapshot = ' --snapshot';
} }
} }
else { else {
console.log(`${process.env.GITHUB_REF.split('/')[2]} tag found`); core.info(`${tag} tag found for commit ${commit}`);
}
} }
if (key) { if (key) {
console.log('🔑 Importing signing key...'); core.info('🔑 Importing signing key...');
let path = `${process.env.HOME}/key.asc`; let path = `${process.env.HOME}/key.asc`;
fs.writeFileSync(path, key, { mode: 0o600 }); fs.writeFileSync(path, key, { mode: 0o600 });
yield exec.exec('gpg', ['--import', path], { yield exec.exec('gpg', ['--import', path], {
silent: silent silent: silent
}); });
} }
console.log('🏃 Running GoReleaser...'); core.info('🏃 Running GoReleaser...');
yield exec.exec(`${goreleaser} ${args}${snapshot}`, undefined, { yield exec.exec(`${goreleaser} ${args}${snapshot}`, undefined, {
silent: silent silent: silent
}); });

25
src/git.ts Normal file
View File

@ -0,0 +1,25 @@
import * as child_process from 'child_process';
const git = async (args: string[] = []) => {
const stdout = child_process.execSync(`git ${args.join(' ')}`, {
encoding: 'utf8'
});
return stdout.trim();
};
export async function isTagDirty(currentTag: string): Promise<boolean> {
try {
await git(['describe', '--exact-match', '--tags', '--match', currentTag]);
} catch (err) {
return true;
}
return false;
}
export async function getTag(): Promise<string> {
return await git(['describe', '--tags', '--abbrev=0']);
}
export async function getShortCommit(): Promise<string> {
return await git(['show', "--format='%h'", 'HEAD', '--quiet']);
}

View File

@ -1,3 +1,4 @@
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as download from 'download'; import * as download from 'download';
import * as fs from 'fs'; import * as fs from 'fs';
@ -15,7 +16,7 @@ export async function getGoReleaser(version: string): Promise<string> {
version = selected; version = selected;
} }
console.log(`✅ GoReleaser version found: ${version}`); core.info(`✅ GoReleaser version found: ${version}`);
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'goreleaser-')); const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'goreleaser-'));
const fileName = getFileName(); const fileName = getFileName();
const downloadUrl = util.format( const downloadUrl = util.format(
@ -24,10 +25,10 @@ export async function getGoReleaser(version: string): Promise<string> {
fileName fileName
); );
console.log(`⬇️ Downloading ${downloadUrl}...`); core.info(`⬇️ Downloading ${downloadUrl}...`);
await download.default(downloadUrl, tmpdir, {filename: fileName}); await download.default(downloadUrl, tmpdir, {filename: fileName});
console.log('📦 Extracting GoReleaser...'); core.info('📦 Extracting GoReleaser...');
let extPath: string = tmpdir; let extPath: string = tmpdir;
if (osPlat == 'win32') { if (osPlat == 'win32') {
extPath = await tc.extractZip(`${tmpdir}/${fileName}`); extPath = await tc.extractZip(`${tmpdir}/${fileName}`);
@ -42,8 +43,7 @@ function getFileName(): string {
const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux'; const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux';
const arch: string = osArch == 'x64' ? 'x86_64' : 'i386'; const arch: string = osArch == 'x64' ? 'x86_64' : 'i386';
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz'; const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz';
const filename: string = util.format('goreleaser_%s_%s.%s', platform, arch, ext); return util.format('goreleaser_%s_%s.%s', platform, arch, ext);
return filename;
} }
interface GitHubRelease { interface GitHubRelease {

View File

@ -1,3 +1,4 @@
import * as git from './git';
import * as installer from './installer'; import * as installer from './installer';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
@ -11,23 +12,29 @@ export async function run(silent?: boolean) {
const workdir = core.getInput('workdir') || '.'; const workdir = core.getInput('workdir') || '.';
const goreleaser = await installer.getGoReleaser(version); const goreleaser = await installer.getGoReleaser(version);
const commit = await git.getShortCommit();
const tag = await git.getTag();
const isTagDirty = await git.isTagDirty(tag);
if (workdir && workdir !== '.') { if (workdir && workdir !== '.') {
console.log(`📂 Using ${workdir} as working directory...`) core.info(`📂 Using ${workdir} as working directory...`);
process.chdir(workdir); process.chdir(workdir);
} }
let snapshot = ''; let snapshot = '';
if (!process.env.GITHUB_REF || !process.env.GITHUB_REF.startsWith('refs/tags/')) { if (args.split(' ').indexOf('release') > -1) {
console.log(`⚠️ No tag found. Snapshot forced`); if (isTagDirty) {
core.info(`⚠️ No tag found for commit ${commit}. Snapshot forced`);
if (!args.includes('--snapshot')) { if (!args.includes('--snapshot')) {
snapshot = ' --snapshot'; snapshot = ' --snapshot';
} }
} else { } else {
console.log(`${process.env.GITHUB_REF!.split('/')[2]} tag found`); core.info(`${tag} tag found for commit ${commit}`);
}
} }
if (key) { if (key) {
console.log('🔑 Importing signing key...'); core.info('🔑 Importing signing key...');
let path = `${process.env.HOME}/key.asc`; let path = `${process.env.HOME}/key.asc`;
fs.writeFileSync(path, key, {mode: 0o600}); fs.writeFileSync(path, key, {mode: 0o600});
await exec.exec('gpg', ['--import', path], { await exec.exec('gpg', ['--import', path], {
@ -35,7 +42,7 @@ export async function run(silent?: boolean) {
}); });
} }
console.log('🏃 Running GoReleaser...'); core.info('🏃 Running GoReleaser...');
await exec.exec(`${goreleaser} ${args}${snapshot}`, undefined, { await exec.exec(`${goreleaser} ${args}${snapshot}`, undefined, {
silent: silent silent: silent
}); });