Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automate the deployment of your own project with Git

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to use Git to automate the deployment of your own project", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Git to automate the deployment of your own projects.

What is a Git hook?

The official website explains: like other version control systems, Git can trigger custom scripts when certain important actions occur. There are two sets of such hooks: client-side and server-side. Client-side hooks are invoked by operations such as commit and merge, while server-side hooks act on networking operations such as receiving pushed submissions. You can use these hooks as you like.

To put it simply, it is a script that is triggered in a particular environment. In other words, we automatically trigger the step that the server wants to pull our updated code, and automatically pull to the latest.

Let's automate deployment with hooks.

Step 1: create a git user

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

# create a user adduser jouzeyu named jouzeyu

Step 2: add permissions to the git user

# create a git folder mkdir / home/git# under the home folder in the root directory and switch to the created git folder cd / home/git# to create a .ssh folder, which is mainly used to put the public key mkdir .ssh # switch to the .ssh folder and create the authorized_keys file cd .sshtouch authorized_keys

Step 3: configure git and get the public key

# configure the user name and mailbox locally. My user name defaults to jouzeyugit config-- global user.name "jouzeyu" git config-- global user.email "your email"

Note: if you use the-- global option, then all your projects will use the user information configured here. If you want to use a different name or mailbox in a particular project, simply execute under that project:

Git config user.name "xxx" git config user.email "xxx"

Next, let's get the public key. Please check if your user's .ssh folder contains both public and private keys. We need to find a pair of files named id_dsa or id_rsa, one of which has a .pub extension. The .pub file is your public key, and the other is your private key. If not, run ssh-keygen.

Use the cat ~ / .ssh/id_rsa.pub command to get the public key, copy it, use the vi or vim command to paste it into the authorized_keys file we created earlier, and save it using: wq.

Step 4: initialize the warehouse

Create a folder to hold the git repository:

Mkdir / www/wwwroot/gitcd / www/wwwroot/git

Initialize the warehouse:

# initialize a naked repository (strongly recommended) git init-bare website.git# configuration repository permissions, so that the previously created git user jouzeyu can read and write chown-R git:git website.git

It must be noted here that if the permission is not given, the subsequent git pull will report an error because there is no permission to write. The difference between a naked warehouse and an ordinary warehouse is simply that the naked warehouse cannot see the project files, and the ordinary warehouse is the same as your project directory, except for an extra .git folder.

Step 5: generate the project repository

This is also done on the server, and note that / www/wwwroot/ is the root of my environment.

# create a project directory on my server, testmkdir / www/wwwroot/test#, clone repository, git clone / www/wwwroot/git/website.git#, set permissions chown-R git website

Note: be sure to pay attention to my path. The git repository is / www/wwwroot/git and the project warehouse is / www/wwwroot/test.

Step 6: clone to the local

# pull git clone git@47.97.121.XXX:/www/wwwroot/git/website.git# from a configured online repository via ip address. If you have a configured domain name, you can also pull git clone git@www.XXX.XXX:/www/wwwroot/git/website.git through the domain name.

Because of the public key, there is no need for a password here. If successful, a website folder will appear on your computer. If you report an error, please check it and then do the following.

Step 7: upload the code (git push)

# Open the local repository cd website# you just cloned and create the README.md file touch README.mdgit add .git commit-m "create README.md file" git push

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

Step 8: add a hook

The writing is relatively detailed, and now let's talk about hooks. Let's go back to our online server. The following is for online operation:

# change to this directory cd / www/wwwroot/git/website.git/hooks# to generate post-receive file touch post-receive# use vim to edit vim post-receive

Paste in the post-receive file:

#! / bin/sh# printout echo'= upload code to the server ='# it's important to open the online project folder cd / www/wwwroot/test/website# If you do not cancel, you will not be able to perform git operation on the path of cd. Unset GIT_DIRgit pull origin master# automatically compiles vue projects. If necessary, please remove the previous # # npm run build# automatic update composer (I haven't tried it yet) # composer updateecho $(date) > > hook.logecho'= code update completed ='

Add running permissions to the post-receive file after saving

Chmod + x post-receive

Last step

Modify some of the content locally, and then submit the push git push, you can see that we have achieved automated deployment.

At this point, I believe you have a deeper understanding of "how to use Git to automate the deployment of your own project". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report