Symfony2 Nginx Docker Deploy Script

Published on Author craigzearfossLeave a comment
#!/bin/bash
COMPOSER_EXEC=/usr/local/bin/composer
PHP_EXEC=/usr/bin/php
WWW_DOC_ROOT=/var/www/production
STAGING_DOC_ROOT=/var/www/staging

die () {
    echo >&2 "$@"
    exit 1
}

[ "$#" -eq 1 ] || die "Please provide a website name (folder) to deploy."


if echo "$1" | grep 'staging'; then
    DOC_ROOT=$STAGING_DOC_ROOT/$1
else
    DOC_ROOT=$WWW_DOC_ROOT/$1
fi

if [ ! -d "$DOC_ROOT" ]; then
  die "The specified directory ($DOC_ROOT) does not exist."
fi


if echo "$1" | grep 'staging'; then

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; git pull"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT && SYMFONY__HOST__ENV=staging. $COMPOSER_EXEC --optimize-autoloader --no-interaction install"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT && setfacl -R -m u:`ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`:rwX -m u:$(id -u `whoami`):rwX app/cache app/logs"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT && setfacl -dR -m u:`ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`:rwX -m u:$(id -u `whoami`):rwX app/cache app/logs"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; SYMFONY__HOST__ENV=staging. $PHP_EXEC app/console cache:clear --env=prod"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; SYMFONY__HOST__ENV=staging. $PHP_EXEC app/console assets:install web --symlink --relative --env=prod"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; SYMFONY__HOST__ENV=staging. $PHP_EXEC app/console fos:js-routing:dump --env=prod"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; SYMFONY__HOST__ENV=staging. $PHP_EXEC app/console assetic:dump --env=prod"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; chown $(id -u `whoami`):$(id -u `whoami`) -R ./"

else

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; git pull"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT && $COMPOSER_EXEC --optimize-autoloader --no-interaction install"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT && setfacl -R -m u:`ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`:rwX -m u:$(id -u `whoami`):rwX app/cache app/logs"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT && setfacl -dR -m u:`ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`:rwX -m u:$(id -u `whoami`):rwX app/cache app/logs"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; $PHP_EXEC app/console cache:clear"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; $PHP_EXECp app/console assets:install web --symlink --relative --env=prod"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; $PHP_EXEC app/console fos:js-routing:dump --env=prod"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; $PHP_EXEC app/console assetic:dump --env=prod"

    docker exec -ti php /bin/bash -c "cd $DOC_ROOT; chown $(id -u `whoami`):$(id -u `whoami`) -R ./"

fi

Leave a Reply

Your email address will not be published. Required fields are marked *