Use git and code cloud to implement automatic code deployment

Let me talk about the basic process

1、首先在码云创建一个项目,名为test

2、在本地创建git版本库,拉取码云上的test代码,与码云上的test项目建立连接

3、远程工作服务器克隆码云上的test代码,与码云上的test项目建立连接

4、在远程服务器上的工作目录建立一个webhooke.php文件,作为回调脚本

5、设置码云上test项目的webhook回调地址为上一步建立的文件地址

6、本地同步代码到码云后,远程工作服务器会自动从码云拉取代码

The process is relatively concise and easy to understand, but the process is tortuous. The main reason is that I am not familiar with the Linux server, which caused a detour.

Let’s review the entire implementation process and encounter pits below.

One, install git

Follow the tutorial searched by Baidu to install it step by step. Link: https://jingyan.baidu.com/article/e9fb46e16698687521f766ec.html An error occurred during the installation process, promptundefined reference to `libiconv'

Install libiconv, detailed link address: http://blog.csdn.net/daojibruce/article/details/75315401
Write picture description here

2. Pull the project on the work server

1. First generate the secret key on the server, and fill in the secret key into the code cloud settings

It should be noted here that when generating the secret key, the user generation of the http server is used. Otherwise, when running the webhook.php file and linking to git, the executor of the shell script is the user www of the http server, and the system will search for the corresponding secret key in the home directory of the www user.

Therefore, when generating the key, you cannot directly ssh-keygen -t rsa -C "Mailbox of Code Cloud". Writing in this way will directly generate the secret key in the home directory of the current user root. So we have to switch to the www user to execute this command, or run the command directly with sudo: sudo -u www ssh-keygen -t rsa -C "Mayun's mailbox", and then press Enter all the way.

However, I encountered another one with the following error output:/dev/null:NOt a directory

After searching for this problem for a long time, I finally found the cause of the problem. It is because the home directory of the www user is set incorrectly, so when the key is generated, the storage location cannot be found. Use the command to view the location of the user's home directory, as follows: The
Write picture description here
www user's home directory should be the www directory, but I don't know how to become dev/null. After changing to www, create a .ssh folder in the www directory, and run the command successfully.

Go to the .ssh directory, copy the string in the is_rsa.pub file, and fill in the code cloud

2. Clone the test project

Still use the www user, run the git command to clone the test project: sudo -u www git clone ssh:git@xxxxxxx

Then output an error message, prompting that no git command was found. It is because the path of git is not set in the environment variables of the current user, you can set the environment variables, or directly splice the path in front of git. Use git --exec-path to view the running path of git, and then execute:

sudo -u www /usr/local/libexec/git-core/git clone ssh:git@xxxxxxx

Three, webhook.php

The logic of the webhook.php file is relatively simple. After receiving the request sent by Code Cloud, verify the password to determine whether it is legal, and then execute the shell command: shell_exec("sudo -u www /usr/local/libexec/git-core/git pull");

Guess you like

Origin blog.csdn.net/u012830303/article/details/82255431