67 lines
1.8 KiB
YAML
67 lines
1.8 KiB
YAML
name: inject-content
|
|
description: Inject content into a file by appending or overwriting
|
|
|
|
inputs:
|
|
content:
|
|
description: Content to write into the target file
|
|
required: true
|
|
target-file:
|
|
description: Path to the target file
|
|
required: true
|
|
mode:
|
|
description: "Write mode: 'append' adds to the end of the file; 'overwrite' replaces its contents"
|
|
required: false
|
|
default: append
|
|
|
|
outputs:
|
|
target-file:
|
|
description: Path of the file that was written
|
|
value: ${{ steps.inject.outputs.target-file }}
|
|
|
|
runs:
|
|
using: composite
|
|
steps:
|
|
- name: Validate target-file input
|
|
shell: bash
|
|
env:
|
|
TARGET_FILE: ${{ inputs.target-file }}
|
|
run: |
|
|
if [[ -z "$TARGET_FILE" ]]; then
|
|
echo "::error::target-file must not be empty"
|
|
exit 1
|
|
fi
|
|
|
|
- name: Validate mode input
|
|
shell: bash
|
|
env:
|
|
MODE: ${{ inputs.mode }}
|
|
run: |
|
|
if [[ "$MODE" != "append" && "$MODE" != "overwrite" ]]; then
|
|
echo "::error::Invalid mode '$MODE'. Must be 'append' or 'overwrite'."
|
|
exit 1
|
|
fi
|
|
|
|
- name: Inject content into file
|
|
id: inject
|
|
shell: bash
|
|
env:
|
|
CONTENT: ${{ inputs.content }}
|
|
TARGET_FILE: ${{ inputs.target-file }}
|
|
MODE: ${{ inputs.mode }}
|
|
run: |
|
|
set -euo pipefail
|
|
|
|
mkdir -p "$(dirname "$TARGET_FILE")"
|
|
|
|
if [[ "$MODE" == "overwrite" ]]; then
|
|
printf '%s\n' "$CONTENT" > "$TARGET_FILE"
|
|
else
|
|
if [[ -s "$TARGET_FILE" ]]; then
|
|
printf '\n%s\n' "$CONTENT" >> "$TARGET_FILE"
|
|
else
|
|
printf '%s\n' "$CONTENT" >> "$TARGET_FILE"
|
|
fi
|
|
fi
|
|
|
|
echo "Content injected into $TARGET_FILE (mode: $MODE)"
|
|
echo "target-file=$TARGET_FILE" >> "$GITHUB_OUTPUT"
|