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
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
Step 3: Configure git and obtain the public key
#在本地配置用户名和邮箱,我的用户名默认为jouzeyu git config --global user.name "jouzeyu" git config --global user.email "your email"
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"
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
Initialize the warehouse:
#初始化一个裸仓库(强烈建议) git init --bare website.git #配置仓库的权限,让我们之前创建好的git用户jouzeyu能读写 chown -R git:git website.git
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
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
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
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
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 '======代码更新完成======'
After saving, add running permissions to the post-receive file:
chmod +x post-receive
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
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!