为何Github自定义Action两种调用方式下random-number输出不一致?
问题:调用GitHub Action时,仅本地路径方式能获取输出值的原因
仓库结构
├── .github │ ├── actions │ │ └── hello-world │ │ └── action.yml │ └── workflows │ └── main.yml ├── action.yml └── goodbye.sh
Action定义文件(action.yml)
两个action.yml文件内容一致,代码如下:
name: "Hello World" description: "Greet to my little friend" inputs: who-to-greet: description: "who to greet" required: true default: "World" outputs: random-number: description: "Random number" value: ${{ steps.random-number-generator.outputs.random-number }} runs: using: "composite" steps: - run: echo Hello ${{ inputs.who-to-greet }}. shell: bash - id: random-number-generator run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT shell: bash - run: echo "${{ github.action_path }}" >> $GITHUB_PATH shell: bash - run: goodbye.sh shell: bash
工作流代码(main.yml)
on: push: workflow_dispatch: jobs: hello_world: runs-on: ubuntu-latest name: say hello to my little firend steps: - uses: actions/checkout@v3 - id: foo uses: chaoqunya/hello-world-composite-action@v1 with: who-to-greet: "Chaos" - run: echo random-number ${{ steps.foo.outputs.random-number }} test shell: bash - id: bar uses: ./.github/actions/hello-world with: who-to-greet: "Chaos" - run: echo random-number ${{ steps.bar.outputs.random-number }} test shell: bash
执行结果

原因分析
核心问题是你通过{owner}/{repo}@{ref}调用的v1版本Action,对应的远程仓库代码与本地测试代码不一致:
- 本地路径调用(
./.github/actions/hello-world)用的是当前仓库本地代码,其中action.yml正确定义了输出,random-number-generator步骤能正常执行生成输出值。 - 而外部版本调用(
chaoqunya/hello-world-composite-action@v1)的v1标签大概率指向了旧提交:- 旧提交的
action.yml可能没正确设置outputs字段,或者输出值的引用表达式有误; - 旧提交中缺少
random-number-generator步骤,导致无法生成输出值; - 甚至旧提交中缺失
goodbye.sh,导致Action步骤执行中断,后续输出未被正常设置。
- 旧提交的
另外也可能是你修改本地代码后,没将更新推送到远程仓库,或者没重新创建v1标签指向最新的正确提交(GitHub标签默认绑定固定提交,不会自动跟随分支更新)。
内容的提问来源于stack exchange,提问作者Animeta
相关产品推荐
相关产品推荐

