Git is easy to use. For my convenience and curiosity, I have learned how to set up a Git server and realize automatic deployment. Here I will share with you the pits I have encountered.
System specifications
· Server: Aliyun ECS server
· Operating system :CentOS 7.2 64-bit
· Operation instructions: Familiar with Linux basic operations — yum installation, Vim editing, chown permission setting, etc.
Set up steps
1. Install Git on the server
yum install git
Note: If the permission is not enough, you can add sudo.
2. Add a Git user
Git users here are git applications on your system, not members of your project. Executable commands:
sudo adduser git
You can also add a user password
sudo passwd git
3. Disable shell login
Use vim to open the /etc/passwd directory
git:x:1000:1000::/home/git:/usr/bin/bash
to
git:x:1000:1000::/home/git:/usr/bin/git-shell
Note: For security reasons, we need to forbid others to log in shell, but our team members can still use Git normally through SSH.
4. Initialize the Git remote repository
Create a Git remote repository on your server and initialize it:
git init –bare test.git
For example, select the /usr/src/app folder and initialize the repository test.git. The result is as follows:
5. Change the ownership of the remote warehouse user
Since the file ownership belongs to the current user at the time of initial creation (as shown in the picture below, I am currently using root), in order to ensure that the git program has operation permission, we changed the ownership to Git own.
sudo chown -R git:git test.git
The results are as follows:
6. Add the SSH login permission to the user
The project team members of SSH public key stored in turn to/home/git /. SSH/authorized_keys file, a line of one. If there are many people, gitosis can be used for management, configuration can refer to the official document, here is not introduced.
7. Local Clone remote warehouse
The first 6 steps are performed on the server, this step can be performed locally, execute command pull code:
git clone git@server:/usr/src/app/test.git
Git: /usr/ SRC /app/test.git: /usr/ SRC /app/test.git
Automated deployment
Git hooks fire specific events upon receiving a push. We can take advantage of this feature to automate deployment using bash scripts.
1. Create a script
Now go to the hooks folder in /usr/src/app/test.git and edit the script:
vim post-receive
The details are as follows:
#! /bin/sh
Delete the GIT_DIR environment variable to avoid interference
unset GIT_DIR
Add the production environment project path environment variable
WORKPATH=/usr/share/nginx/test
Enter the production directory
cd $WORKPATH
git add . -A git stash
git pull origin master
echo ‘done!!! ‘
The /usr/share/nginx/test directory is full of clone code.
2. Modify the permission
3. chown -R git:git post-receive
chmod +x post-receive
Chmod +x is used to increase execution permissions
Instructions on pit
Most of the pits encountered during the period are caused by permission problems, chown and chmod to modify the ownership and operation permissions.
Git installations on different systems are searchable, and each system has its own package management tool.