add support for file_glob
This commit is contained in:
36
src/main.ts
36
src/main.ts
@@ -1,6 +1,9 @@
|
||||
import * as fs from 'fs';
|
||||
import * as core from '@actions/core';
|
||||
import * as github from '@actions/github';
|
||||
import * as path from 'path';
|
||||
|
||||
const glob = require("glob")
|
||||
|
||||
async function get_release_by_tag(tag: string, octokit: any, context: any): Promise<any> {
|
||||
try {
|
||||
@@ -24,7 +27,12 @@ async function get_release_by_tag(tag: string, octokit: any, context: any): Prom
|
||||
}
|
||||
|
||||
async function upload_to_release(release: any, file: string, asset_name: string, tag: string, overwrite: string, octokit: any, context: any) {
|
||||
const file_size = fs.statSync(file).size;
|
||||
const stat = fs.statSync(file);
|
||||
if (!stat.isFile()) {
|
||||
core.debug(`Skipping ${file}, since its not a file`);
|
||||
return;
|
||||
}
|
||||
const file_size = stat.size;
|
||||
const file_bytes = fs.readFileSync(file);
|
||||
|
||||
// Check for duplicates.
|
||||
@@ -64,19 +72,31 @@ async function run() {
|
||||
try {
|
||||
const token = core.getInput('repo_token', { required: true });
|
||||
const file = core.getInput('file', { required: true });
|
||||
const asset_name = core.getInput('asset_name', { required: true });
|
||||
const file_glob = core.getInput('file_glob');
|
||||
const tag = core.getInput('tag', { required: true }).replace("refs/tags/", "");
|
||||
const overwrite = core.getInput('overwrite');
|
||||
|
||||
if (!fs.existsSync(file)) {
|
||||
core.setFailed(`File ${file} wasn't found.`);
|
||||
}
|
||||
|
||||
const octokit = new github.GitHub(token);
|
||||
const context = github.context;
|
||||
|
||||
const release = await get_release_by_tag(tag, octokit, context);
|
||||
await upload_to_release(release, file, asset_name, tag, overwrite, octokit, context);
|
||||
|
||||
if (file_glob === "true") {
|
||||
const files = glob.sync(file);
|
||||
if (files.length > 0) {
|
||||
for (let i = 0; i < files.length; i += 1) {
|
||||
const file = files[i];
|
||||
const asset_name = path.basename(file);
|
||||
await upload_to_release(release, file, asset_name, tag, overwrite, octokit, context);
|
||||
}
|
||||
}
|
||||
else {
|
||||
core.setFailed("No files matching the glob pattern found.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
const asset_name = core.getInput('asset_name', { required: true });
|
||||
await upload_to_release(release, file, asset_name, tag, overwrite, octokit, context);
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user