Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hocgin committed Aug 23, 2024
1 parent 99726d9 commit 1a9a8ca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# action-json-file-to-env
将指定 .json 文件加载为环境参数,支持 url 地址
> 将指定 .json 文件加载为环境参数,支持 url 地址
> 支持 github 和 local
例如: 将 https://github.com/hocgin/.github/workflows/env.json 的内容设置为多个变量 `REMOTE_HOST` / `REMOTE_PORT`


## 如何使用
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ inputs:
debug:
required: false
description: 'debug mode'
type:
required: false
description: 'eg: github, local'
default: 'github'
file:
required: false
description: '.json file path, eg: .github/workflows/env.json'
#
owner:
required: false
description: 'resp owner'
Expand Down
51 changes: 32 additions & 19 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {info, warning, getInput} from "@actions/core";
import * as github from '@actions/github';
import {Inputs, Outputs} from "./main";
import path from "path";
import fs from "fs";


const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
Expand Down Expand Up @@ -32,33 +34,44 @@ async function getBranch(branch: string, repo: string): Promise<string> {
}

export async function run(input: Inputs): Promise<Outputs> {
const crepo = github.context.repo;
let env = {};
let env = {} as any;
let fileContent;
let file = input?.file?.trim();
file = file?.length ? file : ".github/workflows/env.json";

let owner: string = input?.owner?.trim() ?? crepo.owner;
owner = owner?.length ? owner : crepo.owner;
if (input?.type === 'local') {
let baseDir = process.cwd();
const absPath = path.join(baseDir, path.dirname(file), path.basename(file));
if (!fs.existsSync(absPath)) {
warning(`not found file. absPath = ${absPath}`)
} else {
fileContent = fs.readFileSync(absPath).toString();
}
} else {
const crepo = github.context.repo;

let owner: string = input?.owner?.trim() ?? crepo.owner;
owner = owner?.length ? owner : crepo.owner;

let repo = input?.repo?.trim() ?? crepo.repo;
repo = repo?.length ? repo : crepo.repo;
let repo = input?.repo?.trim() ?? crepo.repo;
repo = repo?.length ? repo : crepo.repo;

let _branch = input.branch?.trim() ?? github.context.ref;
_branch = _branch?.length ? _branch : github.context.ref;
let _branch = input.branch?.trim() ?? github.context.ref;
_branch = _branch?.length ? _branch : github.context.ref;

const branch = await getBranch(_branch, repo);
const branch = await getBranch(_branch, repo);

info(`${tag("🟡 QUEUE")} read file content`);
info(`${tag("🟡 QUEUE")} read file content`);

let currentFile = await getFileContents(branch, owner, repo, file);
if (currentFile && 'content' in currentFile) {
const fileContent = nodeBase64ToUtf8(currentFile.content || '');
env = JSON.parse(fileContent);
if (input?.debug) {
console.log('env=', env);
let currentFile = await getFileContents(branch, owner, repo, file);
if (currentFile && 'content' in currentFile) {
fileContent = nodeBase64ToUtf8(currentFile.content || '');
}
}
return {
...env
};
env = JSON.parse(fileContent);
if (input?.debug) {
console.log('env=', env);
}

return {...env};
}
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as core from "@actions/core";
export interface Inputs {
debug?: boolean;
file?: string;
type?: 'github' | 'local';
//
owner?: string;
repo?: string;
branch?: string;
Expand All @@ -18,6 +20,8 @@ export interface Outputs {
let getInput = (): Inputs => ({
debug: core.getInput('debug') === 'true',
file: core.getInput('file', {required: false}),
type: core.getInput('type', {required: false}) as any,
//
owner: core.getInput('owner', {required: false}),
repo: core.getInput('repo', {required: false}),
branch: core.getInput('branch', {required: false}),
Expand Down

0 comments on commit 1a9a8ca

Please sign in to comment.