Skip to content

Instantly share code, notes, and snippets.

@connorhu
Last active Mar 9, 2021
Embed
What would you like to do?
symfony build and deploy
#!/bin/bash
env=$1
if [ "$env" == "" ]
then
echo "env missing"
exit 1
fi
envs=("test" "prod")
if [[ ! " ${envs[@]} " =~ " ${env} " ]]; then
echo "invalid env: $env"
exit 1
fi
if [ ! -f './build/config' ]
then
echo 'Config file missing'
exit 1
fi
. ./build/config
branchname="branch_$env"
branch=${!branchname}
build_path=$build_root/$env
repo_url=$(git config --get remote.origin.url)
if [ ! -d "$build_path" ]
then
mkdir -p $build_path
fi
previous_cwd=$(pwd)
cd $build_path
if [ ! -d ".git" ]
then
git clone -b $branch --single-branch $repo_url .
else
git pull
fi
if [ -f "$previous_cwd/build/hooks/post-pull-hook" ]
then
echo $previous_cwd/build/hooks/post-pull-hook
. $previous_cwd/build/hooks/post-pull-hook
fi
if [ $env = "prod" ]
then
$php $composer install --no-dev --optimize-autoloader
else
$php $composer install
fi
$php $composer symfony:dump-env $env
$php bin/console cache:clear --no-warmup
$php bin/console cache:warmup
echo $previous_cwd/build/hooks/post-clear-hook
if [ -f "$previous_cwd/build/hooks/post-clear-hook" ]
then
echo $previous_cwd/build/hooks/post-clear-hook
. $previous_cwd/build/hooks/post-clear-hook
fi
yarn install
if [ $env = "prod" ]
then
yarn encore production
else
yarn encore dev
fi
chown www-data:www-data var -R
#!/bin/sh
build_root=/server/www/host.tld/.build
branch_test=master
branch_prod=master
php="php7.4"
composer="/usr/local/bin/composer"
remote_path=/server/www/host.tld/www
remote_ssh_port=22
remote_ssh_user=user
remote_ssh_host=host.tld
#!/bin/bash
env=$1
if [ "$env" == "" ]
then
echo "env missing"
exit 1
fi
envs=("test" "prod")
if [[ ! " ${envs[@]} " =~ " ${env} " ]]; then
echo "invalid env: $env"
exit 1
fi
. ./build/config
source_path=$build_root/$env
cd $source_path
commit_id_to_upload=$(git rev-parse HEAD | cut -c1-8)
target_path=$remote_path/${env}-$commit_id_to_upload
current_target=$(ssh -p $remote_ssh_port $remote_ssh_user@$remote_ssh_host "readlink $remote_path/$env")
current_commit_id=$(echo $current_target | sed s/^${env}-//)
if [ "$commit_id_to_upload" = "$current_commit_id" ]
then
echo "Current commit id on the server ($current_commit_id) is the same with you want to upload ($commit_id_to_upload)."
echo "Do you want to upload anyway? (y/N)"
read;
if [ "$REPLY" != "y" ]
then
exit
fi
fi
echo "deploy to $target_path"
rsync -a -e "ssh -p $remote_ssh_port" --info=progress2 --del \
--exclude 'node_modules' --exclude 'web' --exclude 'tests' --exclude 'development' --exclude 'app' \
$source_path/ $remote_ssh_user@$remote_ssh_host:$target_path
ssh -p $remote_ssh_port $remote_ssh_user@$remote_ssh_host "$php $target_path/bin/console cache:clear -e $env"
ssh -p $remote_ssh_port $remote_ssh_user@$remote_ssh_host "$php $target_path/bin/console cache:warmup -e $env"
ssh -p $remote_ssh_port $remote_ssh_user@$remote_ssh_host "chown www-data:www-data $target_path/var -R"
ssh -p $remote_ssh_port $remote_ssh_user@$remote_ssh_host "ln -sfn ${env}-$commit_id_to_upload $remote_path/$env"
ssh -p $remote_ssh_port $remote_ssh_user@$remote_ssh_host "$php $remote_path/$env/bin/console doctrine:migrations:migrate -n"
echo "Remove old? ($remote_path/$current_target) (y/N)"
read remove
if [ "$remove" = "y" ]
then
ssh -p $remote_ssh_port $remote_ssh_user@$remote_ssh_host "rm -rf $remote_path/$current_target"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment