From 3338ad6b8779073555240fbf7851e65ec0fe98e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kukuczka?= Date: Sun, 30 Mar 2025 12:25:27 +0200 Subject: [PATCH] first commit --- README.md | 0 action.yml | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 README.md create mode 100644 action.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..c1f5a4d --- /dev/null +++ b/action.yml @@ -0,0 +1,90 @@ +name: 'Prepare Remote NFS Directories' +description: | + Creates specified directories on a remote server (NFS mount) via SSH. Optionally prefixes directories + with a provided short SHA to isolate deployments. Skips execution if no directories are specified. + +inputs: + ssh_host: + description: 'SSH host of the remote server' + required: true + ssh_user: + description: 'SSH username for the remote server' + required: true + ssh_key: + description: 'SSH private key for authentication' + required: true + directories: + description: | + Newline-separated list of local directories to create remotely and copy contents from. + Example: | + config + public/images + public/icons + required: false + default: '' + remote_base_path: + description: 'Base path on remote server (e.g., /mnt/docker-mounts)' + required: false + default: '/mnt/docker-mounts' + dir_prefix: + description: 'Optional prefix for remote directories (e.g., git short SHA)' + required: false + default: '' + +outputs: + created_paths: + description: 'Newline-separated list of remote directories created' + value: ${{ steps.prepare.outputs.created_paths }} + +runs: + using: 'composite' + steps: + - id: prepare + shell: bash + run: | + set -euo pipefail + + if [ -z "${{ inputs.directories }}" ]; then + echo "DEBUG: No directories specified. Skipping directory creation." + echo "created_paths=" >> $GITHUB_OUTPUT + exit 0 + fi + + SSH_KEY_FILE=$(mktemp) + echo "${{ inputs.ssh_key }}" > "$SSH_KEY_FILE" + chmod 600 "$SSH_KEY_FILE" + echo "DEBUG: Temporary SSH key created at $SSH_KEY_FILE" + + CREATED_PATHS="" + PREFIX="${{ inputs.dir_prefix }}" + REMOTE_BASE="${{ inputs.remote_base_path }}" + + while IFS= read -r LOCAL_DIR || [ -n "$LOCAL_DIR" ]; do + if [ -d "$LOCAL_DIR" ]; then + REMOTE_DIR="$REMOTE_BASE" + if [ -n "$PREFIX" ]; then + REMOTE_DIR="$REMOTE_DIR/$PREFIX" + fi + REMOTE_DIR="$REMOTE_DIR/$LOCAL_DIR" + + echo "DEBUG: Creating remote directory $REMOTE_DIR" + ssh -o StrictHostKeyChecking=no -i "$SSH_KEY_FILE" \ + "${{ inputs.ssh_user }}@${{ inputs.ssh_host }}" \ + "mkdir -p \"$REMOTE_DIR\" && chmod -R 755 \"$REMOTE_DIR\"" + + echo "DEBUG: Copying contents from local '$LOCAL_DIR' to remote '$REMOTE_DIR'" + scp -o StrictHostKeyChecking=no -i "$SSH_KEY_FILE" -r "$LOCAL_DIR/"* \ + "${{ inputs.ssh_user }}@${{ inputs.ssh_host }}:\"$REMOTE_DIR/\"" || echo "DEBUG: No files to copy in '$LOCAL_DIR'" + + CREATED_PATHS+="$REMOTE_DIR"$'\n' + else + echo "DEBUG: Local directory '$LOCAL_DIR' does not exist. Skipping." + fi + done <<< "${{ inputs.directories }}" + + echo "DEBUG: Cleaning up temporary SSH key file" + rm -f "$SSH_KEY_FILE" + + echo "created_paths<> $GITHUB_OUTPUT + echo "$CREATED_PATHS" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT