webhook_deploy_gogs.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * A server that listens on a port for Gogs's webhooks.
  3. * It will check for a push event on the master branch, then deploy the project on the machine.
  4. * The webhook's secret is check to ensure no malicious request from unknown sources.
  5. *
  6. * Usage :
  7. * Set the "WEBHOOK_SECRET" environment variable with the webhook's secret.
  8. *
  9. * @see https://gogs.io/docs/features/webhook
  10. *
  11. *
  12. * @author Antoine Sauvage <contact@asauvage.fr>
  13. * @license MIT 2019 - https://opensource.org/licenses/MIT
  14. * @see https://gist.github.com/rigwild/4238a13cb3501c6e85065b403a71b475
  15. */
  16. 'use strict'
  17. const fs = require('fs')
  18. const http = require('http')
  19. const path = require('path')
  20. const { promisify } = require('util')
  21. const exec = promisify(require('child_process').exec)
  22. // The port which this script will listen on
  23. const port = parseInt(process.env.WEBHOOK_PORT, 10)
  24. // The path to the project directory
  25. const projectPath = path.resolve('.')
  26. // The webhook secret to check the origin of the webhook event
  27. // Check the "WEBHOOK_SECRET" environment variable is set
  28. if (!process.env.WEBHOOK_SECRET && process.env.WEBHOOK_SECRET !== '') {
  29. console.error(`${new Date().toLocaleString()} - The "WEBHOOK_SECRET" environment variable is not set.`)
  30. process.exit(1)
  31. }
  32. const webhookSecret = process.env.WEBHOOK_SECRET
  33. // Check whether the project path exists and script has read access
  34. console.log(`${new Date().toLocaleString()} - Configured project path : ${projectPath}\n`)
  35. try {
  36. fs.accessSync(projectPath, fs.constants.W_OK)
  37. console.log(`${new Date().toLocaleString()} - The project's directory exists and script has write permission.`)
  38. }
  39. catch (err) {
  40. console.error(`${new Date().toLocaleString()} - The project's directory does not exist or script has not write permission.`, err)
  41. process.exit(1)
  42. }
  43. // Check the "PORT" environment variable is set to a valid integer
  44. if (!process.env.PORT || !parseInt(process.env.PORT, 10)) {
  45. console.error(`${new Date().toLocaleString()} - The "PORT" environment variable is not set or is not an integer.`)
  46. process.exit(1)
  47. }
  48. // Check the "SERVE_CLIENT" environment variable is set to 'true' or 'false'
  49. if (!process.env.SERVE_CLIENT || !['true', 'false'].some(x => x === process.env.SERVE_CLIENT)) {
  50. console.error(`${new Date().toLocaleString()} - The "SERVE_CLIENT" environment variable is not set or is not 'true' or 'false'`)
  51. process.exit(1)
  52. }
  53. const env = {
  54. PORT: parseInt(process.env.PORT, 10),
  55. SERVE_CLIENT: process.env.SERVE_CLIENT,
  56. IMAGES_PATH: process.env.IMAGES_PATH
  57. }
  58. // Recap used environment variables
  59. Object.keys(env).forEach(x => console.log(`${x}=${env[x]}`))
  60. // The script that will be executed by the machine
  61. const deployScript = `cd ${projectPath}` +
  62. ' && git reset --hard HEAD' +
  63. ' && git pull origin master' +
  64. ' && docker-compose down' +
  65. ' && docker-compose build' +
  66. ' && docker-compose up -d'
  67. console.log('\nConfiguration is valid. Starting the webhook-listener server ...')
  68. const deploy = async () => {
  69. try {
  70. console.log(`${new Date().toLocaleString()} - Deploying project ...`)
  71. const startTime = process.hrtime()
  72. const { stdout, stderr } = await exec(
  73. deployScript,
  74. {
  75. cwd: projectPath,
  76. env
  77. }
  78. )
  79. const endTime = process.hrtime(startTime)
  80. // Logs received from the deploy script are sent in stdout :
  81. // git fetch and docker-compose build/up are writing their success logs in stderr...
  82. // A deploy fail will be printed in stderr (in the catch)
  83. console.log('stdout :\n', stdout)
  84. console.log('stderr :\n', stderr)
  85. console.log(`\n${new Date().toLocaleString()} - Project successfully deployed with Docker.`)
  86. console.log(`Total deploy time : ${endTime[0]}s and ${endTime[1] / 1000000}ms.`)
  87. }
  88. catch (err) {
  89. console.error(`\n${new Date().toLocaleString()} - Error deploying project.\n`, err)
  90. }
  91. }
  92. // Configuration is fine, start the server
  93. http.createServer((req, res) => {
  94. // Check the method is POST
  95. if (req.method !== 'POST') {
  96. res.statusCode = 400
  97. res.write('Wrong HTTP method.')
  98. res.end()
  99. return
  100. }
  101. // Check the event is a push
  102. if (req.headers['x-gogs-event'] !== 'push') {
  103. res.statusCode = 200
  104. res.write('OK. Not a push event.')
  105. res.end()
  106. return
  107. }
  108. // Answer OK and close the connection
  109. res.statusCode = 200
  110. res.write('OK')
  111. res.end()
  112. let body = []
  113. req.on('data', chunk => body.push(chunk))
  114. req.on('end', () => {
  115. try {
  116. body = JSON.parse(Buffer.concat(body).toString())
  117. // Check if the event was on master
  118. if (!body.ref || body.ref !== 'refs/heads/master') return
  119. // Check if secret matches
  120. if (!body.secret || body.secret !== webhookSecret) return
  121. console.log(`${new Date()} - Valid webhook event. Push on master was sent. Deployement process starts.`)
  122. deploy()
  123. }
  124. catch (err) {
  125. console.error(`${new Date()} - Invalid JSON was received`)
  126. }
  127. })
  128. }).listen(port)
  129. console.log(`${new Date().toLocaleString()} - Server is listening on http://localhost:${port}/`)