git - 如何从 java 脚本执行 bash 脚本
问题描述
我想在我的一些存储库之间共享 GitHub 操作,这些存储库现在在每个存储库中都包含一个发布 bash 脚本。
为了能够运行相同的脚本,我需要一个 Github Action 来执行此操作。
我对 javascript 知之甚少,无法重写简单的 hello world javascript 操作(https://github.com/actions/hello-world-javascript-action/blob/master/index.js)来运行 bash 脚本。
使用 javascript 作为操作的想法是首选,因为它的性能和提供对 GitHub webhook 有效负载的访问。
我第一次尝试提供基于 hello-world 操作的 javascript 操作:
const exec = require('@actions/exec');
const core = require('@actions/core');
const github = require('@actions/github');
try {
const filepath = core.getInput('file-path');
console.log(`testing ${filepath`});
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2);
console.log(`The event payload: ${payload}`);
exec.exec('./test')
} catch (error) {
core.setFailed(error.message);
}
如何从控制台执行 javascript?
解决方案
这就是从 javascript 操作执行 bash 脚本的方式。脚本文件是index.js
const core = require("@actions/core");
const exec = require("@actions/exec");
const github = require("@actions/github");
async function run() {
try {
// Set the src-path
const src = __dirname + "/src";
core.debug(`src: ${src}`);
// Fetch the file path from input
const filepath = core.getInput("file-path");
core.debug(`input: ${filepath}`);
// Execute bash script
await exec.exec(`${src}/test`);
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2);
console.debug(`github event payload: ${payload}`);
} catch (error) {
core.setFailed(error.message);
}
}
// noinspection JSIgnoredPromiseFromCall
run();
推荐阅读
- javascript - JavaScript 不适用于 HTML 文件 (Express)
- javascript - react native:将类组件更改为功能组件和挂钩的方法是什么?
- mongodb - mongodb - 将嵌套对象数组合并为一个保留所有数据的数组
- ruby - 我正在尝试通过 rbenv build on mac os catalina 安装 ruby-2.2.3 但收到以下错误
- firebase - Firebase 抛出“服务器以状态 400 响应”
- angular - Angular 指令更改元素的输入
- javascript - Mongo 认为 10 月 1 日和 2 日是不同的月份
- azure-sql-database - CTE 不适用于 azure sql,但适用于普通 sql server
- algorithm - 仅使用单个堆栈的 ABCD 的可能输出序列?
- postgresql - Postgresql:将选择查询的结果传递给 jsonb_to_recordset 不起作用