Home PHP Framework Laravel Use Git to implement automated deployment of Laravel projects

Use Git to implement automated deployment of Laravel projects

Sep 19, 2019 am 10:13 AM
git laravel

Use Git to implement automated deployment of Laravel projects

Introduction

In our development process, we will inevitably use version control. Of course, this also gives you an understanding of Git and SVN. Both are excellent version control tools. I am personally more accustomed to using Git. Of course, this may be related to personal habits. I don’t know how you used git for development in the first place. Anyway, I personally submitted the code to the github repository, then logged in to the server using SSH, and then cloned or updated the version. It sounds very troublesome, and of course it is also very troublesome in practice. So is there any way to "fix it once and for all"? Please read below!

Easy to use

Git hook

What is a git hook? The official explanation is a bit long. Simply put, it is a script that is triggered in a specific environment. This explanation may not be accurate, but I think it is easier to understand. If you want to know more, you can go to the Git official website to check it out. Below we will use hooks to implement automated deployment.

Step one: Create a git user

Log in to our server. By default, you have installed git. Create a git user:

# 创建一个名叫jouzeyu的用户
adduser jouzeyu
Copy after login

Step 2: Add permissions to the git user

#在根目录下的 home 文件夹下创建一个git文件夹
mkdir /home/git 
#切换到创建好的git文件夹
cd /home/git
#创建 .ssh文件夹,里面主要用来放公钥
mkdir .ssh
#切换到.ssh文件夹并创建authorized_keys文件
cd .ssh
touch authorized_keys
Copy after login

Step 3: Configure git and obtain the public key

#在本地配置用户名和邮箱,我的用户名默认为jouzeyu
git config --global user.name "jouzeyu"
git config --global user.email "your email"
Copy after login

Note: If you use the --global option, all your future projects will use the user information configured here. If you want to use another name or email address in a specific project, just execute under the project:

git config user.name "xxx"
git config user.email "xxx"
Copy after login

OK, next we get the public key, please check the .ssh file under your user first If the folder contains public and private keys before, we need to look for 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 obtain the public key, copy it, use the vi or vim command to paste it into the authorized_keys file we created previously, and use: wq to save it.

Step 4: Initialize the warehouse

Create a folder to store the git warehouse:

mkdir /www/wwwroot/git
cd /www/wwwroot/git
Copy after login

Initialize the warehouse:

#初始化一个裸仓库(强烈建议)
git init --bare website.git
#配置仓库的权限,让我们之前创建好的git用户jouzeyu能读写
chown -R git:git website.git
Copy after login

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

Step 5: Generate the project warehouse

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

#创建我服务器上的项目目录test
mkdir /www/wwwroot/test
#克隆仓库
git clone /www/wwwroot/git/website.git
#设置权限
chown -R git website
Copy after login

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

Step 6: Clone to local

# 通过ip地址从配置好的线上仓库拉取下来
git clone git@47.97.121.XXX:/www/wwwroot/git/website.git
# 如果有配置域名的话也可以通过域名拉取
git clone git@www.XXX.XXX:/www/wwwroot/git/website.git
Copy after login

Because of the public key, no password is required here. If successful, a website will appear on your computer. folder, if an error is reported, please check before proceeding with the following operations.

Step 7: Test upload (git pull)

# 打开刚才克隆下来的本地仓库
cd website
# 创建README.md文件
touch README.md
git add .
git commit -m"创建README.md文件"
git push
Copy after login

It has been uploaded normally. If an error is reported, please check the permissions. As mentioned above, if If it doesn’t work yet, you can comment below.

Step 8: Add the hook

Finally it’s time to get to the main point. The writing is relatively detailed, so it’s a little more troublesome. Back to our online server, the following is performed online:

#切换到这个目录
cd /www/wwwroot/git/website.git/hooks
# 生成post-receive文件
touch post-receive
# 使用vim编辑
vim post-receive
Copy after login

Paste in the post-receive file:

#!/bin/sh
# 打印输出
echo '======上传代码到服务器======'
# 打开线上项目文件夹
cd /www/wwwroot/test/website
# 这个很重要,如果不取消的话将不能在cd的路径上进行git操作
unset GIT_DIR
git pull origin master
# 自动编译vue项目,如有需要请去掉前面的#号
# npm run build
# 自动更新composer(我暂时没试过)
# composer update
echo $(date) >> hook.log
echo '======代码更新完成======'
Copy after login

After saving, add running permissions to the post-receive file:

chmod +x post-receive
Copy after login

The last step

Modify some content locally, and then submit and push git pull. You can see that we have implemented automated deployment.

Use Git to realize automated deployment of Laravel projects

Use Git to implement automated deployment of Laravel projects

For more Laravel-related technical articles, please visit the Laravel Framework Getting Started Tutorial column. study!

The above is the detailed content of Use Git to implement automated deployment of Laravel projects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to view the results after Bootstrap is modified How to view the results after Bootstrap is modified Apr 07, 2025 am 10:03 AM

Steps to view modified Bootstrap results: Open the HTML file directly in the browser to ensure that the Bootstrap file is referenced correctly. Clear the browser cache (Ctrl Shift R). If you use CDN, you can directly modify CSS in the developer tool to view the effects in real time. If you modify the Bootstrap source code, download and replace the local file, or rerun the build command using a build tool such as Webpack.

How to use vue pagination How to use vue pagination Apr 08, 2025 am 06:45 AM

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()

Laravel's geospatial: Optimization of interactive maps and large amounts of data Laravel's geospatial: Optimization of interactive maps and large amounts of data Apr 08, 2025 pm 12:24 PM

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter Monitor MySQL and MariaDB Droplets with Prometheus MySQL Exporter Apr 08, 2025 pm 02:42 PM

Effective monitoring of MySQL and MariaDB databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Prometheus MySQL Exporter is a powerful tool that provides detailed insights into database metrics that are critical for proactive management and troubleshooting.

How to view the JavaScript behavior of Bootstrap How to view the JavaScript behavior of Bootstrap Apr 07, 2025 am 10:33 AM

The JavaScript section of Bootstrap provides interactive components that give static pages vitality. By looking at the open source code, you can understand how it works: Event binding triggers DOM operations and style changes. Basic usage includes the introduction of JavaScript files and the use of APIs, and advanced usage involves custom events and extension capabilities. Frequently asked questions include version conflicts and CSS style conflicts, which can be resolved by double-checking the code. Performance optimization tips include on-demand loading and code compression. The key to mastering Bootstrap JavaScript is to understand its design concepts, combine practical applications, and use developer tools to debug and explore.

Is Git the same as GitHub? Is Git the same as GitHub? Apr 08, 2025 am 12:13 AM

Git and GitHub are not the same thing. Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions, and GitHub provides an online collaboration environment.

See all articles