Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
TapTap21 committed Nov 2, 2020
1 parent 8f45f43 commit 5649c26
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM docker:stable

RUN apk --no-cache add openssh-client docker-compose

COPY docker-entrypoint.sh /docker-entrypoint.sh

ENTRYPOINT ["/docker-entrypoint.sh"]
43 changes: 43 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Docker Deployment
author: Al-waleed Shihadeh <[email protected]>
description: A GitHub Action that supports docker-compose and Docker Swarm deployments
inputs:
remote_docker_host:
description: Remote Docker host ie (user@host)
required: true
ssh_public_key:
description: Remote Docker SSH public key
required: true
ssh_private_key:
description: SSH private key used to connect to the docker host
required: true
args:
description: Deployment command args.
required: true
stack_file_name:
description: Docker stack file used. Default is docker-compose.yaml
required: false
pre_deployment_command_args:
description: The args for the pre deploument command.
required: false
ssh_port:
description: The ssh port of the server. Default is 22
required: false
docker_login_password:
description: The docker login password
required: false
docker_login_user:
description: The docker login user
required: false
docker_login_registry:
description: The docker login registry
required: false

runs:
using: docker
image: Dockerfile

branding:
icon: send
color: green

8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3.3"

services:
webapp:
image: 832064218730.dkr.ecr.eu-west-1.amazonaws.com/api-test:1dd970e
ports:
- 8080:8080
network_mode: bridge
83 changes: 83 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/sh
set -eu

execute_ssh(){
echo "Execute Over SSH: $@"
ssh -q -t -i "$HOME/.ssh/id_rsa" \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no "$INPUT_REMOTE_DOCKER_HOST" -p "$INPUT_SSH_PORT" "$@"
}

if [ -z "$INPUT_REMOTE_DOCKER_HOST" ]; then
echo "Input remote_docker_host is required!"
exit 1
fi

if [ -z "$INPUT_SSH_PUBLIC_KEY" ]; then
echo "Input ssh_public_key is required!"
exit 1
fi

if [ -z "$INPUT_SSH_PRIVATE_KEY" ]; then
echo "Input ssh_private_key is required!"
exit 1
fi

if [ -z "$INPUT_ARGS" ]; then
echo "Input input_args is required!"
exit 1
fi

if [ -z "$INPUT_STACK_FILE_NAME" ]; then
INPUT_STACK_FILE_NAME=docker-compose.yml
fi

if [ -z "$INPUT_SSH_PORT" ]; then
INPUT_SSH_PORT=22
fi

STACK_FILE=${INPUT_STACK_FILE_NAME}
DEPLOYMENT_COMMAND_OPTIONS="--host ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_SSH_PORT"

DEPLOYMENT_COMMAND="docker-compose -f $STACK_FILE"


SSH_HOST=${INPUT_REMOTE_DOCKER_HOST#*@}

echo "Registering SSH keys..."

# register the private key with the agent.
mkdir -p ~/.ssh
ls ~/.ssh
printf '%s\n' "$INPUT_SSH_PRIVATE_KEY" > ~/.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)
ssh-add ~/.ssh/id_rsa


echo "Add known hosts"
#printf '%s %s\n' "$SSH_HOST" "$INPUT_SSH_PUBLIC_KEY" >> /etc/ssh/ssh_known_hosts
ssh-keyscan "$SSH_HOST" >> ~/.ssh/known_hosts
ssh-keyscan "$SSH_HOST" >> /etc/ssh/ssh_known_hosts
# set context
echo "Create docker context"
docker context create staging --docker "host=ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_SSH_PORT"
docker context use staging


docker ps
if [ -n "$INPUT_DOCKER_LOGIN_PASSWORD" ] || [ -n "$INPUT_DOCKER_LOGIN_USER" ] || [ -n "$INPUT_DOCKER_LOGIN_REGISTRY" ]; then
echo "Connecting to $INPUT_REMOTE_DOCKER_HOST... Command: docker login"
docker login -u "$INPUT_DOCKER_LOGIN_USER" -p "$INPUT_DOCKER_LOGIN_PASSWORD" "$INPUT_DOCKER_LOGIN_REGISTRY"
fi

echo "Command: ${DEPLOYMENT_COMMAND} pull"
${DEPLOYMENT_COMMAND} ${DEPLOYMENT_COMMAND_OPTIONS} pull

echo "Command: ${DEPLOYMENT_COMMAND} ${INPUT_ARGS}"
${DEPLOYMENT_COMMAND} ${DEPLOYMENT_COMMAND_OPTIONS} ${INPUT_ARGS}


0 comments on commit 5649c26

Please sign in to comment.