新增gitea部署

This commit is contained in:
cuizhibin 2025-07-22 09:25:20 +08:00
parent 58ae758ece
commit 8a75441642
1 changed files with 38 additions and 0 deletions

View File

@ -7,10 +7,13 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -51,6 +54,41 @@ public class DeployController {
return ResponseEntity.ok("部署流程已启动"); return ResponseEntity.ok("部署流程已启动");
} }
@ApiOperation(value = "gitea自动部署", httpMethod = "POST")
@PostMapping("/gitea-webhook")
public ResponseEntity<String> handleWebhook(@RequestHeader("X-Gitea-Signature") String signature,
@RequestBody(required = false) byte[] body) {
// 1. 签名校验
if (!validSignature(body, signature)) {
throw new RuntimeException("签名错误");
}
// 2. 启动部署流程
startDeployment();
return ResponseEntity.ok("部署流程已启动");
}
private boolean validSignature(byte[] body, String sigHeader) {
if (sigHeader == null) return false;
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(webhookSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] hash = mac.doFinal(body);
String computed = "sha256=" + bytesToHex(hash);
return computed.equalsIgnoreCase(sigHeader);
} catch (Exception e) {
return false;
}
}
private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) sb.append(String.format("%02x", b));
return sb.toString();
}
@ApiOperation(value = "查询自动部署状态", httpMethod = "GET") @ApiOperation(value = "查询自动部署状态", httpMethod = "GET")
@GetMapping("/deployment-status") @GetMapping("/deployment-status")
public ResponseEntity<String> getDeploymentStatus() { public ResponseEntity<String> getDeploymentStatus() {