问题
I would like to share GitHub actions between some of my repositories which now contain a release bash script in each repository.
In order to be able to run the same script, I need a Github Action in order to do that.
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.
The idea of using a javascript as an action is preferred because of its performance and providing access to The GitHub webhook payload.
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);
}
How do I execute the javascript from the console?
回答1:
Currently, the only possible types of actions are Javascript and Docker container actions.
So your options are either:
- Execute the bash script in a Docker container action
- Execute the bash script from a Javascript action. The @actions/exec package of the actions/toolkit is designed to do exactly this—execute tools and scripts.
回答2:
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();
来源:https://stackoverflow.com/questions/58976112/how-to-execute-a-bash-script-from-a-java-script