如何从Java脚本执行bash脚本 [英] How to execute a bash-script from a java script
问题描述
我想在我的一些存储库之间共享GitHub动作,现在每个存储库中都包含一个发布bash脚本.
I would like to share GitHub actions between some of my repositories which now contain a release bash script in each repository.
为了能够运行相同的脚本,我需要执行Github操作.
In order to be able to run the same script, I need a Github Action in order to do that.
我对javascript不太了解,无法重写简单的hello world javascript操作(
I have little knowledge of javascript and am unable to rewrite the simple hello world javascript action (https://github.com/actions/hello-world-javascript-action/blob/master/index.js) to run a bash script.
首选使用javascript作为动作的想法,因为它的性能好并且可以访问GitHub webhook有效负载.
The idea of using a javascript as an action is preferred because of its performance and providing access to The GitHub webhook payload.
我第一次尝试根据hello-world动作提供javascript动作:
My first attempt of providing a javascript action based on the hello-world action:
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?
How do I execute the javascript from the console?
推荐答案
这是一种可以通过JavaScript操作执行bash脚本的方法.脚本文件为index.js
This is how one can execute a bash script from a javascript action. Script-file is 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();
这篇关于如何从Java脚本执行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!