[Linux] Write a deployment tool using rsync with a shell script

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 ...

overall structure

Screen-Shot-2019-03-08-at-23.41.46.png

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.

Learn how to use rsync

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!

Whole code


$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

[Linux] Write a deployment tool using rsync with a shell script
[Linux] Copy data from Linux to Windows with a shell script
Write a batch script with Python3.5 ~
Manage logrotate generations with a shell script.
Creating a shell script to write a diary
View today's weather forecast with a shell script
A shell script that puts Webmin into Alpine Linux
Try programming with a shell!
Let's try a shell script
Process the files in the folder in order with a shell script
Using a printer with Debian 10
I'll never forget how to write a shell script, don't forget! !!
Write processing time measurement a little easier using the with clause
Process the contents of the file in order with a shell script
Write a script to calculate the distance with Elasticsearch 5 system painless
[Infrastructure] Linux command, shell script collection
Write a Residual Network with TFLearn
Write a stacked histogram with matplotlib
Using a webcam with Raspberry Pi
Using Kali Linux Gui with WSL2
Create a tool to automatically furigana with html using Mecab from Python3
Create a permanent write area on Kali Linux with USB memory boot
Create a star system with Blender 2.80 script
Idempotent guarantee with deployment tool fabric + cuisine
Write a TCP client with Python Twisted
[Linux] Build a jenkins environment with Docker
Equipped with a card function using payjp
Write a nice pie chart with matplotlib
[Linux] Build a Docker environment with Amazon Linux 2
[AWS Lambda] Create a deployment package using the Docker image of Amazon Linux