手动触发GitHub Actions跨私有仓库工作流遇参数错误问题咨询
GitHub Actions跨仓库触发报错解决:Unexpected inputs provided
问题场景
两个私有GitHub仓库,手动运行第一个仓库的Workflow触发第二个仓库的Actions时,触发环节出现以下错误:
{
"message": "Unexpected inputs provided: ["repository", "commit", "env"]",
"documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}
第一个仓库的Workflow配置
on: workflow_dispatch: inputs: repository: description: 'Name of triggered repo' required: true default: '123re' type: string commit: description: 'Short commit' required: true type: string default: 'e123' env: description: 'Environment' required: true type: choice options: - dev - staging - prod jobs: ping-pong: runs-on: ubuntu-latest outputs: output1: ${{ steps.vars.outputs.sha_short }} steps: - name: Checkout uses: actions/checkout@v3 - name: Set outputs id: vars run: | echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT - name: Trigger Workflow if: ${{ (inputs.env) == 'dev' }} run: | curl -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${{ secrets.ACCESS_TOKEN }}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/owner/repo/actions/workflows/test-connection.yml/dispatches \ -d '{"ref":"main","inputs":{"repository":"$REPO","commit":"$COMMIT","env":"$ENV"}}' env: ENV: ${{ inputs.env }} COMMIT: ${{ steps.vars.outputs.sha_short }} REPO: ${{ inputs.repository }}
第二个仓库的Workflow配置(原版本)
name: Remote Dispatch Action Responder on: [repository_dispatch, workflow_dispatch] jobs: ping-pong: runs-on: ubuntu-latest steps: - name: Event Information run: | echo '${{ github.event.inputs.commit }}' && echo "Event '${{ github.event.inputs.env }}' received from '${{ github.event.inputs.repository }}'"
错误原因
触发报错的核心问题是:第二个仓库的Workflow没有在workflow_dispatch事件中定义对应的inputs参数。
通过GitHub API调用workflow_dispatch事件时,传入的inputs字段必须和目标仓库Workflow里on.workflow_dispatch.inputs中定义的字段完全匹配。如果目标Workflow没有声明这些输入字段,GitHub API就会认为是非法输入,返回"Unexpected inputs provided"错误。
修复方案
1. 修正第二个仓库的Workflow配置
为workflow_dispatch添加与第一个仓库传递的字段完全匹配的inputs定义:
name: Remote Dispatch Action Responder on: repository_dispatch: workflow_dispatch: inputs: repository: description: '触发来源仓库名称' required: true type: string commit: description: '短提交哈希' required: true type: string env: description: '环境' required: true type: choice options: - dev - staging - prod jobs: ping-pong: runs-on: ubuntu-latest steps: - name: 打印事件信息 run: | echo '${{ github.event.inputs.commit }}' && echo "收到来自'${{ github.event.inputs.repository }}'的环境'${{ github.event.inputs.env }}'触发事件"
2. 其他检查项
- 确认第一个仓库API调用中的
ref参数正确,目标仓库确实存在main分支(如果是master或其他分支名,需对应修改)。 - 验证
ACCESS_TOKEN权限:该Token必须拥有目标私有仓库的repo权限,且具备触发Workflow的权限。
兼容两种触发方式的可选优化
如果需要同时支持repository_dispatch和workflow_dispatch,注意两者的输入获取方式不同:
workflow_dispatch的输入通过github.event.inputs.xxx获取repository_dispatch的输入需放在client_payload中,通过github.event.client_payload.xxx获取
可以调整第二个仓库的步骤代码实现兼容:
- name: 打印事件信息 run: | if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then echo '${{ github.event.inputs.commit }}' && echo "收到来自'${{ github.event.inputs.repository }}'的环境'${{ github.event.inputs.env }}'触发事件" elif [ "${{ github.event_name }}" = "repository_dispatch" ]; then echo '${{ github.event.client_payload.commit }}' && echo "收到来自'${{ github.event.client_payload.repository }}'的环境'${{ github.event.client_payload.env }}'触发事件" fi
内容的提问来源于stack exchange,提问作者ChoosenOne
相关产品推荐
相关产品推荐

