Composite actions substitute ${{ inputs.* }} directly into the run:
script source before execution, and the runner echoes that resolved
script at the top of the step log. Interpolating private_key and
transfers straight into the heredocs meant the raw SSH private key
was printed in plaintext on every run. Move both through env: and
reference them as shell variables instead, matching ssh-checkout and
ssh-command.
75 lines
2.4 KiB
YAML
75 lines
2.4 KiB
YAML
name: 'SSH Upload'
|
|
description: 'Upload files via SCP, automatically creating destination directories.'
|
|
author: 'MegaMiley Studio'
|
|
|
|
inputs:
|
|
host:
|
|
description: 'SSH Host IP or Domain'
|
|
required: true
|
|
port:
|
|
description: 'SSH Port (default: 22)'
|
|
required: false
|
|
default: '22'
|
|
username:
|
|
description: 'SSH Username'
|
|
required: true
|
|
private_key:
|
|
description: 'SSH Private Key'
|
|
required: true
|
|
transfers:
|
|
description: 'List of transfers formatted as "source_file(s) | remote_destination". One per line.'
|
|
required: true
|
|
|
|
runs:
|
|
using: 'composite'
|
|
steps:
|
|
- name: Execute SSH/SCP Transfers
|
|
shell: bash
|
|
env:
|
|
PRIVATE_KEY: ${{ inputs.private_key }}
|
|
TRANSFERS: ${{ inputs.transfers }}
|
|
HOST: ${{ inputs.host }}
|
|
PORT: ${{ inputs.port }}
|
|
USERNAME: ${{ inputs.username }}
|
|
run: |
|
|
# 1. Create a secure temporary file for the SSH key
|
|
SSH_KEY_PATH=$(mktemp)
|
|
printf '%s\n' "$PRIVATE_KEY" > "$SSH_KEY_PATH"
|
|
chmod 600 "$SSH_KEY_PATH"
|
|
|
|
# 2. Add Host to known_hosts to prevent verification prompts
|
|
mkdir -p ~/.ssh
|
|
ssh-keyscan -p "$PORT" -H "$HOST" >> ~/.ssh/known_hosts 2>/dev/null
|
|
|
|
# 3. Securely write transfers input to a file for parsing
|
|
printf '%s\n' "$TRANSFERS" > transfers.txt
|
|
|
|
# 4. Loop through each line and execute commands
|
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
# Skip empty lines
|
|
[[ -z "$(echo "$line" | tr -d '[:space:]')" ]] && continue
|
|
|
|
# Parse using the pipe | delimiter
|
|
IFS='|' read -r src dest <<< "$line"
|
|
|
|
# Trim leading and trailing whitespace
|
|
src=$(echo "$src" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
|
dest=$(echo "$dest" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
|
|
|
if [ -n "$src" ] && [ -n "$dest" ]; then
|
|
echo "::group::Transfer to $dest"
|
|
|
|
echo "Creating remote directory: $dest"
|
|
ssh -i "$SSH_KEY_PATH" -p "$PORT" "$USERNAME@$HOST" "mkdir -p \"$dest\"" < /dev/null
|
|
|
|
echo "Copying $src to $dest..."
|
|
# Note: eval is used so wildcards or multiple space-separated files expand properly
|
|
eval "scp -r -i \"$SSH_KEY_PATH\" -P $PORT $src \"$USERNAME@$HOST:$dest/\"" < /dev/null
|
|
|
|
echo "::endgroup::"
|
|
fi
|
|
done < transfers.txt
|
|
|
|
# 5. Cleanup
|
|
rm -f "$SSH_KEY_PATH" transfers.txt
|