The key to automatic deployment is to use Github’s Webhooks. After you set up a project on Github, go to the home page of the project and click Settings. You can click open webhooks to add a link. When the specified event occurs, will send to you specify the link to send a post request, and then you can know your which project what happened, and then do some specific operations, then you can use a shell script automatically pull and redeploy the implementation project, finally the effect of the implementation is in local push code, after a period of time, The effect is displayed on the website.
1. Configure the git SSH public and private keys
Google it yourself (the simplest basic operation) : Once configured, the service pulls github code
2. Configuration Webhooks
New project =>Settings=>Webhooks=>add hooks The configuration items are as follows:
Payload URL // Content of the interface address triggered by the hooktypeWhich events would you like to trigger this webhook The default is to select the first (push event) Active // whether to provide detailsCopy the code
3. Create automated deployment Shell scripts (or other scripts or means, Shell as an example)
#! /bin/bash
cd/opt/... git pull ... Start the commandCopy the code
4. Write an interface to invoke the automated deployment Shell (for example, node service KOA2, or other language services)
Save Secret in the server environment variable (for example, contos7).
Provisional effect:
exportSECRET_TOKEN= Secret specified when creating Webhooksecho $SECRET_TOKEN// Verify that it was successfulCopy the code
Permanent effect:
Vim /etc/profile at the end, add: SECRET_TOKEN= Secret that was entered when you created WebhooksexportSECRET_TOKEN Save and exitsource /etc/profile
echo $SECRET_TOKEN// Verify that it was successfulCopy the code
Create a KOA2 project and write an interface that calls the automated deployment Shell
Create a KOA2 project and configure the project in general (500 words omitted, please Google)
Key code:
const router = require('koa-router')()
const childProcess = require('child_process'// create subprocess const crypto = require('crypto') // Encryption and decryption tool router.post('/url'.function(CTX, next) {// The/URL must be the same as the interface path when you configure Webhooks.'x-hub-signature']
if(hubSignatureKV){// Obtain github signature const hubsignaturekv.slice (5) // Obtain system environment variable SECRET_TOKEN const Secret = process.env.secret_token // Create an hMAC object const hmac = crypto. CreateHmac (secret= crypto.'sha1', secret) // Add summary Content to the HMAC object (must be the request body because ContenttypeSet it to application/json, Const up = hmac.update(js.stringify (cxx.Request.body)) // Use the digest method to generate the encrypted content (must be in hex format) const signature = up.digest('hex')
if(hubSignature===signature){'/opt/shell/hexo.sh'.function(err){// Use the child process to execute the system command console.log(err) // Null when the success is error}) ctx.body ='Executed successfully'
}else{
ctx.body = 'The server has actively rejected your request'}}else{
ctx.body = 'The server has actively rejected your request'
}
})
module.exports = router
Copy the code
5. Deploy and verify the interface service
Deploy the service to the server (in this case, node service, pM2 daemon deployment is recommended). After successful deployment, push code verification
Get your Github going!
Author: Li Hui-xin, Research and development group of client end, research and development Center of Yu Da
Recruitment information
New students are wanted in the research and development Center!
FE/iOS/Android engineer
Company benefits:
- Full five insurance and one housing fund, and additional purchase of commercial insurance
- Free gym + annual physical
- 10% discount on rent near the company
- 2 promotion opportunities per year
Welcome to join us if you have a passion for technology! Resume please send to [email protected], or add wechat V-nice-v details chat!