Making the address

1. Build git server

I use Tencent cloud server of CenOS system


    1. Installing Git on the server
    yum install -y git
    Copy the code

    After the installation is complete, use the command to view the version and check whether the installation is successful.

    $ git --version
    Copy the code
    1. Create a system user to manage the Git repository

    I’m going to start htgit(check if the user exists)

    [root@VM_183_60_centos hongtao]# id htgit
    Copy the code

    Will appear if present:

    uid=1001(htgit) gid=1001(htgit) groups=1001(htgit)
    Copy the code

    Prompt if none exists

    id: htgit: no such user
    Copy the code

    To create a user, type the following command

    useradd htgit
    Copy the code

    After the creation, run the ID htgit command to check whether the creation is successful

    1. Create a project repository

    Create a project folder

    Mkdir -p < directory name ># For example, mine is
    mkdir -p mypro
    Copy the code

    Creating a Git repository

    mkdir -p myblog.git
    Copy the code

    Initialize the Git repository

    git init --bare myblog.git
    Copy the code

    Run ll to view directory information

    # ll
    drwxr-xr-x  2 root root 4096 Jan 29 16:19 myblog.git
    Copy the code

    Myblog.git belongs to the root user and is now changed to the htgit user we created earlier.

    sudo chown -R htgit:htgit myblog.git
    Copy the code

    The server pulls the code for the server git repository

    git clone ./myblog.git
    Copy the code

    Ll View the current folder

    drwxr-xr-x  3 root  root  4096 Jan 29 16:34 myblog
    drwxr-xr-x  7 htgit htgit 4096 Jan 29 16:26 myblog.git
    Copy the code

    Note: Change the myblog directory to htgit user permissions, otherwise git hooks will not have permissions to operate

2. Configure SSHD password-free connection


    1. SSH authentication is set up on the server

    Use CD ~htgit or CD /home/htgit to go to our htgit user directory and create an.ssh folder.

    mkdir .ssh
    Copy the code

    Change the permissions

    chown -R htgit:htgit .ssh
    chmod -R 700 .ssh
    Copy the code

    Enter. SSH /

    cd .ssh
    Copy the code

    Create authorized_keys

    vim authorized_keys
    Copy the code

    You will need to fill the public key of the client into this file later

    Change the permissions on this file

    chmod 600 authorized_keys
    Copy the code
    1. Configure SSH on the client
    ssh-keygen -t rsa -C 'Your email'
    Copy the code

    You can just use it if you’ve created it before

    The client is Windows

    Find the id_rsa.pub file on the system disk, usually C:\Users\ username \. SSH. Open it with an editor and copy the contents into the authorized_keys file we created on the server earlier.

Create a client local project


    1. Client pulls code

    For example, if I’m running a Windows system, you need to have Git installed, open Git bash, and type in the path to the Git repository we installed on the server

    git clonehtgit@[your server IP address]:[your git repository path]Copy the code

    Like mine:

    git clone[email protected]: / usr/hongtao/myblog. GitCopy the code
    1. Configuring local code

    I’ll use the iView-admin project as an example

    Iview-admin code address

    Git Clone > git clone > myblog

    I’m going to skip the demo here and just copy the code for iView-admin. And then submit it and upload it

    1. Server running code

    Git repository: git clone./myblog. Git folder: git clone./myblog. Go into this folder and type Git pull to get the latest code for the current server’s Git repository. Then perform

    npm i
    npm run dev
    Copy the code

    Then you can run on the server.

    At this point we know how to run the front-end code manually.

    There are many ways to run a front-end project on a server. Such as:

    • Nginx static resource server
    • NodeJs server
    • The PHP service
    • . , etc.

    This article uses a front-end project running nodeJS as an example. Run the project directly with webpack’s hot update.

4. Write git hooks


Go to our server git repository folder

cd /usr/hongtao/myblog.git
Copy the code

This directory contains an hooks directory CD hooks access

Write a file called post-receive

vim post-receive
Copy the code

Write the following

#! /bin/bash
DIR=/usr/hongtao/myblog
cd $DIR
echo $DIR
echo 'Server code Update'
unset $(git rev-parse --local-env-vars)
git fetch --all
git reset --hard origin/master
git pull
npm install
Copy the code

Add executable permissions

chmod +x post-receive
Copy the code

This hook will run every time our client submits code. Since our front-end code is a hot update, when the code is updated, we go directly to our latest page.