如何从 java 脚本执行 bash-脚本

How to execute a bash-script from a java script

我想在我的一些存储库之间共享 GitHub 操作,这些存储库现在在每个存储库中包含一个发布 bash 脚本。

为了能够 运行 相同的脚本,我需要一个 Github 操作才能做到这一点。

我对 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?

目前,唯一可能的 types of actions 是 Javascript 和 Docker 容器操作。

所以你的选择是:

  1. 在 Docker 容器操作中执行 bash 脚本
  2. 从 Javascript 操作执行 bash 脚本。 @actions/exec package of the actions/toolkit 就是为执行此操作而设计的——执行工具和脚本。

这就是如何从 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();