I. Demand background
Demand backgroundAutomatic code generation tool based on ProtoBuf message bodyThe article has been described, interested students can understand, this article is relatively simple, just a description of the process of the last article.
First shows the demand, as a result of the front-end origin_proto directory message body copy documents from the backend, he changes the backend must notify to the changes in the front end point, and put the specific file, change the content to the front, front end to replace the corresponding file content to the correct alignment, this operation is very silly x, Efficiency is also relatively low. From there, I was able to do a synchronization code task to synchronize the front and back end of the code
2. Program selection
- Scheme 1: The Webhook hook provided by GitLab triggers the execution of synchronization tasks
Principle: Each time the backend submits the code, it will send a request to the backend. The backend detects the proto directory of the backend, and updates the proTO directory of the backend to the front-end if any update is detected. That’s probably how it works. But just a few words, it is a very troublesome operation, and in the actual use, but also encounter problems. Here’s a rough list of the predictable downsides:
- Update detection is too frequent
Every time the back end commits the code, a check is triggered, and there are a lot of unnecessary checks
- No need to merge work
The background updates other files in the proto directory (files not available for use by the front end), but the server still detects the directory update, resulting in an additional unnecessary code merge
- Merge conflicts
In every time before the merge operation, while the service side will pull the latest code, but does not exclude the front-end someone to modify origin_proto directory files, and submitted in the server merge operation code, while the service side and then to submit may encounter conflict, subsequent requires artificial to initiate a request, partial trouble
- Scheme 2: Timing synchronization task execution based on Jenkins
Due to the shortcomings of scheme 1 and the high complexity of implementation, we still choose Scheme 2 in practical application.
Principle: Create a directory in the packaging server to store the front-end and back-end projects and write shell scripts. The general flow of the scripts is as follows:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 1 specific steps. Git log -- {path}/{proto menu of backend project}, record the last time that the backend proto directory was committed. Open the front end project, clear the code in the hold area, pull the latest code, and get the last commit time of the proto directory in the same way as fontTime # 3. If backTime > fontTime, it indicates that the proto file has been updated in the background, and the proto file needs to be updated, enter the fourth step, ** otherwise exit the process ** # 4. Clear the files under the proto directory of the front end, copy all proTos under the proto directory of the background project, and paste them into the corresponding directory file # 5 of the front end. Git add. => git commit -m "[{project name}][{version}][AUTO TASK][MERGE PROTO] Automatically MERGE PROTO files "=> git push # 6. Complete the task, exit process # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Copy the code
Next comes the code implementation
. function LogInfo() { echo "" echo "[INFO#]===================================================================" echo "[INFO#] $1 " echo "[INFO#]===================================================================" } function main() { ### Branch_name =$1 ## Whether to force merge 0: no 1: yes isforce=$2 echo "[= = = = = = = = = = = = = = = = = = BASE_PARAM = = = = = = = = = = = = = = = = = =]" echo "branch name: ${branch_name}" echo "whether mandatory update: ${isforce}" echo "[==================BASE_PARAM==================]" ################################ LogInfo "(1). Get Proto Folder Update Time "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # the project directory, open the background BACK_WORK_PATH="${XXX path}" BACK_PROTO_PATH="${XXX path}" CD $BACK_WORK_PATH ### Git fetch origin ${branch_name} ## Is out of the process git branch - a | grep -e "remotes/origin / ${branch_name} $" if [[$?! = 0]]; then LogInfo "No branch ${branch_name}, not need to sync" exit 0 fi; Git pull origin ${branch_name} ### BACK_LAST_MODIFY_TIMESTAMP= 'git log-1 --pretty=format:"%at" ${BACK_PROTO_PATH} '### echo "Back last modify time is: ${BACK_LAST_MODIFY_TIMESTAMP}" ### FRONT_WORK_PATH="${XXX path}" FRONT_PROTO_PATH="${XXX path}" CD $FRONT_WORK_PATH ## Git fetch origin ${branch_name} ## Is out of the process git branch - a | grep -e "remotes/origin / ${branch_name} $" if [[$?! = 0]]; then LogInfo "No branch ${branch_name}, not need to sync" exit 0 fi; Git pull origin ${branch_name} git pull origin ${branch_name} ### --pretty=format:"%at" ${FRONT_PROTO_PATH} '### echo "Front last modify time is: ${FRONT_LAST_MODIFY_TIMESTAMP}" LogInfo "(2). Compare Last Modify Time" ### If [$BACK_LAST_MODIFY_TIMESTAMP -gt $FRONT_LAST_MODIFY_TIMESTAMP -o ${isforce} -eq 1]; if [$BACK_LAST_MODIFY_TIMESTAMP -gt $FRONT_LAST_MODIFY_TIMESTAMP -o ${isforce} -eq 1]; Then ### update time in the background is longer than that in the front end. Echo "Need to Update" BACK_FILE_PATH="${XXX path}/*.proto" BACK_FILE_PATH="${XXX path}/*.proto" Rm -r ${FRONT_FILE_PATH}/* ### $${BACK_FILE_PATH} ${FRONT_FILE_PATH} ## LogInfo "(3) Copy Finish, Git status git add --all git Commit -m "[${project name}][$1][AUTO_TASK][PROTO_MERGE] --no-verify LogInfo "(4) Commit Finish, Start Push" git push origin ${branch_name} LogInfo "(5) Push Finish, Else ### LogInfo "No Change, Exit"; fi; } ### Start main $@Copy the code
OK, this is the end of the script implementation, is not very simple
Next up is Jenkins
- Create a New Jenkins task
- Configuration tasks
Git branch configuration is mandatoryParameter isforce is optionalThen there is the timed build configuration (synchronization task by default at 2am)Next comes the Build configuration, select the machine to run, and then fill in the execute command
Bingo! That’s the end of the process
- 3. Results display
If you look at the log of the synchronization task, you can see that it has been perfectly merged
And then don’t merge if you don’t need to merge. Perfect!
Well, that’s the end of sharing this module!
This is adorable new second article, hope the big guys give more advice!
Shoot shoot you