feat: new ssh-deploy job
This commit is contained in:
104
ssh-deploy/action.yaml
Normal file
104
ssh-deploy/action.yaml
Normal file
@@ -0,0 +1,104 @@
|
||||
name: SSH Deploy (docker compose)
|
||||
description: SSH into a remote host, pull the latest images, and recreate containers with docker compose.
|
||||
|
||||
inputs:
|
||||
ssh_host:
|
||||
description: Remote server hostname or IP address
|
||||
required: true
|
||||
ssh_port:
|
||||
description: SSH port
|
||||
required: false
|
||||
default: "22"
|
||||
ssh_username:
|
||||
description: SSH username for the remote server
|
||||
required: true
|
||||
ssh_key:
|
||||
description: SSH private key for the remote server (pass via secrets)
|
||||
required: true
|
||||
remote_dir:
|
||||
description: Directory on the remote server containing docker-compose.yml
|
||||
required: true
|
||||
compose_files:
|
||||
description: >
|
||||
Compose file(s) to use, space-separated (applied via -f flags).
|
||||
When empty, docker compose uses its default file detection.
|
||||
required: false
|
||||
default: ""
|
||||
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- name: Validate inputs
|
||||
shell: bash
|
||||
env:
|
||||
SSH_HOST: ${{ inputs.ssh_host }}
|
||||
SSH_USERNAME: ${{ inputs.ssh_username }}
|
||||
SSH_KEY: ${{ inputs.ssh_key }}
|
||||
REMOTE_DIR: ${{ inputs.remote_dir }}
|
||||
run: |
|
||||
if [ -z "${SSH_HOST}" ] || [ "${SSH_HOST// }" = "" ]; then
|
||||
echo "::error::ssh_host is not set or is empty."
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "${SSH_USERNAME}" ] || [ "${SSH_USERNAME// }" = "" ]; then
|
||||
echo "::error::ssh_username is not set or is empty."
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "${SSH_KEY}" ] || [ "${SSH_KEY// }" = "" ]; then
|
||||
echo "::error::ssh_key is not set or is empty."
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "${REMOTE_DIR}" ] || [ "${REMOTE_DIR// }" = "" ]; then
|
||||
echo "::error::remote_dir is not set or is empty."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Set up SSH key and known_hosts
|
||||
shell: bash
|
||||
env:
|
||||
SSH_KEY: ${{ inputs.ssh_key }}
|
||||
SSH_HOST: ${{ inputs.ssh_host }}
|
||||
SSH_PORT: ${{ inputs.ssh_port }}
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${SSH_KEY}" > ~/.ssh/id_deploy
|
||||
chmod 600 ~/.ssh/id_deploy
|
||||
|
||||
# Populate known_hosts so StrictHostKeyChecking works without prompts
|
||||
ssh-keyscan -p "${SSH_PORT}" "${SSH_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
|
||||
|
||||
if ! grep -q "${SSH_HOST}" ~/.ssh/known_hosts; then
|
||||
echo "::error::Failed to add ${SSH_HOST} to known_hosts (host may be unreachable)."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Pull and recreate containers
|
||||
shell: bash
|
||||
env:
|
||||
SSH_HOST: ${{ inputs.ssh_host }}
|
||||
SSH_PORT: ${{ inputs.ssh_port }}
|
||||
SSH_USERNAME: ${{ inputs.ssh_username }}
|
||||
REMOTE_DIR: ${{ inputs.remote_dir }}
|
||||
COMPOSE_FILES: ${{ inputs.compose_files }}
|
||||
run: |
|
||||
# Build -f flags if compose_files is provided
|
||||
COMPOSE_FLAGS=""
|
||||
if [ -n "${COMPOSE_FILES}" ] && [ "${COMPOSE_FILES// }" != "" ]; then
|
||||
for f in ${COMPOSE_FILES}; do
|
||||
COMPOSE_FLAGS="${COMPOSE_FLAGS} -f \"${f}\""
|
||||
done
|
||||
fi
|
||||
|
||||
echo "Connecting to ${SSH_USERNAME}@${SSH_HOST}:${SSH_PORT}"
|
||||
echo "Working directory: ${REMOTE_DIR}"
|
||||
if [ -n "${COMPOSE_FLAGS}" ]; then
|
||||
echo "Compose files: ${COMPOSE_FILES}"
|
||||
fi
|
||||
|
||||
ssh -i ~/.ssh/id_deploy \
|
||||
-p "${SSH_PORT}" \
|
||||
-o StrictHostKeyChecking=yes \
|
||||
"${SSH_USERNAME}@${SSH_HOST}" \
|
||||
"cd '${REMOTE_DIR}' \
|
||||
&& docker compose ${COMPOSE_FLAGS} pull \
|
||||
&& docker compose ${COMPOSE_FLAGS} up -d --force-recreate"
|
||||
Reference in New Issue
Block a user