首页
/ GitHub Action SSH 项目启动与配置教程

GitHub Action SSH 项目启动与配置教程

2025-05-19 14:05:40作者:江焘钦

1. 项目目录结构及介绍

GitHub Action SSH 项目是一个简单的 GitHub Action,用于通过 SSH 在远程服务器上执行命令。以下是项目的目录结构及其简要介绍:

  • .github/
    • GitHub Actions 的工作流文件存放目录,其中定义了自动化任务。
  • dist/
    • 编译后的文件存放目录。
  • lib/
    • 项目依赖库存放目录。
  • src/
    • 源代码存放目录,包含了实现 SSH 连接和命令执行的核心代码。
  • .gitignore
    • 定义了 Git 忽略文件列表。
  • .node-version
    • 指定了项目使用的 Node.js 版本。
  • .prettierignore
    • 定义了 Prettier 忽略文件列表。
  • .prettierrc.json
    • Prettier 配置文件。
  • LICENSE
    • 项目许可证文件,本项目采用 MIT 许可。
  • README-zh.md
    • 中文项目自述文件。
  • README.md
    • 英文项目自述文件。
  • action.yml
    • GitHub Action 的配置文件。
  • package-lock.json
    • 项目的包锁定文件。
  • package.json
    • 项目的包描述文件,定义了项目依赖和脚本等。
  • result.png
    • 项目示例结果图片。
  • tsconfig.json
    • TypeScript 配置文件。

2. 项目的启动文件介绍

项目的启动主要通过 .github/workflows 目录下的 YAML 文件定义。以下是一个简单的启动文件示例:

name: Example Workflow

on: [push]

jobs:
  ssh-command:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      
      - name: Run SSH command
        uses: garygrossgarten/github-action-ssh@release
        with:
          command: ls -a
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          sshKey: ${{ secrets.SSH_KEY }}

在这个文件中,我们定义了一个 GitHub Actions 工作流,当代码被 push 到仓库时触发。工作流中包含了一个步骤,该步骤使用了 garygrossgarten/github-action-ssh Action 来在远程服务器上执行 ls -a 命令。

3. 项目的配置文件介绍

项目的配置主要通过 action.yml 文件进行。以下是 action.yml 的一个示例:

name: 'GitHub Action SSH'
description: 'Simple GitHub Action to run a command on a remote server using SSH.'
inputs:
  command:
    description: 'The command to run on the remote server.'
    required: true
  host:
    description: 'Hostname or IP address of the server.'
    default: 'localhost'
  port:
    description: 'Port number of the server.'
    default: 22
  username:
    description: 'Username for authentication.'
  password:
    description: 'Password for password-based user authentication.'
  sshKey:
    description: 'SSH key for key-based or hostbased user authentication (OpenSSH format).'
  passphrase:
    description: 'Passphrase for an encrypted SSH key.'
  tryKeyboard:
    description: 'Try keyboard-interactive user authentication if primary user authentication method fails.'
    default: false

action.yml 文件中,我们定义了 Action 的名称、描述和输入参数。用户在使用此 Action 时可以通过输入参数来配置远程服务器的连接细节和要执行的命令。默认值提供了基本配置,而用户可以根据需要覆盖这些默认值。

登录后查看全文
热门项目推荐