Skip to content

Commit

Permalink
improvement: Review snapshot behavior (#95)
Browse files Browse the repository at this point in the history
* Improve git tag detection (#77)
* Only handle snapshot flag for release cmd (#94)
* Use core.info instead of console.log
* Update gitattributes
  • Loading branch information
crazy-max committed Feb 11, 2020
1 parent 81d25e3 commit e198786
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/lib/** linguist-detectable=false
/lib/** linguist-generated=true
/node_modules/** linguist-detectable=false
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ jobs:
uses: actions/setup-go@v1
with:
go-version: 1.13.x
-
name: Check
uses: ./
with:
version: ${{ matrix.version }}
args: check --debug
-
name: GoReleaser
uses: ./
Expand Down
49 changes: 49 additions & 0 deletions lib/git.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions lib/installer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions lib/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -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']);
}
10 changes: 5 additions & 5 deletions src/installer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import * as download from 'download';
import * as fs from 'fs';
Expand All @@ -15,7 +16,7 @@ export async function getGoReleaser(version: string): Promise<string> {
version = selected;
}

console.log(`βœ… GoReleaser version found: ${version}`);
core.info(`βœ… GoReleaser version found: ${version}`);
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'goreleaser-'));
const fileName = getFileName();
const downloadUrl = util.format(
Expand All @@ -24,10 +25,10 @@ export async function getGoReleaser(version: string): Promise<string> {
fileName
);

console.log(`⬇️ Downloading ${downloadUrl}...`);
core.info(`⬇️ Downloading ${downloadUrl}...`);
await download.default(downloadUrl, tmpdir, {filename: fileName});

console.log('πŸ“¦ Extracting GoReleaser...');
core.info('πŸ“¦ Extracting GoReleaser...');
let extPath: string = tmpdir;
if (osPlat == 'win32') {
extPath = await tc.extractZip(`${tmpdir}/${fileName}`);
Expand All @@ -42,8 +43,7 @@ function getFileName(): string {
const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux';
const arch: string = osArch == 'x64' ? 'x86_64' : 'i386';
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz';
const filename: string = util.format('goreleaser_%s_%s.%s', platform, arch, ext);
return filename;
return util.format('goreleaser_%s_%s.%s', platform, arch, ext);
}

interface GitHubRelease {
Expand Down
25 changes: 16 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as git from './git';
import * as installer from './installer';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
Expand All @@ -11,31 +12,37 @@ export async function run(silent?: boolean) {
const workdir = core.getInput('workdir') || '.';
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 !== '.') {
console.log(`πŸ“‚ Using ${workdir} as working directory...`)
core.info(`πŸ“‚ Using ${workdir} as working directory...`);
process.chdir(workdir);
}

let snapshot = '';
if (!process.env.GITHUB_REF || !process.env.GITHUB_REF.startsWith('refs/tags/')) {
console.log(`⚠️ No tag found. Snapshot forced`);
if (!args.includes('--snapshot')) {
snapshot = ' --snapshot';
if (args.split(' ').indexOf('release') > -1) {
if (isTagDirty) {
core.info(`⚠️ No tag found for commit ${commit}. Snapshot forced`);
if (!args.includes('--snapshot')) {
snapshot = ' --snapshot';
}
} else {
core.info(`βœ… ${tag} tag found for commit ${commit}`);
}
} else {
console.log(`βœ… ${process.env.GITHUB_REF!.split('/')[2]} tag found`);
}

if (key) {
console.log('πŸ”‘ Importing signing key...');
core.info('πŸ”‘ Importing signing key...');
let path = `${process.env.HOME}/key.asc`;
fs.writeFileSync(path, key, {mode: 0o600});
await exec.exec('gpg', ['--import', path], {
silent: silent
});
}

console.log('πŸƒ Running GoReleaser...');
core.info('πŸƒ Running GoReleaser...');
await exec.exec(`${goreleaser} ${args}${snapshot}`, undefined, {
silent: silent
});
Expand Down

0 comments on commit e198786

Please sign in to comment.