GitHub Actions报错:Unrecognized named-value: 'github' 求助
解决方案
核心问题
可复用工作流(被其他工作流调用的)不能通过github.events.inputs获取调用方传递的参数,正确的引用方式是直接使用inputs.xxx,同时必须在工作流的on: workflow_call中显式定义输入参数(仅workflow_dispatch无法支持跨工作流调用的参数传递)。
具体修改步骤
更新工作流触发配置,添加
workflow_call并定义输入参数
在on字段中同时保留workflow_dispatch(支持手动触发)和workflow_call(支持被其他工作流调用),两者的输入参数定义保持一致:on: workflow_dispatch: inputs: context: description: The context used for the "docker build" command. required: true dockerfile: description: The dockerfile used for the "docker build" command. required: true image_name: description: The name of the image to publish required: true registry: description: The registry where the image will be uploaded. required: true registry-user: description: The username used for the login. required: true registry-password: description: The password used for the login to the registry. required: true workflow_call: inputs: context: description: The context used for the "docker build" command. required: true type: string dockerfile: description: The dockerfile used for the "docker build" command. required: true type: string image_name: description: The name of the image to publish required: true type: string registry: description: The registry where the image will be uploaded. required: true type: string registry-user: description: The username used for the login. required: true type: string registry-password: description: The password used for the login to the registry. required: true type: string替换所有参数引用为
inputs.xxx
将代码中所有github.events.inputs.xxx替换为inputs.xxx,例如:- 原
context: ${{ github.events.inputs.context }}改为context: ${{ inputs.context }} - 原
tags: ${{ github.events.inputs.image_name }}:${{ github.sha }}改为tags: ${{ inputs.image_name }}:${{ github.sha }}
- 原
处理发布标签的上下文(可选)
如果需要在可复用工作流中获取调用方的release标签,github.event.release.tag_name仅在调用方的工作流由release事件触发时可用,无需修改(该上下文属于触发调用方工作流的事件,可正常访问)。
修改后的完整工作流代码
name: VewDockerAction description: Easily builds and publishes Docker images. See readme.md at VewTech/Vew-Docker-Action on: workflow_dispatch: inputs: context: description: The context used for the "docker build" command. required: true dockerfile: description: The dockerfile used for the "docker build" command. required: true image_name: description: The name of the image to publish required: true registry: description: The registry where the image will be uploaded. required: true registry-user: description: The username used for the login. required: true registry-password: description: The password used for the login to the registry. required: true workflow_call: inputs: context: description: The context used for the "docker build" command. required: true type: string dockerfile: description: The dockerfile used for the "docker build" command. required: true type: string image_name: description: The name of the image to publish required: true type: string registry: description: The registry where the image will be uploaded. required: true type: string registry-user: description: The username used for the login. required: true type: string registry-password: description: The password used for the login to the registry. required: true type: string jobs: build-and-publish: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Build Docker image uses: docker/build-push-action@v2 if: github.event_name != 'release' with: context: ${{ inputs.context }} file: ${{ inputs.dockerfile }} push: false tags: ${{ inputs.image_name }}:${{ github.sha }} - name: Login to Registry if: github.event_name == 'release' uses: docker/login-action@v1 with: registry: ${{ inputs.registry }} username: ${{ inputs.registry-user }} password: ${{ inputs.registry-password }} - name: Build and Push Docker image if: github.event_name == 'release' uses: docker/build-push-action@v2 with: context: ${{ inputs.context }} file: ${{ inputs.dockerfile }} push: true tags: | ${{ inputs.registry }}/${{ inputs.image_name }}:${{ github.event.release.tag_name }} ${{ inputs.registry }}/${{ inputs.image_name }}:latest
调用方式无需修改
你原来的调用代码保持不变即可,调用方通过with传递的参数会自动映射到可复用工作流的inputs对象中。
内容的提问来源于stack exchange,提问作者Josep Sendra
相关产品推荐
相关产品推荐

