Skip to content

Commit

Permalink
Add deploy feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPattell committed Jan 23, 2019
1 parent 9639181 commit 6fe5a8a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ to the require section of your `composer.json` file.
- AWS create/update "Task Definitions"
- Auto update _/etc/hosts_ file on host machine
- Auto create nginx proxies on host machine
- Deploy
- And etc.

## LIFEHACKS
Expand Down
107 changes: 107 additions & 0 deletions bin/site-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env bash

# deploy code to server

function getVendorPath() {
(
package=$(readlink $1)
package=$(dirname $(dirname "${package//.}"))
cd $(dirname $1)
vendor=$(dirname $(dirname "$PWD/$(basename $1)"))
echo "$vendor$package"
)
}

# get package vendor dir
VENDOR_DIR=$(getVendorPath "${BASH_SOURCE[0]}")

# export environments
. "${VENDOR_DIR}/helpers/compile-env.sh"

COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_NONE='\033[0m'

for i in "$@"
do
case ${i} in
-s=*|--server-name=*)
DEPLOY_SERVER_NAME="${i#*=}"
shift
;;
-c=*|--container-name=*)
DEPLOY_CONTAINER_NAME="${i#*=}"
shift
;;
-p=*|--project-path=*)
PROJECT_PATH="${i#*=}"
shift
;;
-s=*|--strategy=*)
DEPLOY_STRATEGY="${i#*=}"
shift
;;
*)
# unknown option
;;
esac
done

DEPLOY_DEFAULT_SHELL=/bin/bash

if [ -z "$DEPLOY_SERVER_NAME" ]; then
echo -e "${COLOR_RED}Server name not found (--server-name).${COLOR_NONE}"
exit 1;
fi

if [ -z "$DEPLOY_CONTAINER_NAME" ]; then
echo -e "${COLOR_RED}Container name not found (--container-name).${COLOR_NONE}"
exit 1;
fi

if [ -z "$PROJECT_PATH" ]; then
echo -e "${COLOR_RED}Project path not found (--project-path).${COLOR_NONE}"
exit 1;
fi

if [ -z "$DEPLOY_STRATEGY" ]; then
echo -e "${COLOR_RED}Strategy not set (--strategy).${COLOR_NONE}"
exit 1;
fi

DEPLOY_CONTAINER_ID=$(ssh ${SERVER_NAME} "docker ps -a --format 'table {{.ID}}' -f name=$DEPLOY_CONTAINER_NAME | sed -n 2p")

if [ -z "$DEPLOY_CONTAINER_NAME" ]; then
echo -e "${COLOR_RED}Container id not found.${COLOR_NONE}"
exit 1;
fi

echo -e "${COLOR_GREEN}Container id found: $DEPLOY_CONTAINER_ID ${COLOR_NONE}"
echo -e "${COLOR_GREEN}Strategy: $DEPLOY_STRATEGY ${COLOR_NONE}"
echo -e "${COLOR_GREEN}Server: $DEPLOY_SERVER_NAME ${COLOR_NONE}"

# Strategy 1 git pull
if [ "$DEPLOY_STRATEGY" == 1 ]; then
ssh ${SERVER_NAME} "cd $PROJECT_PATH && git pull"
exit 0
fi

# Strategy 2 git pull + docker container composer install
if [ "$DEPLOY_STRATEGY" == 2 ]; then
ssh ${SERVER_NAME} "cd $PROJECT_PATH && git pull && docker exec ${DEPLOY_CONTAINER_ID} ${DEPLOY_DEFAULT_SHELL} -c 'composer install'"
exit 0
fi

# Strategy 3 in docker container git pull
if [ "$DEPLOY_STRATEGY" == 3 ]; then
ssh ${SERVER_NAME} "docker exec ${DEPLOY_CONTAINER_ID} ${DEPLOY_DEFAULT_SHELL} -c 'git pull'"
exit 0
fi

# Strategy 4 in docker container git pull + composer install
if [ "$DEPLOY_STRATEGY" == 4 ]; then
ssh ${SERVER_NAME} "docker exec ${DEPLOY_CONTAINER_ID} ${DEPLOY_DEFAULT_SHELL} -c 'git pull && composer install'"
exit 0
fi

echo -e "${COLOR_RED}Strategy case not foud. Use 1-4. ${COLOR_NONE}"
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "matthew-p/docker-server",
"description": "Universal docker server, Nginx, PHP-FPM, MySql, Redis",
"type": "package",
"keywords": ["docker","server","php","fpm","nginx","mysql","redis","docker-compose","universal","dynamic"],
"keywords": ["docker","server","php","fpm","nginx","mysql","redis","docker-compose","universal","dynamic","deploy","composer"],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Matthew Patell",
"email": "[email protected]"
}
],
"bin": ["bin/site-start.sh", "bin/site-exec.sh", "bin/site-run.sh", "bin/site-aws.sh"]
"bin": ["bin/site-start.sh", "bin/site-exec.sh", "bin/site-run.sh", "bin/site-aws.sh", "bin/site-deploy.sh"]
}
6 changes: 6 additions & 0 deletions docker/.env-default
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ HOST_ETC_KEEP_CONF=no
# Keep empty for get nginx container ip
HOST_ETC_HOST_IP=

# DEPLOY CONFIG
#DEPLOY_SERVER_NAME=
#DEPLOY_CONTAINER_NAME=
#PROJECT_PATH=
#DEPLOY_STRATEGY=

# AWS COMMANDS (-SERVICES- will be replaced)
# "-" will be replaced to "_" (in composer command)
# YOU NEED SET PROJECT_ENV_PATH_FORCE
Expand Down
9 changes: 9 additions & 0 deletions docs/ENVIRONMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Testing envs](#testing-envs)
- [Auto update hosts file (host machine)](#auto-update-hosts-envs)
- [Auto create nginx proxy (host machine)](#auto-create-proxy-envs)
- [Deploy](#deploy)

## <a id="domain-envs"></a>Domain envs (service: nginx, server)
Property | Values | Description
Expand Down Expand Up @@ -138,3 +139,11 @@ Property | Values | Description
---------|--------|------------
`AWS_UPDATE_TASK`| `(string)` | Create/update task definition on aws. Console command: `composer server-prod update-task`
`AWS_UPDATE_ENV`| `(string)` | Create/update `.env` file on remote host. Console command: `composer server-prod update-env`

## <a id="deploy"></a>Deploy
Property | Values | Description
---------|--------|------------
`DEPLOY_SERVER_NAME`| `string` | SSH server name.
`DEPLOY_CONTAINER_NAME`| `string` | Docker container name for deploy.
`PROJECT_PATH`| `(string)` | Path to project folder in remote server.
`DEPLOY_STRATEGY`| `(string)` `1-4` | See `site-deploy.sh` for more info

0 comments on commit 6fe5a8a

Please sign in to comment.