Demand:
Write an automated deployment script that automatically updates the release code according to the Git Push action (patch file (or update file, patch for short))
Solution:
With git webhooks, Python scripts, and Linux shell scripts, all actions are done automatically, and the manual part is limited to the release of patch files
git webhooks implementation triggers execution script
Python scripts are used to receive post data from Git webhooks and determine whether to execute shell scripts based on this data
Linux shell scripts for operations such as deployment, backup, rollback, etc.
Specific steps (scenario implementation): The principle of implementation:
After the user has written the patch, in addition to push the patch file to Git, Writes the path of the patch file and related operations to the specified file (for example, the name update.conf, the short profile), the Automated Deployment script (hereinafter referred to as the script) performs the relevant operations on the production server based on the configuration file, assigns the files the correct permissions, and clears the cache if it needs to be cached (about whether you need to Clear the cache, which can be obtained from the specified file mentioned above, and restart related services as needed
Conventions about specifying file formats. You can follow the following format:
file path/to/filename1 addfile path/to/filename2 removefile path/to/filename3 updatefile path/to/filename4 addconfig cleancache enableconfig cleancache disableconfig restartservice enableconfig restartservice disabletarget serverid ... ...  &NBSP, .....
File represents the patch file, path/to/filename1 represents the path to the files, add, remove, and update indicate the action of the file execution
Config indicates configuration, clears cache and restarts service, and is determined by enable and disable
Target represents the IP or hostname of the production server, and each target can be ssh-connected by an update server
The work and steps of the script completion (some are principles and related explanations):
The GIT server triggers the webhooks action on the GIT service based on the user's git push action, and the GIT servers post the JSON data to the update server
The update server captures JSON data sent from the Git service, obtains repository URLs and branch, and, depending on the information in the JSON, determines whether git fetch is required to get the patch files, and whether the deployment action needs to be performed
When performing a deployment operation, the update server backs up the code on the production server to a backup server or other backup location on the production server, depending on the configuration file above to decide whether to add or remove files, whether to clear the cache and whether to restart the service
The update server can ssh to the production server, file operations, clear cache and restart the service commands are all done via SSH
Detects the relevant API, determines whether the update is successful, logs the update log if the update is successful, rolls back the file if the update fails, and then decides whether to clear the cache and restart the service if necessary
Tag:git WebHooks, automated deployment, shell scripting, Python Basehttpserver,python receive WebHooks
--end--
This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1734297
Automatic update of release code via git webhooks+ script