gitee-webhook 部署测试

This commit is contained in:
cuizhibin 2025-06-25 13:43:58 +08:00
parent 84348d3ee8
commit c7de5c9db9
3 changed files with 12 additions and 6 deletions

View File

@ -1,6 +1,8 @@
package com.dite.znpt.web.build; package com.dite.znpt.web.build;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value; 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.*;
@ -12,6 +14,7 @@ import java.io.InputStreamReader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@Api(tags = "webhook自动部署")
@RestController @RestController
public class DeployController { public class DeployController {
@ -24,6 +27,7 @@ public class DeployController {
private Process deploymentProcess; private Process deploymentProcess;
private final GiteeSignatureVerifier signatureVerifier = new GiteeSignatureVerifier(); private final GiteeSignatureVerifier signatureVerifier = new GiteeSignatureVerifier();
@ApiOperation(value = "自动部署", httpMethod = "POST")
@PostMapping("/gitee-webhook") @PostMapping("/gitee-webhook")
public ResponseEntity<String> handleWebhook( public ResponseEntity<String> handleWebhook(
@RequestHeader(value = "X-Gitee-Token", required = false) String receivedSignature, @RequestHeader(value = "X-Gitee-Token", required = false) String receivedSignature,
@ -31,7 +35,7 @@ public class DeployController {
@RequestBody String payload) { @RequestBody String payload) {
// 0. 基本验证 // 0. 基本验证
if (StrUtil.isAllNotBlank(receivedSignature, timestamp)) { if (!StrUtil.isAllNotBlank(receivedSignature, timestamp)) {
return ResponseEntity.status(403).body("签名验证失败"); return ResponseEntity.status(403).body("签名验证失败");
} }
@ -47,6 +51,7 @@ public class DeployController {
return ResponseEntity.ok("部署流程已启动"); return ResponseEntity.ok("部署流程已启动");
} }
@ApiOperation(value = "查询自动部署状态", httpMethod = "GET")
@GetMapping("/deployment-status") @GetMapping("/deployment-status")
public ResponseEntity<String> getDeploymentStatus() { public ResponseEntity<String> getDeploymentStatus() {
try { try {
@ -62,6 +67,7 @@ public class DeployController {
} }
} }
@ApiOperation(value = "查询自动部署日志", httpMethod = "GET")
@GetMapping("/deployment-log") @GetMapping("/deployment-log")
public ResponseEntity<String> getDeploymentLog( public ResponseEntity<String> getDeploymentLog(
@RequestParam(defaultValue = "20") int lines) { @RequestParam(defaultValue = "20") int lines) {

View File

@ -1,13 +1,13 @@
package com.dite.znpt.web.build; package com.dite.znpt.web.build;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.net.URLEncodeUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant; import java.time.Instant;
import java.util.Base64;
@Slf4j @Slf4j
public class GiteeSignatureVerifier { public class GiteeSignatureVerifier {
@ -59,10 +59,10 @@ public class GiteeSignatureVerifier {
byte[] rawSignature = hmac.doFinal(data.getBytes(StandardCharsets.UTF_8)); byte[] rawSignature = hmac.doFinal(data.getBytes(StandardCharsets.UTF_8));
// Step 2: Base64 encode // Step 2: Base64 encode
String base64Sig = Base64.getEncoder().encodeToString(rawSignature); String base64Sig = Base64Encoder.encode(rawSignature);
// Step 3: URL encode // Step 3: URL encode
return URLEncoder.encode(base64Sig, StandardCharsets.UTF_8.toString()); return URLEncodeUtil.encode(base64Sig);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("签名计算失败", e); throw new RuntimeException("签名计算失败", e);
} }

View File

@ -140,4 +140,4 @@ upload:
# 部署配置 # 部署配置
deploy: deploy:
secret: cRc5888KAo4TxRS4y5iv35GM secret: cRc5888KAo4TxRS4y5iv35GM
app-dir: /home/dtyx/znpt-backend build-dir: /home/dtyx/znpt-backend