mirror of
https://github.com/goreleaser/goreleaser-action
synced 2026-07-08 18:37:31 +00:00
feat: Use native GitHub Action tools to download assets and use GitHub API (#187)
* Use native GitHub Action tools to download assets and use GitHub API * Fix unexpected output when tag not found * Use GitHub Action exec * Add screenshot Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
import * as actionsExec from '@actions/exec';
|
||||
import {ExecOptions} from '@actions/exec';
|
||||
|
||||
export interface ExecResult {
|
||||
success: boolean;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
}
|
||||
|
||||
export const exec = async (command: string, args: string[] = [], silent: boolean): Promise<ExecResult> => {
|
||||
let stdout: string = '';
|
||||
let stderr: string = '';
|
||||
|
||||
const options: ExecOptions = {
|
||||
silent: silent,
|
||||
ignoreReturnCode: true
|
||||
};
|
||||
options.listeners = {
|
||||
stdout: (data: Buffer) => {
|
||||
stdout += data.toString();
|
||||
},
|
||||
stderr: (data: Buffer) => {
|
||||
stderr += data.toString();
|
||||
}
|
||||
};
|
||||
|
||||
const returnCode: number = await actionsExec.exec(command, args, options);
|
||||
|
||||
return {
|
||||
success: returnCode === 0,
|
||||
stdout: stdout.trim(),
|
||||
stderr: stderr.trim()
|
||||
};
|
||||
};
|
||||
+15
-13
@@ -1,12 +1,22 @@
|
||||
import * as child_process from 'child_process';
|
||||
import * as exec from './exec';
|
||||
|
||||
const git = async (args: string[] = []) => {
|
||||
const stdout = child_process.execSync(`git ${args.join(' ')}`, {
|
||||
encoding: 'utf8'
|
||||
const git = async (args: string[] = []): Promise<string> => {
|
||||
return await exec.exec(`git`, args, true).then(res => {
|
||||
if (res.stderr != '' && !res.success) {
|
||||
throw new Error(res.stderr);
|
||||
}
|
||||
return res.stdout.trim();
|
||||
});
|
||||
return stdout.trim();
|
||||
};
|
||||
|
||||
export async function getTag(): Promise<string> {
|
||||
try {
|
||||
return await git(['describe', '--tags', '--abbrev=0']);
|
||||
} catch (err) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
export async function isTagDirty(currentTag: string): Promise<boolean> {
|
||||
try {
|
||||
await git(['describe', '--exact-match', '--tags', '--match', currentTag]);
|
||||
@@ -16,14 +26,6 @@ export async function isTagDirty(currentTag: string): Promise<boolean> {
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function getTag(): Promise<string> {
|
||||
try {
|
||||
return await git(['describe', '--tags', '--abbrev=0']);
|
||||
} catch (err) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
export async function getShortCommit(): Promise<string> {
|
||||
return await git(['show', "--format='%h'", 'HEAD', '--quiet']);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import * as httpm from '@actions/http-client';
|
||||
|
||||
export interface GitHubRelease {
|
||||
id: number;
|
||||
tag_name: string;
|
||||
}
|
||||
|
||||
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
|
||||
const url: string = `https://github.com/goreleaser/goreleaser/releases/${version}`;
|
||||
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
|
||||
return (await http.getJson<GitHubRelease>(url)).result;
|
||||
};
|
||||
+27
-43
@@ -1,68 +1,52 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import * as download from 'download';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as util from 'util';
|
||||
import * as restm from 'typed-rest-client/RestClient';
|
||||
import * as github from './github';
|
||||
import * as core from '@actions/core';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
|
||||
let osPlat: string = os.platform();
|
||||
let osArch: string = os.arch();
|
||||
const osPlat: string = os.platform();
|
||||
const osArch: string = os.arch();
|
||||
|
||||
export async function getGoReleaser(version: string): Promise<string> {
|
||||
const selected = await determineVersion(version);
|
||||
if (selected) {
|
||||
version = selected;
|
||||
const release: github.GitHubRelease | null = await github.getRelease(version);
|
||||
if (!release) {
|
||||
throw new Error(`Cannot find GoReleaser ${version} release`);
|
||||
}
|
||||
|
||||
core.info(`✅ GoReleaser version found: ${version}`);
|
||||
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'goreleaser-'));
|
||||
const fileName = getFileName();
|
||||
core.info(`✅ GoReleaser version found: ${release.tag_name}`);
|
||||
const filename = getFilename();
|
||||
const downloadUrl = util.format(
|
||||
'https://github.com/goreleaser/goreleaser/releases/download/%s/%s',
|
||||
version,
|
||||
fileName
|
||||
release.tag_name,
|
||||
filename
|
||||
);
|
||||
|
||||
core.info(`⬇️ Downloading ${downloadUrl}...`);
|
||||
await download.default(downloadUrl, tmpdir, {filename: fileName});
|
||||
const downloadPath: string = await tc.downloadTool(downloadUrl);
|
||||
core.debug(`Downloaded to ${downloadPath}`);
|
||||
|
||||
core.info('📦 Extracting GoReleaser...');
|
||||
let extPath: string = tmpdir;
|
||||
let extPath: string;
|
||||
if (osPlat == 'win32') {
|
||||
extPath = await tc.extractZip(`${tmpdir}/${fileName}`);
|
||||
extPath = await tc.extractZip(downloadPath);
|
||||
} else {
|
||||
extPath = await tc.extractTar(`${tmpdir}/${fileName}`);
|
||||
extPath = await tc.extractTar(downloadPath);
|
||||
}
|
||||
core.debug(`Extracted to ${extPath}`);
|
||||
|
||||
return path.join(extPath, osPlat == 'win32' ? 'goreleaser.exe' : 'goreleaser');
|
||||
const cachePath: string = await tc.cacheDir(extPath, 'goreleaser-action', release.tag_name.replace(/^v/, ''));
|
||||
core.debug(`Cached to ${cachePath}`);
|
||||
|
||||
const exePath: string = path.join(cachePath, osPlat == 'win32' ? 'goreleaser.exe' : 'goreleaser');
|
||||
core.debug(`Exe path is ${exePath}`);
|
||||
|
||||
return exePath;
|
||||
}
|
||||
|
||||
function getFileName(): string {
|
||||
const 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';
|
||||
return util.format('goreleaser_%s_%s.%s', platform, arch, ext);
|
||||
}
|
||||
|
||||
interface GitHubRelease {
|
||||
tag_name: string;
|
||||
}
|
||||
|
||||
async function determineVersion(version: string): Promise<string> {
|
||||
let rest: restm.RestClient = new restm.RestClient('goreleaser-action', 'https://github.com', undefined, {
|
||||
headers: {
|
||||
Accept: 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
let res: restm.IRestResponse<GitHubRelease> = await rest.get<GitHubRelease>(
|
||||
`/goreleaser/goreleaser/releases/${version}`
|
||||
);
|
||||
if (res.statusCode != 200 || res.result === null) {
|
||||
throw new Error(`Cannot find GoReleaser ${version} release (http ${res.statusCode})`);
|
||||
}
|
||||
|
||||
return res.result.tag_name;
|
||||
}
|
||||
};
|
||||
|
||||
+3
-7
@@ -4,7 +4,7 @@ import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
import * as fs from 'fs';
|
||||
|
||||
export async function run(silent?: boolean) {
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
const version = core.getInput('version') || 'latest';
|
||||
const args = core.getInput('args');
|
||||
@@ -37,15 +37,11 @@ export async function run(silent?: boolean) {
|
||||
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
|
||||
});
|
||||
await exec.exec('gpg', ['--import', path]);
|
||||
}
|
||||
|
||||
core.info('🏃 Running GoReleaser...');
|
||||
await exec.exec(`${goreleaser} ${args}${snapshot}`, undefined, {
|
||||
silent: silent
|
||||
});
|
||||
await exec.exec(`${goreleaser} ${args}${snapshot}`);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user