Skip to content

fix: prevent stdout duplication when capture_stdout is enabled#403

Closed
cyril23 wants to merge 1 commit intoappleboy:masterfrom
LogicsSoftwareGmbH:fix/capture-stdout-duplication
Closed

fix: prevent stdout duplication when capture_stdout is enabled#403
cyril23 wants to merge 1 commit intoappleboy:masterfrom
LogicsSoftwareGmbH:fix/capture-stdout-duplication

Conversation

@cyril23
Copy link

@cyril23 cyril23 commented Jan 24, 2026

Summary

Fix output duplication bug when capture_stdout: true is enabled.

Fixes #397

Problem

The bug was introduced in PR #374 (commit b6690ee) during a refactoring. The original implementation was correct:

# Original - correct
echo 'stdout<<EOF' >>$GITHUB_OUTPUT
sh -c "${TARGET} $*" | tee -a $GITHUB_OUTPUT
echo 'EOF' >>$GITHUB_OUTPUT

The refactoring wrapped everything in a subshell but kept tee -a:

# After refactoring - buggy
if [[ "${INPUT_CAPTURE_STDOUT}" == 'true' ]]; then
  {
    echo 'stdout<<EOF'
    "${TARGET}" "$@" | tee -a "${GITHUB_OUTPUT}"  # <-- 1st write
    echo 'EOF'
  } >>"${GITHUB_OUTPUT}"  # <-- 2nd write (subshell redirect)

This causes command output to be written twice to GITHUB_OUTPUT:

  1. Via tee -a "${GITHUB_OUTPUT}" directly
  2. Via the subshell { ... } >>"${GITHUB_OUTPUT}" redirect

Solution

Change tee -a "${GITHUB_OUTPUT}" to tee /dev/stderr:

  • Real-time output is still visible in the Actions log (via stderr)
  • Output is only written once to GITHUB_OUTPUT (via the subshell redirect)

Testing

Tested in production CI/CD pipelines deploying to multiple servers. Before the fix, all captured output lines appeared duplicated. After the fix, output appears correctly once.

The previous implementation wrote command output to GITHUB_OUTPUT twice:
1. Via `tee -a "${GITHUB_OUTPUT}"` directly
2. Via the subshell redirect `} >>"${GITHUB_OUTPUT}"`

This caused every line of captured output to appear twice.

Fix: Change `tee -a "${GITHUB_OUTPUT}"` to `tee /dev/stderr` so that:
- Real-time output is still visible in the Actions log (via stderr)
- Output is only written once to GITHUB_OUTPUT (via subshell redirect)
@appleboy
Copy link
Owner

@cyril23 I will take it.

appleboy added a commit that referenced this pull request Jan 28, 2026
- Write the stdout<<EOF and EOF markers directly to GITHUB_OUTPUT instead of using a group command
- Simplify the process for capturing and appending command output to GITHUB_OUTPUT

fix #403
fix #397

Signed-off-by: appleboy <appleboy.tw@gmail.com>
pull bot pushed a commit to Mu-L/ssh-action that referenced this pull request Jan 28, 2026
…ppleboy#404)

* refactor: streamline output handling for GITHUB_OUTPUT in workflows

- Write the stdout<<EOF and EOF markers directly to GITHUB_OUTPUT instead of using a group command
- Simplify the process for capturing and appending command output to GITHUB_OUTPUT

fix appleboy#403
fix appleboy#397

Signed-off-by: appleboy <appleboy.tw@gmail.com>

* test: enhance stdout capture and verification in tests

- Add a check to ensure captured stdout is not empty
- Add steps to capture and verify multiline stdout output
- Add verification that specific lines and the username are present in captured output
- Add steps to handle and verify stdout containing special characters and file paths

Signed-off-by: appleboy <appleboy.tw@gmail.com>

* ci: enforce unique occurrence of lines in multiline output validation

- Add a step to verify that lines "Line 1", "Line 2", and "Line 3" each appear exactly once in the multiline output
- Fail the workflow if any line is missing or duplicated
- Confirm successful validation with a message when no duplicates are found

Signed-off-by: appleboy <appleboy.tw@gmail.com>

---------

Signed-off-by: appleboy <appleboy.tw@gmail.com>
@cyril23 cyril23 deleted the fix/capture-stdout-duplication branch January 28, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicated output capture on the latest (1.2.3) version.

2 participants