summaryrefslogtreecommitdiff
path: root/deploy.sh
blob: 18808041f0a63c43527c4140289799adf58f19d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash

set -e

remote_host=
remote_path=
deploy_id=

while getopts ":h:p:i:" opt; do
    case ${opt} in
        h)
            remote_host="$OPTARG"
            ;;
        p)
            remote_path="$OPTARG"
            ;;
        i)
            deploy_id="$OPTARG"
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            exit 1
            ;;
        :)
            echo "The option -$OPTARG requires an argument" >&2
            exit 1
            ;;
    esac
done

if [ -z "${remote_host}" ] || [ -z "${remote_path}" ] || [ -z "${deploy_id}" ]; then
    echo "Please specify -h[remote host], -p[remote path] and -i[deploy id]" >&2
    exit 1
fi

echo "syncing ${PWD}/ to ${remote_host}:${remote_path}/${deploy_id}/"

rsync -vAax --exclude '.git*' --exclude .composer/ \
    -e "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" \
    ./ "${remote_host}:${remote_path}/${deploy_id}/"

ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${remote_host}" "
    set -e

    if [[ -f \"${remote_path}/current/config/config.php\" ]]; then
        echo \"Config backup\"
        cp \"${remote_path}/current/config/config.php\" \"${deploy_id}-config.php\"
    fi

    echo \"Changing symlink\"
    unlink \"${remote_path}/current\"
    ln -s \"${remote_path}/${deploy_id}\" \"${remote_path}/current\"

    if [[ -f \"${deploy_id}-config.php\" ]]; then
        echo \"Restoring config\"
        cp  \"${deploy_id}-config.php\" \"${remote_path}/current/config/config.php\"
    fi
"