首页 > 解决方案 > 如何从 java 脚本执行 bash 脚本

问题描述

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

标签: gitgithubgithub-actions

解决方案


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

推荐阅读