How to execute a bash-script from a java script

主宰稳场 提交于 2019-12-11 08:22:03

问题


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:

  1. Execute the bash script in a Docker container action
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!