当前位置:   article > 正文

Gitee推送触发自动构建_todo gitee

todo gitee

java核心代码

  1. package com.webhook.controller;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.web.bind.annotation.*;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.Map;
  10. @RestController
  11. @RequestMapping("/webhook")
  12. public class PullController {
  13. @Value("${shell.startFileName:start.sh}")
  14. private String startFileName;
  15. @Value("${shell.directory}")
  16. private String directory;
  17. @Value("${shell.token:31346337}")
  18. private String token;
  19. private static final Logger logger = LoggerFactory.getLogger(PullController.class);
  20. /**
  21. * 请求
  22. * @param userAgent
  23. * @param giteeToken
  24. * @param giteeEvent
  25. * @return
  26. * @throws IOException
  27. */
  28. @PostMapping("/auto")
  29. public String auto(@RequestHeader("User-Agent") String userAgent,
  30. @RequestHeader("X-Gitee-Token") String giteeToken,
  31. @RequestHeader("X-Gitee-Event") String giteeEvent) throws IOException {
  32. //git-oschina-hook
  33. logger.info("User-Agent:{}", userAgent);
  34. //zhimaxxx
  35. logger.info("X-Gitee-Token:{}", giteeToken);
  36. //Push Hook
  37. logger.info("Gitee-Event:{}", giteeEvent);
  38. //todo 校验签名 https://gitee.com/help/articles/4290
  39. if("git-oschina-hook".equals(userAgent)
  40. && "PUSH HOOK".equals(giteeEvent)
  41. && token.equals(giteeToken)){
  42. //才去调用shell脚本
  43. executeShell();
  44. return "ok";
  45. }
  46. return "非法调用";
  47. }
  48. /**
  49. * 执行脚本
  50. * @throws IOException
  51. */
  52. public void executeShell() throws IOException {
  53. String fullName = getFullName(startFileName);
  54. File file = new File(fullName);
  55. if(!file.exists()) {
  56. logger.error("file {} not existed!", fullName);
  57. return;
  58. }
  59. //赋予755权限并调用
  60. ProcessBuilder processBuilder = new ProcessBuilder("/bin/chmod", "755", fullName);
  61. processBuilder.directory(new File(directory));
  62. Process process = processBuilder.start();
  63. int runningStatus = 0;
  64. try {
  65. runningStatus = process.waitFor();
  66. } catch (InterruptedException e) {
  67. logger.error("shell", e);
  68. }
  69. if(runningStatus != 0) {
  70. logger.error("failed.");
  71. }else {
  72. logger.info("success.");
  73. }
  74. }
  75. /**
  76. * 文件调用全路径
  77. * @param fileName
  78. * @return
  79. */
  80. private String getFullName(String fileName) {
  81. return directory + File.separator + fileName;
  82. }
  83. }

start.sh 脚本如下

  1. #!/bin/bash
  2. # 切到源码目录,以确保能正常执行
  3. cd /home/deploy/my-vuepress
  4. # 拉取最新代码
  5. git pull
  6. # 杀死目前已启动进程
  7. ID=`ps -ef|grep node | grep vuepress|awk '{print $2}'`
  8. echo --- the process is $ID ---
  9. kill -9 $ID
  10. echo "Killed $ID"
  11. # 启动
  12. nohup npm run docs:dev&

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/996137
推荐阅读
相关标签
  

闽ICP备14008679号