You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将文件内容作为参数传入自定义GitHub Action?

解决方法

1. 现有Action下的快速实现(无需修改Action)

不能直接传入文件路径,必须先读取.sql文件内容再传递给Action的args参数。可以通过GitHub Actions的命令行步骤读取文件,将内容存入输出变量后传给你的Action:

jobs:
  setup:
    name: MySQL Database Setup
    runs-on: [self-hosted]

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Read SQL file content
        id: read_sql
        run: echo "content=$(cat ./path/to/your/file.sql)" >> "$GITHUB_OUTPUT"

      - name: Execute mysql commands
        uses: myaction@main
        env: 
          MYSQL_USERNAME: root
          MYSQL_PASSWORD: root
          MYSQL_ADRESS: some.mysql.server.domain.com
          MYSQL_PORT: 10207
        with:
          args: "${{ steps.read_sql.outputs.content }}"

如果SQL文件包含换行、引号等特殊字符,用jq转义可以避免格式问题:

echo "content=$(jq -Rs . ./path/to/your/file.sql)" >> "$GITHUB_OUTPUT"

传递时无需额外加引号,因为jq已将内容转为合法的JSON字符串。

2. 更优雅的实现:修改自定义Action支持文件路径参数

这是长期友好的方案,让Action自行处理文件读取,用户只需传入文件路径即可。

步骤1:修改Action的action.yml,新增sql-file参数

name: 'MySQL Executor'
description: 'Connect to MySQL and execute statements'
inputs:
  args:
    description: 'MySQL statements to execute'
    required: false
  sql-file:
    description: 'Path to SQL file to execute'
    required: false
env:
  MYSQL_USERNAME:
    required: true
  MYSQL_PASSWORD:
    required: true
  MYSQL_ADRESS:
    required: true
  MYSQL_PORT:
    required: true
runs:
  using: 'node16' # 或你使用的其他运行环境(如docker)
  main: 'index.js' # 对应你的Action执行脚本

步骤2:在Action执行脚本中处理文件读取

假设你的Action用Node.js编写,修改index.js:

const core = require('@actions/core');
const mysql = require('mysql2/promise');

async function run() {
  try {
    // 获取参数
    const args = core.getInput('args');
    const sqlFile = core.getInput('sql-file');
    
    let sqlContent = '';
    if (sqlFile) {
      // 读取SQL文件内容
      const fs = require('fs');
      sqlContent = fs.readFileSync(sqlFile, 'utf8');
    } else if (args) {
      sqlContent = args;
    } else {
      core.setFailed('Either args or sql-file must be provided');
      return;
    }

    // 连接MySQL并执行
    const connection = await mysql.createConnection({
      host: process.env.MYSQL_ADRESS,
      port: process.env.MYSQL_PORT,
      user: process.env.MYSQL_USERNAME,
      password: process.env.MYSQL_PASSWORD,
      multipleStatements: true // 支持执行多语句SQL文件
    });

    await connection.execute(sqlContent);
    await connection.end();
  } catch (error) {
    core.setFailed(error.message);
  }
}

run();

步骤3:Workflow中直接使用文件路径参数

jobs:
  setup:
    name: MySQL Database Setup
    runs-on: [self-hosted]

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Execute SQL file
        uses: myaction@main
        env: 
          MYSQL_USERNAME: root
          MYSQL_PASSWORD: root
          MYSQL_ADRESS: some.mysql.server.domain.com
          MYSQL_PORT: 10207
        with:
          sql-file: './path/to/your/file.sql' # 直接传文件路径

补充说明

  • 若你的Action基于Docker镜像编写,逻辑类似:在镜像的入口脚本中判断是否传入文件路径参数,用cat读取内容后执行。
  • 确保MySQL客户端支持多语句执行(如上述Node.js示例中的multipleStatements: true配置),避免SQL文件包含多语句时执行失败。

内容的提问来源于stack exchange,提问作者Roland Deschain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:05:34