I will show you how to easily create a tool for deploying the contents pushed to git on the production server. I'm assuming that I will deploy Laravel this time, but I think that there is no particular problem with other languages. I would like you to ignore the places that deal with composer. The site that uses this kind of deployment method these days may be legacy, but ...
After developing in your own local environment, git commit and push to go up remotely. The code that the project members pushed remotely will accumulate more and more. Let's give it to the production server at a good stage! So, at that time, use the deploy tool to reflect the latest git status in the production environment.
The point is that it is necessary to set the environment where the deployment tool is placed so that ssh connection can always be made in the production environment. Don't forget to prepare the public key and private key as a set in the deployment environment and keep the public key in the production environment. Think of this private key as a key for an environment locked by a public key created in a set.
Please refer to here
ssh public key authentication setting summary --Qiita
Should I git pull in a production environment? ?? You may think, but I wonder if it will be more secure because it can be prevented to some extent by limiting it on the deployment tool. Also, it is a good point that you can flexibly respond to deploying other than the contents of git.
rsync is a command you use when you want to copy files in your environment to another server. Basically
rsync [option] copy source copy destination
I will copy using this method. However, since it is time to deploy, I will add some options.
rsync \
--recursive \
--exclude='/storage/' \
--exclude='.git*' \
-e "ssh" ./ deploy@[Production IP address]:[Where to deploy the production environment (e.g /var/Under www)]
--- recuesive means "recursive". The meaning is that in the structure of the directory, the one at the bottom is also the target. This time all the elements under ./ are now subject to copying.
--exclude means "exclude". Therefore, storage will not be deployed in the production environment. (Recommended for those who are planning to deploy Laravel! You can prevent the log for the test environment from moving)
Last
-e" ssh "./ deploy @ [IP address of production environment]: [Destination location of production environment (under e.g / var / www)]
You will be able to make an SSH connection to the production environment with. Now you're ready to copy!
$BASE_DIR =Where to do git pull on the deploy server
$PROD_ADDRESS =Production IP address
$REMOTE_DIR =Where to deploy the production environment
function prepare_deploy(){
cd $BASE_DIR
echo -e "Pulling..."
git pull origin master
echo -e "Composer install..."
composer install
composer dump-autoload
echo -e "Composer install finished"
}
function deploy(){
rsync \
--recursive \
--exclude='/storage/' \
--exclude='.git*' \
-e "ssh" ./ deploy@$PROD_ADDRESS:$REMOTE_DIR
echo -e "Deploy finished!"
}
prepare_deploy
deploy
In preparation for deployment, we move to the place where we do git pull, then pull and composer install. Save it as deploy.sh and run it to deploy! In addition, if you create a validate function and validate it, you will be able to deploy more safely! For example, you can check if the DB schema status of the production environment and the local environment are the same, or use the mysqldump command. For the time being, the basic part is complete.
Recommended Posts