Skip to main content
⚠️ 本文档由 AI 自动翻译。如有任何不准确之处,请参考英文原版

背景

更新其他人正在使用的插件可能很繁琐。传统方式需要修改代码、更新版本、推送更改、创建分支、打包文件并手动提交 PR——这是一个重复性的流程,会拖慢开发进度。 因此,我们创建了 Plugin Auto-PR,这是一个 GitHub Actions 工作流,可以自动化整个流程。现在你只需一个操作就能完成打包、推送和创建 PR,专注于构建优秀的插件。

概念

GitHub Actions

GitHub Actions 可以在 GitHub 中自动化你的开发任务。 工作原理:当触发时(例如,通过代码推送),它会在基于云的虚拟机中运行你的工作流,自动处理从构建到部署的所有事项。 Workflow 限制
  • 公开仓库:无限制
  • 私有仓库:每月 2000 分钟

Plugin Auto-PR

工作原理
  1. 当你将代码推送到插件源代码仓库的主分支时,工作流会被触发
  2. 工作流从 manifest.yaml 文件读取插件信息
  3. 自动将插件打包为 .difypkg 文件
  4. 将打包后的文件推送到你 fork 的 dify-plugins 仓库
  5. 创建新分支并提交更改
  6. 自动创建 PR 以合并到上游仓库

前提条件

仓库

  • 你已经有自己的插件源代码仓库(例如,your-name/plugin-source
  • 你已经有自己 fork 的插件仓库(例如,your-name/dify-plugins
  • 你 fork 的仓库已经有插件目录结构:
dify-plugins/
└── your-author-name
    └── plugin-name

权限

此工作流需要适当的权限才能运行:
  • 你需要创建一个具有足够权限的 GitHub 个人访问令牌(PAT)
  • PAT 必须有权限将代码推送到你 fork 的仓库
  • PAT 必须有权限向上游仓库创建 PR

参数和配置

设置要求

要开始自动发布,你需要两个关键组件: manifest.yaml 文件:此文件驱动自动化流程:
  • name:你的插件名称(影响包和分支名称)
  • version:语义版本号(每次发布时递增)
  • author:你的 GitHub 用户名(决定仓库路径)
PLUGIN_ACTION Secret:你需要将此 secret 添加到你的插件源代码仓库:
  • 值:必须是具有足够权限的个人访问令牌(PAT)
  • 权限:能够将分支推送到你 fork 的仓库并向上游仓库创建 PR

自动生成的参数

设置完成后,工作流会自动处理这些参数:
  • GitHub 用户名:从 manifest.yaml 中的 author 字段读取
  • 作者文件夹名称:与 author 字段一致
  • 插件名称:从 manifest.yaml 中的 name 字段读取
  • 分支名称:bump-{plugin-name}-plugin-{version}
  • 包文件名:{plugin-name}-{version}.difypkg
  • PR 标题和内容:根据插件名称和版本自动生成

分步指南

1

准备仓库

确保你已经 fork 了官方的 dify-plugins 仓库,并且有自己的插件源代码仓库。
2

配置 Secret

导航到你的插件源代码仓库,点击 Settings > Secrets and variables > Actions > New repository secret,然后创建一个 GitHub Secret:
  • 名称:PLUGIN_ACTION
  • 值:具有目标仓库(your-name/dify-plugins)写入权限的 GitHub 个人访问令牌(PAT)
Create Secrets
3

创建工作流文件

在你的仓库中创建 .github/workflows/ 目录,在此目录中创建名为 plugin-publish.yml 的文件,并将以下内容复制到文件中:
# .github/workflows/auto-pr.yml
name: Auto Create PR on Main Push

on:
  push:
    branches: [ main ]  # Trigger on push to main

jobs:
  create_pr: # Renamed job for clarity
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Print working directory # Kept for debugging
        run: |
          pwd
          ls -la

      - name: Download CLI tool
        run: |
          # Create bin directory in runner temp
          mkdir -p $RUNNER_TEMP/bin
          cd $RUNNER_TEMP/bin

          # Download CLI tool
          wget https://github.com/langgenius/dify-plugin-daemon/releases/latest/download/dify-plugin-linux-amd64
          chmod +x dify-plugin-linux-amd64

          # Show download location and file
          echo "CLI tool location:"
          pwd
          ls -la dify-plugin-linux-amd64

      - name: Get basic info from manifest # Changed step name and content
        id: get_basic_info
        run: |
          PLUGIN_NAME=$(grep "^name:" manifest.yaml | cut -d' ' -f2)
          echo "Plugin name: $PLUGIN_NAME"
          echo "plugin_name=$PLUGIN_NAME" >> $GITHUB_OUTPUT

          VERSION=$(grep "^version:" manifest.yaml | cut -d' ' -f2)
          echo "Plugin version: $VERSION"
          echo "version=$VERSION" >> $GITHUB_OUTPUT

          # If the author's name is not your github username, you can change the author here
          AUTHOR=$(grep "^author:" manifest.yaml | cut -d' ' -f2)
          echo "Plugin author: $AUTHOR"
          echo "author=$AUTHOR" >> $GITHUB_OUTPUT

      - name: Package Plugin
        id: package
        run: |
          # Use the downloaded CLI tool to package
          cd $GITHUB_WORKSPACE
          # Use variables for package name
          PACKAGE_NAME="${{ steps.get_basic_info.outputs.plugin_name }}-${{ steps.get_basic_info.outputs.version }}.difypkg"
          # Use CLI from runner temp
          $RUNNER_TEMP/bin/dify-plugin-linux-amd64 plugin package . -o "$PACKAGE_NAME"

          # Show packaging result
          echo "Package result:"
          ls -la "$PACKAGE_NAME"
          echo "package_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT

          # Show full file path and directory structure (kept for debugging)
          echo "\\nFull file path:"
          pwd
          echo "\\nDirectory structure:"
          tree || ls -R

      - name: Checkout target repo
        uses: actions/checkout@v3
        with:
          # Use author variable for repository
          repository: ${{steps.get_basic_info.outputs.author}}/dify-plugins
          path: dify-plugins
          token: ${{ secrets.PLUGIN_ACTION }}
          fetch-depth: 1 # Fetch only the last commit to speed up checkout
          persist-credentials: true # Persist credentials for subsequent git operations

      - name: Prepare and create PR
        run: |
          # Debug info (kept)
          echo "Debug: Current directory $(pwd)"
          # Use variable for package name
          PACKAGE_NAME="${{ steps.get_basic_info.outputs.plugin_name }}-${{ steps.get_basic_info.outputs.version }}.difypkg"
          echo "Debug: Package name: $PACKAGE_NAME"
          ls -la

          # Move the packaged file to the target directory using variables
          mkdir -p dify-plugins/${{ steps.get_basic_info.outputs.author }}/${{ steps.get_basic_info.outputs.plugin_name }}
          mv "$PACKAGE_NAME" dify-plugins/${{ steps.get_basic_info.outputs.author }}/${{ steps.get_basic_info.outputs.plugin_name }}/

          # Enter the target repository directory
          cd dify-plugins

          # Configure git
          git config user.name "GitHub Actions"
          git config user.email "[email protected]"

          # Ensure we are on the latest main branch
          git fetch origin main
          git checkout main
          git pull origin main

          # Create and switch to a new branch using variables and new naming convention
          BRANCH_NAME="bump-${{ steps.get_basic_info.outputs.plugin_name }}-plugin-${{ steps.get_basic_info.outputs.version }}"
          git checkout -b "$BRANCH_NAME"

          # Add and commit changes (using git add .)
          git add .
          git status # for debugging
          # Use variables in commit message
          git commit -m "bump ${{ steps.get_basic_info.outputs.plugin_name }} plugin to version ${{ steps.get_basic_info.outputs.version }}"

          # Push to remote (use force just in case the branch existed before from a failed run)
          git push -u origin "$BRANCH_NAME" --force

          # Confirm branch has been pushed and wait for sync (GitHub API might need a moment)
          git branch -a
          echo "Waiting for branch to sync..."
          sleep 10  # Wait 10 seconds for branch sync

      - name: Create PR via GitHub API
        env:
          GH_TOKEN: ${{ secrets.PLUGIN_ACTION }} # Use the provided token for authentication
        run: |
          gh pr create \
            --repo langgenius/dify-plugins \
            --head "${{ steps.get_basic_info.outputs.author }}:${{ steps.get_basic_info.outputs.plugin_name }}-${{ steps.get_basic_info.outputs.version }}" \
            --base main \
            --title "bump ${{ steps.get_basic_info.outputs.plugin_name }} plugin to version ${{ steps.get_basic_info.outputs.version }}" \
            --body "bump ${{ steps.get_basic_info.outputs.plugin_name }} plugin package to version ${{ steps.get_basic_info.outputs.version }}

            Changes:
            - Updated plugin package file" || echo "PR already exists or creation skipped." # Handle cases where PR already exists

      - name: Print environment info # Kept for debugging
        run: |
          echo "GITHUB_WORKSPACE: $GITHUB_WORKSPACE"
          echo "Current directory contents:"
          ls -R
4

更新 manifest.yaml

确保以下字段正确设置:
version: 0.0.x  # Version number
author: your-github-username  # GitHub username/Author name
name: your-plugin-name  # Plugin name

使用指南

首次设置

首次设置自动发布工作流时,请完成以下步骤:
  1. 确保你已经 fork 了官方的 dify-plugins 仓库
  2. 确保你的插件源代码仓库结构正确
  3. 在你的插件源代码仓库中设置 PLUGIN_ACTION Secret
  4. 创建工作流文件 .github/workflows/plugin-publish.yml
  5. 确保 manifest.yaml 文件中的 nameauthor 字段配置正确

后续更新

设置完成后,发布新版本:
  1. 修改代码
  2. 更新 manifest.yaml 中的 version 字段
Release
  1. 将所有更改推送到主分支
  2. 等待 GitHub Actions 完成打包、分支创建和 PR 提交

结果

当你将代码推送到插件源代码仓库的主分支时,GitHub Actions 将自动执行发布流程:
  • {plugin-name}-{version}.difypkg 格式打包插件
  • 将打包后的文件推送到目标仓库
  • 创建 PR 以合并到 fork 仓库
Outcome

示例仓库

查看示例仓库以了解配置和最佳实践。
编辑此页面 | 报告问题