如何访问 GitHub 可重用操作的输出?

问题描述 投票:0回答:1

我正在尝试执行一个可重用的操作,该操作能够获取自上次推送以来修改的某种类型的所有文件。该操作完美运行,其输出如下(其中包括用于测试的调试消息):

echo "DEBUG TEST: $FILTERED_PROJECTS"
echo "modified-files=$FILTERED_PROJECTS" >> $GITHUB_OUTPUT

DEBUG TEST
正确输出了
$FILTERED_PROJECTS
,通过日志可以看出:

DEBUG TEST: _test/folder_noskip/test1.txt,_test/folder_noskip/test2.txt

然后我做了一个测试工作流程如下:

name: Get Modified Files

on:
  push:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Action Test
        id: action-test
        uses: user/get-files@v1-alpha
        with:
          commits-json: ${{ toJson(github.event.commits) }}
          git-branch: ${{ github.ref_name }}
          file-type: ".txt"
          file-ignore: ".fileignore"
          
      - name: Check result
        id: result-check
        run: |
          echo "Outputs: ${{ toJson(steps.action-test.outputs) }}"
          echo "Outputted files:"
          MODIFIED_FILES=$(echo ${{ steps.action-test.outputs.modified-files }} | tr ',' '\n')
          echo "$MODIFIED_FILES"
          echo "Raw: ${{ steps.action-test.outputs.modified-files }}"

action-test
步骤按预期记录所有内容,但
result-check
无法获得
action-test
的输出,这可以通过其日志看到:

Outputs: {}
Outputted files:

Raw: 

这可能是可重用操作如何输出其数据的问题,因此我在打开

[DEBUG]
消息的情况下重新运行工作流程。操作完成后记录以下内容:

##[debug]Set output modified-files = _test/folder_noskip/test1.txt,_test/folder_noskip/test2.txt

因此,看起来该操作正确输出,但

result-check
步骤完全无法获取它。

我访问输出的方式有什么问题吗?如果不是,那么还有什么阻止工作流程正确获取输出?任何帮助将不胜感激!

完整的action.yml:

name: "Get Modified Files"
description: "Fetches modified files based on the current event's commits and filters them."

inputs:
  commits-json:
    description: "The current event's commits in JSON format."
    required: true
  git-branch:
    description: "The branch to fetch for modified files."
    required: true
  file-type:
    description: "The file extension to look for."
    required: false
  file-ignore:
    description: "The file name that, if found, will skip a file in the output."
    required: false
    
outputs:
  modified-files:
    description: "A comma-separated list of modified files."

runs:
  using: "composite"
  steps:
    - name: Get modified files
      id: get-commit-files
      shell: bash
      run: |
        # Gets modified files from the event's commits
        echo -e "\033[36mFetching modified ${{ inputs.file-type }} files in all commits from the event...\033[0m"

        # Output the event's commits.
        COMMITS_COUNT=$(jq '. | length' <<EOF
        ${{ inputs.commits-json }}
        EOF
        )
        if [ -z "$COMMITS_COUNT" ] || [ "$COMMITS_COUNT" -eq 0 ]; then
          echo "::error::Unable to determine the amount of commits in the event."
          exit 1
        fi
        echo -e "\033[36mCommits in the event ($COMMITS_COUNT):\033[0m"
        jq -r '.[].id' <<EOF
        ${{ inputs.commits-json }}
        EOF

        # Ensure the repository is fully fetched to the correct depth
        git fetch --prune --unshallow || true
        git fetch origin "${{ inputs.git-branch }}" --depth="$((COMMITS_COUNT+1))" || true

        # Get all modified files
        PREVIOUS_SHA=$(git rev-parse HEAD~$COMMITS_COUNT)
        CURRENT_SHA=$(jq -r '.[-1].id' <<EOF
        ${{ inputs.commits-json }}
        EOF
        )
        MODIFIED_PROJECTS=$(git diff --name-only $PREVIOUS_SHA..$CURRENT_SHA | grep "${{ inputs.file-type }}$" | sort | uniq || true)
        FILTERED_PROJECTS=""

        # Output modified files and filter by the file-ignore
        echo -e "\033[36mModified files:\033[0m"
        for project in $MODIFIED_PROJECTS; do
          PROJECT_DIR=$(dirname "$project")
          if [ -f "$PROJECT_DIR/${{ inputs.file-ignore }}" ]; then
            echo -e "\033[31m$project (skipped)\033[0m"
          else
            FILTERED_PROJECTS="$FILTERED_PROJECTS$project,"
            echo "$project"
          fi
        done
        FILTERED_PROJECTS=${FILTERED_PROJECTS%,}

        # Handle case where no files are modified
        if [ -z "$FILTERED_PROJECTS" ]; then
          echo -e "\033[33mNo modified ${{ inputs.file-type }} files found in the current event.\033[0m"
        fi
        
        echo "DEBUG TEST: $FILTERED_PROJECTS"
        echo "modified-files=$FILTERED_PROJECTS" >> $env.GITHUB_OUTPUT
bash github github-actions git-bash
1个回答
0
投票

经过更彻底的研究和实验,我发现复合动作必须显式声明其输出值,并引用输出它的内部步骤。

在这种情况下,您只需在输出声明的action.yml中添加

value: ${{ steps.get-commit-files.outputs.modified-files }}
即可:

(...)
outputs:
  modified-files:
    description: "A comma-separated list of modified files."
    value: ${{ steps.get-commit-files.outputs.modified-files }}

runs:
  using: "composite"
  steps:
    - name: Get modified files
      id: get-commit-files
      shell: bash
      run: |
(...)
        echo "modified-files=$FILTERED_PROJECTS" >> $GITHUB_OUTPUT

这样,您就可以通过action-test.yml文件中的

${{ steps.action-test.outputs.modified-files }}"
正确检索其值。

© www.soinside.com 2019 - 2024. All rights reserved.