Introduction to the

In our development process, we inevitably use version control. Of course, this also leads you to know about Git and SVN. Both are excellent version control tools, and I’m more comfortable with Git, though it may be a matter of personal habit. I don’t know how you get started with Git, but FOR me, I submit the code to the Github repository, log in to the server using SSH, and clone or update it. Sounds very troublesome, of course, in practice is also very troublesome, so what can “once and for all” way? Read on!

Simple to use

Git hooks

What are Git hooks? The official explanation is a bit long, but it is simply a script that is triggered under certain circumstances. This explanation may not be accurate, but I think it’s easier to understand. If you want to know more about Git, you can check out the Git website. Here we use hooks to automate the deployment.

Step 1: Create a Git user

Log in to our server, where you have Git installed by default. Create a Git user:

Create a user named Jouzeyu
adduser jouzeyu
Copy the code

Step 2: Add privileges to git users

Create a git folder under the home folder in the root directory
mkdir /home/git 
Switch to the created Git folder
cd /home/git
# create a.ssh folder, which is mainly used to hold public keys
mkdir .ssh
# Switch to the.ssh folder and create the authorized_keys file
cd .ssh
touch authorized_keys
Copy the code

Step 3: Configure Git and get the public key

Configure the user name and mailbox locally. My user name is jouzeyu by default
git config --global user.name "jouzeyu"
git config --global user.email "your email"
Copy the code

Note: If you use the –global option, all your future projects will use the user information configured here. If you want to use a different name or mailbox for a particular project, simply execute under the project:

git config user.name "xxx"
git config user.email "xxx"
Copy the code

We need to find a pair of files named id_dsa or ID_rsa, one of which has the.pub extension. The.pub file is your public key and the other is your private key. If not, run ssh-keyGen. Use cat ~/.ssh/id_rsa.pub to get the public key, copy it, use vi or vim to paste it into the authorized_keys file we created earlier, and save it with :wq.

Step 4: Initialize the repository

Create a folder for your Git repository:

mkdir /www/wwwroot/git
cd /www/wwwroot/git
Copy the code

Initialize the warehouse:

# Initialize a bare repository (strongly recommended)
git init --bare website.git
Configure the repository permissions so that jouzeyu can read and write the git user we created earlier
chown -R git:git website.git
Copy the code

It is important to note that if you do not give permission, the following Git pull will report an error because you do not have permission to write. The difference between a bare repository and a regular repository is simply that you can’t see your project files in a bare repository. A regular repository is just like your project directory, except that you have a.git folder.

Step 5: Build the project repository

This is also done on the server, and/WWW /wwwroot/ is the root directory of my environment.

Create project directory test on my server
mkdir /www/wwwroot/test
# Clone warehouse
git clone /www/wwwroot/git/website.git
# Set permissions
chown -R git website
Copy the code

Git repository is/WWW /wwwroot/git, project repository is/WWW /wwwroot/test.

Step 6: Clone the vm to the local PC

# Pull from the configured online repository by IP address
git clone[email protected]: / WWW/below/git/their gitIf the domain name is configured, you can also pull it by domain name
git clone [email protected]:/www/wwwroot/git/website.git
Copy the code

Because of the public key, there is no need for a password. If successful, a folder of “website” will appear on your computer. If there is an error, please check and then proceed with the following operations.

Step 7: Test the upload (Git pull)

Open the local repository that you cloned
cd website
# Create readme.md file
touch README.md
git add .
git commit -m"Create the readme.md file"
git push
Copy the code

No accident has been uploaded normally, if there is an error, please check the permission, mentioned above, if not, you can comment below.

Step 8: Add hooks

Finally to the main drama, write more detailed, so more trouble. Going back to our online server, the following is what we do online:

# Switch to this directory
cd /www/wwwroot/git/website.git/hooks
# Generate post-receive file
touch post-receive
# Edit using Vim
vim post-receive
Copy the code

Paste the following in the post-receive file:

#! /bin/sh
# Print out
echo '====== Upload code to server ======'
Open the online project folder
cd /www/wwwroot/test/website
This is very important, if you do not cancel, will not be able to perform git operation on the CD path
unset GIT_DIR
git pull origin master
Automatic compilation of vue projects, if necessary please remove the first # sign
# npm run build
# Automatically update composer (I haven't tried it yet)
# composer update
echo $(date) >> hook.log
echo '====== code update completed ======'
Copy the code

Add run permission to the post-receive file:

chmod +x post-receive
Copy the code

The last step

Modify some of the content locally, then commit the Git pull, and you can see that we have automated the deployment.