index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const express = require('express')
  2. const app = express()
  3. const port = 3222
  4. const fs = require('fs')
  5. const Git = require('./utils/git')
  6. const ftpDeploy = require('./utils/ftpDeploy')
  7. // const { REMOTE_NAME_TEXT_MAP } = require('./config/remote_config')
  8. const readConfig = require('./utils/readConfig')
  9. const logsInit = require('./utils/log')
  10. const runCmds = require('./utils/runCmds')
  11. app.use(express.json())
  12. app.use(express.urlencoded({ extended: true }))
  13. app.post('/git-hooker', async (req, res) => {
  14. let body = req.body
  15. if (body.ref_type !== 'tag') return;
  16. const TAG = body.ref
  17. // let branch = TAG.indexOf('test') > -1 ? 'test' : ''
  18. // if (!branch) return
  19. const _projectName = body.repository.name
  20. var git = new Git(_projectName)
  21. await git.clone(body.repository.clone_url, _projectName)
  22. git = new Git(_projectName)
  23. await git.pull()
  24. let deplogConfig = readConfig(_projectName, TAG)
  25. let branch
  26. // 判断是否触发流程
  27. if (TAG.indexOf(deplogConfig.tagExg) > -1) {
  28. branch = deplogConfig.branch
  29. }
  30. if (!branch) return;
  31. logsInit(_projectName, TAG)
  32. await git.pull()
  33. await git.checkout(deplogConfig.branch)
  34. try {
  35. await runCmds(deplogConfig.cmds, _projectName)
  36. } catch (err) {
  37. console.log(err)
  38. }
  39. await ftpDeploy(deplogConfig.parameter)
  40. res.send({
  41. code: 0,
  42. msg: '完成'
  43. })
  44. })
  45. app.listen(port, () => console.log(`listening on port ${port}!`))