【问题标题】:How to execute a bash-script from a java script如何从 java 脚本执行 bash 脚本
【发布时间】:2019-11-22 19:04:36
【问题描述】:

我想在我的一些存储库之间共享 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?

【问题讨论】:

    标签: git github github-actions


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      所以你的选择是:

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

      【讨论】:

      • 我非常感谢一个如何使用@actions/exec 执行脚本的示例
      • 今后,请不要更改问题并收回接受答案。如果您有后续问题,您应该提出一个新问题。如果我有时间,我会尝试使用@actions/exec 做一个例子。同时,您可以查看this code,我正在使用 Javascript 操作执行 Python。
      最近更新 更多