17 Commits

Author SHA1 Message Date
smf
e8b7bda08f Merge pull request 'base_image' (#4) from base_image into local
Reviewed-on: #4
2024-03-08 21:47:22 +08:00
smf
5e5c5eeda2 info 2024-03-08 21:46:42 +08:00
smf
45da665ce0 change base image 2024-03-08 21:26:54 +08:00
smf
1ec01bdf99 Merge pull request 'update readme' (#3) from readme into local
Reviewed-on: #3
2024-03-08 18:46:40 +08:00
smf
83c611bb20 update readme 2024-03-08 18:46:09 +08:00
smf
3ec54a128a m5x2 (#2)
Reviewed-on: #2
2024-03-08 18:15:54 +08:00
smf
e39a4b8cea Merge pull request 'pack' (#1) from pack into local
Reviewed-on: #1
2024-03-06 11:40:05 +08:00
smf
30e8216aac no pack 2024-03-06 11:39:21 +08:00
smf
aac8c97a53 add pack action 2024-03-05 21:35:49 +08:00
smf
5c6b04eda8 change base image 2024-03-05 17:12:08 +08:00
smf
71bbecf3a2 更新基础镜像 2024-03-05 17:01:55 +08:00
smf
e60d189fd7 check version 2024-03-05 16:27:34 +08:00
smf
ec857c7af8 restore 2024-03-05 16:08:40 +08:00
smf
824e8e340d plugin cmd 2024-03-05 15:32:52 +08:00
smf
110724cd60 add . 2024-03-05 12:33:55 +08:00
smf
101b7e5e22 ssh test 2024-03-04 22:19:33 +08:00
smf
f0f9c0800d fix echo info 2024-03-04 21:11:23 +08:00
6 changed files with 76 additions and 170 deletions

View File

@@ -1,40 +0,0 @@
name: Release
on:
release:
types:
- created
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: ./
file: ./Dockerfile
push: true
tags: |
sulthonzh/docker-remote-deployment-action:latest
sulthonzh/docker-remote-deployment-action:${{ github.event.release.tag_name }}

View File

@@ -1,12 +1,16 @@
FROM docker/compose:1.29.2 FROM docker
COPY --from=docker/compose-bin:edge /docker-compose /usr/libexec/docker/cli-plugins/docker-compose
RUN docker compose version
LABEL 'name'='Docker Deployment Action' LABEL 'name'='Docker Compose Remote Deployment Action'
LABEL 'com.github.actions.name'='Docker Deployment' LABEL 'com.github.actions.name'='Docker Compose Remote Deployment'
LABEL 'com.github.actions.description'='supports docker-compose and Docker Swarm deployments' LABEL 'com.github.actions.description'='supports docker-compose remotely through ssh.'
LABEL 'com.github.actions.icon'='send' LABEL 'com.github.actions.icon'='send'
LABEL 'com.github.actions.color'='green' LABEL 'com.github.actions.color'='green'
RUN apk --no-cache add openssh-client RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk --no-cache add openssh-client socat
COPY docker-entrypoint.sh /docker-entrypoint.sh COPY docker-entrypoint.sh /docker-entrypoint.sh

View File

@@ -25,25 +25,23 @@ Below is a brief example on how the action can be used:
## Input Configurations ## Input Configurations
### `remote_docker_host` ### `remote_docker_host`
Remote Docker host ie (user@host). Remote Docker host.
### `remote_docker_user`
Remote Docker user.
### `remote_docker_port` ### `remote_docker_port`
Remote Docker ssh port ie (22). Remote Docker ssh port ie (22).
### `ssh_public_key`
Remote Docker SSH public key eg (~/.ssh/rsa_id.pub).
### `ssh_private_key` ### `ssh_private_key`
SSH private key used to connect to the docker host eg (~/.ssh/rsa_id). SSH private key used to connect to the docker host eg (~/.ssh/rsa_id).
### `ssh_proxy_cmd`
SSH ProxyCommand used to connect to the docker host.
### `args` ### `args`
Deployment command args. Deployment command args.
### `deployment_mode`
Deployment mode either docker-swarm or docker-compose. Default is docker-compose.
### `copy_stack_file`
Copy stack file to remote server and deploy from the server. Default is false.
### `deploy_path` ### `deploy_path`
The path where the stack files will be copied to. Default ~/docker-deployment. The absolute path on remote docker host to deploy, default rely on repo working directory.
### `stack_file_name` ### `stack_file_name`
Docker stack file used. Default is docker-compose.yml. Compose configuration files.
### `keep_files` ### `env_file_name`
Number of the files to be kept on the server. Default is 3. Specify an alternate environment file.
### `docker_prune` ### `docker_prune`
A boolean input to trigger docker prune command. Default is false. A boolean input to trigger docker prune command. Default is false.
### `pre_deployment_command_args` ### `pre_deployment_command_args`

View File

@@ -2,35 +2,32 @@ name: Docker Remote (SSH) Deployment
description: A GitHub Action that supports docker-compose and Docker Swarm deployments description: A GitHub Action that supports docker-compose and Docker Swarm deployments
inputs: inputs:
remote_docker_host: remote_docker_host:
description: Remote Docker host ie (user@host). description: Remote Docker host.
required: true
remote_docker_user:
description: Remote Docker user.
required: true required: true
remote_docker_port: remote_docker_port:
description: Remote Docker ssh port ie (22). description: Remote Docker ssh port ie (22).
required: false required: false
default: '22' default: '22'
ssh_public_key:
description: Remote Docker SSH public key eg (~/.ssh/rsa_id.pub).
required: true
ssh_private_key: ssh_private_key:
description: SSH private key used to connect to the docker host eg (~/.ssh/rsa_id). description: SSH private key used to connect to the docker host eg (~/.ssh/rsa_id).
required: true required: true
ssh_proxy_cmd:
description: SSH ProxyCommand used to connect to the docker host.
required: false
args: args:
description: Deployment command args. description: Deployment command args.
required: true required: true
deployment_mode:
description: Deployment mode either docker-swarm or docker-compose. Default is docker-compose.
required: false
copy_stack_file:
description: Copy stack file to remote server and deploy from the server. Default is false.
required: false
deploy_path: deploy_path:
description: The path where the stack files will be copied to. Default ~/docker-deployment. description: The absolute path on remote docker host to deploy, default rely on repo working directory.
required: false required: false
stack_file_name: stack_file_name:
description: Docker stack file used. Default is docker-compose.yml. description: Compose configuration files.
required: false required: false
keep_files: env_file_name:
description: Number of the files to be kept on the server. Default is 3. description: Specify an alternate environment file.
required: false required: false
docker_prune: docker_prune:
description: A boolean input to trigger docker prune command. Default is false. description: A boolean input to trigger docker prune command. Default is false.
@@ -38,9 +35,6 @@ inputs:
pre_deployment_command_args: pre_deployment_command_args:
description: The args for the pre deploument command. description: The args for the pre deploument command.
required: false required: false
pull_images_first:
description: Pull docker images before deploying. Default is false.
required: false
docker_registry_username: docker_registry_username:
description: The docker registry username. description: The docker registry username.
required: false required: false

View File

@@ -1,85 +1,43 @@
#!/bin/sh #!/bin/sh
set -eu set -eu
execute_ssh(){ if [ -z "${INPUT_REMOTE_DOCKER_PORT+x}" ] || [ -z "$INPUT_REMOTE_DOCKER_PORT" ]; then
echo "Execute Over SSH: $@"
ssh -q -t -i "$HOME/.ssh/id_rsa" \
-o UserKnownHostsFile=/dev/null \
-p $INPUT_REMOTE_DOCKER_PORT \
-o StrictHostKeyChecking=no "$INPUT_REMOTE_DOCKER_HOST" "$@"
}
if [ -z "${INPUT_REMOTE_DOCKER_PORT+x}" ]; then
INPUT_REMOTE_DOCKER_PORT=22 INPUT_REMOTE_DOCKER_PORT=22
fi fi
if [ -z "${INPUT_REMOTE_DOCKER_HOST+x}" ]; then if [ -z "${INPUT_REMOTE_DOCKER_HOST+x}" ] || [ -z "$INPUT_REMOTE_DOCKER_HOST" ]; then
echo "Input remote_docker_host is required!" echo "Input remote_docker_host is required!"
exit 1 exit 1
fi fi
if [ -z "${INPUT_SSH_PUBLIC_KEY+x}" ]; then if [ -z "${INPUT_REMOTE_DOCKER_USER+x}" ] || [ -z "$INPUT_REMOTE_DOCKER_USER" ]; then
echo "Input ssh_public_key is required!" echo "Input remote_docker_user is required!"
exit 1 exit 1
fi fi
if [ -z "${INPUT_SSH_PRIVATE_KEY+x}" ]; then if [ -z "${INPUT_SSH_PRIVATE_KEY+x}" ] || [ -z "$INPUT_SSH_PRIVATE_KEY" ]; then
echo "Input ssh_private_key is required!" echo "Input ssh_private_key is required!"
exit 1 exit 1
fi fi
if [ -z "${INPUT_ARGS+x}" ]; then if [ -z "${INPUT_ARGS+x}" ] || [ -z "$INPUT_ARGS" ]; then
echo "Input input_args is required!" echo "Input input_args is required!"
exit 1 exit 1
fi fi
if [ -z "${INPUT_DEPLOY_PATH+x}" ]; then if ! [ -z "${INPUT_DEPLOY_PATH+x}" ] && ! [ -z "$INPUT_DEPLOY_PATH" ]; then
INPUT_DEPLOY_PATH=~/docker-deployment echo "Change working directory"
echo "Current: `pwd`"
echo "Target: $INPUT_DEPLOY_PATH"
mkdir -p $INPUT_DEPLOY_PATH
cp -rfp ./. $INPUT_DEPLOY_PATH
cd $INPUT_DEPLOY_PATH
fi fi
if [ -z "${INPUT_STACK_FILE_NAME+x}" ]; then if [ -z "${INPUT_DOCKER_REGISTRY_URI+x}" ] || [ -z "$INPUT_DOCKER_REGISTRY_URI" ]; then
INPUT_STACK_FILE_NAME=docker-compose.yaml
fi
if [ -z "${INPUT_KEEP_FILES+x}" ]; then
INPUT_KEEP_FILES=4
else
INPUT_KEEP_FILES=$((INPUT_KEEP_FILES+1))
fi
if [ -z "${INPUT_DOCKER_REGISTRY_URI+x}" ]; then
INPUT_DOCKER_REGISTRY_URI=https://registry.hub.docker.com INPUT_DOCKER_REGISTRY_URI=https://registry.hub.docker.com
fi fi
if [ -z "${INPUT_COPY_STACK_FILE+x}" ]; then
INPUT_COPY_STACK_FILE=false
fi
STACK_FILE=${INPUT_STACK_FILE_NAME}
DEPLOYMENT_COMMAND_OPTIONS=""
if [ "$INPUT_COPY_STACK_FILE" == "true" ]; then
STACK_FILE="$INPUT_DEPLOY_PATH/$STACK_FILE"
else
DEPLOYMENT_COMMAND_OPTIONS=" --log-level debug --host ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_REMOTE_DOCKER_PORT"
fi
case $INPUT_DEPLOYMENT_MODE in
docker-swarm)
DEPLOYMENT_COMMAND="docker $DEPLOYMENT_COMMAND_OPTIONS stack deploy --compose-file $STACK_FILE"
;;
*)
INPUT_DEPLOYMENT_MODE="docker-compose"
DEPLOYMENT_COMMAND="docker-compose -f $STACK_FILE $DEPLOYMENT_COMMAND_OPTIONS"
;;
esac
SSH_HOST=${INPUT_REMOTE_DOCKER_HOST#*@}
echo "Registering SSH keys..." echo "Registering SSH keys..."
# register the private key with the agent. # register the private key with the agent.
@@ -87,23 +45,37 @@ mkdir -p ~/.ssh
ls ~/.ssh ls ~/.ssh
printf '%s\n' "$INPUT_SSH_PRIVATE_KEY" > ~/.ssh/id_rsa printf '%s\n' "$INPUT_SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa
printf '%s\n' "$INPUT_SSH_PUBLIC_KEY" > ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/id_rsa.pub
#chmod 600 "~/.ssh"
eval $(ssh-agent) eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa ssh-add ~/.ssh/id_rsa
echo "Add known hosts" echo "Add REMOTE_DOCKER_HOST alias to ~/.ssh/config"
ssh-keyscan -p $INPUT_REMOTE_DOCKER_PORT "$SSH_HOST" >> ~/.ssh/known_hosts touch ~/.ssh/config
ssh-keyscan -p $INPUT_REMOTE_DOCKER_PORT "$SSH_HOST" >> /etc/ssh/ssh_known_hosts echo >> ~/.ssh/config
echo "Host REMOTE_DOCKER_HOST" >> ~/.ssh/config
echo " HostName $INPUT_REMOTE_DOCKER_HOST" >> ~/.ssh/config
echo " User $INPUT_REMOTE_DOCKER_USER" >> ~/.ssh/config
echo " Port $INPUT_REMOTE_DOCKER_PORT" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config
echo " StrictHostKeyChecking no" >> ~/.ssh/config
echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config
if ! [ -z "${INPUT_SSH_PROXY_CMD+x}" ] && ! [ -z "$INPUT_SSH_PROXY_CMD" ]; then
echo "Add ProxyCommand: $INPUT_SSH_PROXY_CMD"
echo " ProxyCommand $INPUT_SSH_PROXY_CMD" >> ~/.ssh/config
fi
echo >> ~/.ssh/config
chmod 600 ~/.ssh/config
docker -v
docker compose version
set context set context
echo "Create docker context" echo "Create docker context"
docker context create remote --docker "host=ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_REMOTE_DOCKER_PORT" docker context create remote --docker "host=ssh://REMOTE_DOCKER_HOST"
docker context use remote docker context use remote
if ! [ -z "${INPUT_DOCKER_REGISTRY_USERNAME+x}" ] && ! [ -z "${INPUT_DOCKER_REGISTRY_PASSWORD+x}" ]; then if ! [ -z "${INPUT_DOCKER_REGISTRY_USERNAME+x}" ] && ! [ -z "$INPUT_DOCKER_REGISTRY_USERNAME" ] &&
echo "Connecting to $INPUT_REMOTE_DOCKER_HOST... Command: docker login" ! [ -z "${INPUT_DOCKER_REGISTRY_PASSWORD+x}" ] && ! [ -z "$INPUT_DOCKER_REGISTRY_PASSWORD" ]; then
echo "Connecting to $INPUT_DOCKER_REGISTRY_URI... Command: docker login"
echo "$INPUT_DOCKER_REGISTRY_PASSWORD" | docker login -u "$INPUT_DOCKER_REGISTRY_USERNAME" --password-stdin "$INPUT_DOCKER_REGISTRY_URI" echo "$INPUT_DOCKER_REGISTRY_PASSWORD" | docker login -u "$INPUT_DOCKER_REGISTRY_USERNAME" --password-stdin "$INPUT_DOCKER_REGISTRY_URI"
fi fi
@@ -111,34 +83,17 @@ if ! [ -z "${INPUT_DOCKER_PRUNE+x}" ] && [ $INPUT_DOCKER_PRUNE = 'true' ] ; then
yes | docker --log-level debug --host "ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_REMOTE_DOCKER_PORT" system prune -a 2>&1 yes | docker --log-level debug --host "ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_REMOTE_DOCKER_PORT" system prune -a 2>&1
fi fi
if ! [ -z "${INPUT_COPY_STACK_FILE+x}" ] && [ $INPUT_COPY_STACK_FILE = 'true' ] ; then DEPLOYMENT_COMMAND="docker --log-level=debug compose"
execute_ssh "mkdir -p $INPUT_DEPLOY_PATH/stacks || true"
FILE_NAME="docker-stack-$(date +%Y%m%d%s).yaml"
scp -i "$HOME/.ssh/id_rsa" \ if ! [ -z "${INPUT_STACK_FILE_NAME+x}" ] && ! [ -z "$INPUT_STACK_FILE_NAME" ]; then
-o UserKnownHostsFile=/dev/null \ DEPLOYMENT_COMMAND="$DEPLOYMENT_COMMAND -f ${INPUT_STACK_FILE_NAME//,/ -f }"
-o StrictHostKeyChecking=no \
-P $INPUT_REMOTE_DOCKER_PORT \
$INPUT_STACK_FILE_NAME "$INPUT_REMOTE_DOCKER_HOST:$INPUT_DEPLOY_PATH/stacks/$FILE_NAME"
DIRS=$(dirname "$INPUT_DEPLOY_PATH/$INPUT_STACK_FILE_NAME")
mkdir -p "$DIRS" || exit 0
execute_ssh "ln -nfs $INPUT_DEPLOY_PATH/stacks/$FILE_NAME $INPUT_DEPLOY_PATH/$INPUT_STACK_FILE_NAME"
execute_ssh "ls -t $INPUT_DEPLOY_PATH/stacks/docker-stack-* 2>/dev/null | tail -n +$INPUT_KEEP_FILES | xargs rm -- 2>/dev/null || true"
if ! [ -z "${INPUT_PULL_IMAGES_FIRST+x}" ] && [ $INPUT_PULL_IMAGES_FIRST = 'true' ] && [ $INPUT_DEPLOYMENT_MODE = 'docker-compose' ] ; then
execute_ssh ${DEPLOYMENT_COMMAND} "pull"
fi
if ! [ -z "${INPUT_PRE_DEPLOYMENT_COMMAND_ARGS+x}" ] && [ $INPUT_DEPLOYMENT_MODE = 'docker-compose' ] ; then
execute_ssh "${DEPLOYMENT_COMMAND} $INPUT_PRE_DEPLOYMENT_COMMAND_ARGS" 2>&1
fi
execute_ssh ${DEPLOYMENT_COMMAND} "$INPUT_ARGS" 2>&1
else
echo "Connecting to $INPUT_REMOTE_DOCKER_HOST... Command: ${DEPLOYMENT_COMMAND} ${INPUT_ARGS}"
${DEPLOYMENT_COMMAND} ${INPUT_ARGS} 2>&1
fi fi
if ! [ -z "${INPUT_ENV_FILE_NAME+x}" ] && ! [ -z "$INPUT_ENV_FILE_NAME" ]; then
DEPLOYMENT_COMMAND="$DEPLOYMENT_COMMAND --env-file ${INPUT_ENV_FILE_NAME//,/ --env-file }"
fi
# DEPLOYMENT_COMMAND_OPTIONS=""
echo "Connecting to $INPUT_REMOTE_DOCKER_HOST... Command: ${DEPLOYMENT_COMMAND} ${INPUT_ARGS}"
${DEPLOYMENT_COMMAND} ${INPUT_ARGS} 2>&1

View File

@@ -1,5 +0,0 @@
{
"extends": [
"config:base"
]
}