{"id":10020863,"date":"2025-06-29T04:02:23","date_gmt":"2025-06-28T20:02:23","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020863"},"modified":"2025-06-29T04:02:23","modified_gmt":"2025-06-28T20:02:23","slug":"%e5%a6%82%e4%bd%95%e7%bb%93%e5%90%88ci-cd%e6%b5%81%e6%b0%b4%e7%ba%bf%e6%a3%80%e6%9f%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020863","title":{"rendered":"\u5982\u4f55\u7ed3\u5408CI\/CD\u6d41\u6c34\u7ebf\u68c0\u67e5\uff1f"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5728CI\/CD\u6d41\u6c34\u7ebf\u4e2d\u6709\u6548\u5d4c\u5165\u68c0\u67e5\u73af\u8282\u7684\u5b9e\u8df5\u65b9\u6848\uff0c\u7ed3\u5408\u81ea\u52a8\u5316\u6d4b\u8bd5\u3001\u4ee3\u7801\u8d28\u91cf\u626b\u63cf\u3001\u5b89\u5168\u5408\u89c4\u53ca\u90e8\u7f72\u9a8c\u8bc1\uff1a<\/p>\n<p>\ud83d\udd27 \u200c\u4e00\u3001\u6838\u5fc3\u68c0\u67e5\u73af\u8282\u5d4c\u5165\u7b56\u7565\u200c<br \/>\u200c\u6d41\u6c34\u7ebf\u9636\u6bb5\u200c    \u200c\u68c0\u67e5\u7c7b\u578b\u200c    \u200c\u5de5\u5177\u793a\u4f8b\u200c    \u200c\u5173\u952e\u914d\u7f6e\u200c<br \/>\u200c\u4ee3\u7801\u63d0\u4ea4\u200c    \u9759\u6001\u4ee3\u7801\u5206\u6790    SonarQube34\u3001Checkstyle10    \u914d\u7f6e\u8d28\u91cf\u9608\uff08\u5982\u8986\u76d6\u7387\u226580%\uff09\uff0c\u963b\u585e\u4f4e\u8d28\u91cf\u5408\u5e76<br \/>\u200c\u6784\u5efa\u9636\u6bb5\u200c    \u5b89\u5168\u626b\u63cf\uff08\u4f9d\u8d56\/\u955c\u50cf\uff09    Trivy2\u3001OWASP Dependency Check2    \u626b\u63cf\u4f9d\u8d56\u6f0f\u6d1e\uff0c\u9ad8\u98ce\u9669\u6f0f\u6d1e\u4e2d\u65ad\u6784\u5efa<br \/>\u200c\u6d4b\u8bd5\u9636\u6bb5\u200c    \u81ea\u52a8\u5316\u6d4b\u8bd5\u8986\u76d6    JUnit12\u3001Selenium4    \u5355\u5143\u6d4b\u8bd5\u8986\u76d6\u7387\u226570%\uff0c\u96c6\u6210\u6d4b\u8bd5\u5168\u901a\u8fc7<br \/>\u200c\u9884\u53d1\u5e03\u73af\u5883\u200c    \u52a8\u6001\u5b89\u5168\u626b\u63cf    OWASP ZAP2\u3001Nessus    \u6a21\u62df\u653b\u51fb\u68c0\u6d4b\u8fd0\u884c\u65f6\u53ef\u6ce8\u5165\u6f0f\u6d1e<br \/>\u200c\u90e8\u7f72\u9636\u6bb5\u200c    \u5408\u89c4\u6027\u68c0\u67e5    Kubernetes\u7b56\u7565\u5f15\u64ce\uff08OPA\/PSP\uff09    \u6821\u9a8c\u5bb9\u5668\u6743\u9650\u3001\u7f51\u7edc\u7b56\u7565\u5408\u89c4\u6027<br \/>\u2699\ufe0f \u200c\u4e8c\u3001Java\u9879\u76eeCI\/CD\u68c0\u67e5\u914d\u7f6e\u793a\u4f8b\u200c\uff08GitLab CI\uff09<br \/>yaml<br \/>Copy Code<br \/>stages:<\/p>\n<ul>\n<li>build<\/li>\n<li>test<\/li>\n<li>security<\/li>\n<li>deploy<\/li>\n<\/ul>\n<h2>1. \u7f16\u8bd1\u4e0e\u5355\u5143\u6d4b\u8bd5<\/h2>\n<p>maven_build:<br \/>  stage: build<br \/>  image: maven:3.8-openjdk-17<br \/>  script:<\/p>\n<pre><code>- mvn clean package\n- mvn jacoco:report  # \u751f\u6210\u8986\u76d6\u7387\u62a5\u544a\n<\/code><\/pre>\n<h2>2. \u4ee3\u7801\u8d28\u91cf\u4e0e\u5b89\u5168\u68c0\u67e5<\/h2>\n<p>sonarqube_check:<br \/>  stage: test<br \/>  dependencies: [maven_build]<br \/>  script:<\/p>\n<pre><code>- mvn sonar:sonar -Dsonar.qualitygate.wait=true  # \u963b\u585e\u5f0f\u8d28\u91cf\u95e8\u7981:ml-citation{ref=&quot;3&quot; data=&quot;citationList&quot;}\n<\/code><\/pre>\n<h2>3. \u5bb9\u5668\u955c\u50cf\u6f0f\u6d1e\u626b\u63cf<\/h2>\n<p>container_scan:<br \/>  stage: security<br \/>  script:<\/p>\n<pre><code>- docker build -t myapp .\n- trivy image --exit-code 1 --severity CRITICAL myapp  # \u5173\u952e\u6f0f\u6d1e\u4e2d\u65ad\u6d41\u7a0b:ml-citation{ref=&quot;2&quot; data=&quot;citationList&quot;}\n<\/code><\/pre>\n<h2>4. \u90e8\u7f72\u5230\u9884\u53d1\u73af\u5883\u5e76\u8fd0\u884c\u5408\u89c4\u68c0\u67e5<\/h2>\n<p>deploy_staging:<br \/>  stage: deploy<br \/>  environment: staging<br \/>  script:<\/p>\n<pre><code>- kubectl apply -f k8s-manifest.yaml\n- kubectl label ns staging security=enforced  # \u542f\u7528\u547d\u540d\u7a7a\u95f4\u5b89\u5168\u7b56\u7565:ml-citation{ref=&quot;2&quot; data=&quot;citationList&quot;}<\/code><\/pre>\n<p>\ud83d\udea8 \u200c\u4e09\u3001\u5173\u952e\u68c0\u67e5\u9879\u5f3a\u5236\u963b\u65ad\u89c4\u5219\u200c<br \/>\u200c\u8d28\u91cf\u95e8\u7981\u62e6\u622a\u200c<br \/>SonarQube\u914d\u7f6e\uff1a\u4ee3\u7801\u91cd\u590d\u7387\uff1c5% + \u96f6\u4e25\u91cd\u5f02\u5473 + \u6d4b\u8bd5\u8986\u76d6\u7387\u8fbe\u6807<br \/>sonar-project.properties<br \/>Copy Code<br \/>sonar.coverage.jacoco.xmlReportPaths=target\/site\/jacoco\/jacoco.xml<br \/>sonar.qualitygate.wait=true<br \/>\u200c\u5b89\u5168\u5408\u89c4\u963b\u65ad\u200c<br \/>\u5bb9\u5668\u626b\u63cf\uff1a\u5b58\u5728CRITICAL\u6f0f\u6d1e\u7acb\u5373\u5931\u8d25<br \/>K8s\u90e8\u7f72\uff1a\u62d2\u7edd\u7279\u6743\u5bb9\u5668\u542f\u52a8\uff08\u901a\u8fc7PodSecurityPolicy\uff09<br \/>\u200c\u6d4b\u8bd5\u8986\u76d6\u7387\u5f3a\u5236\u8981\u6c42\u200c<br \/>Jacoco\u62a5\u544a\u9a8c\u8bc1\uff1a\u5206\u652f\u8986\u76d6\u7387\u672a\u8fbe\u9608\u503c\u5219\u6807\u8bb0\u5931\u8d25<br \/>xml<br \/>Copy Code<br \/>&lt;!&#8211; pom.xml\u914d\u7f6e &#8211;&gt;<br \/>&lt;plugin&gt;<br \/>  &lt;groupId&gt;org.jacoco&lt;\/groupId&gt;<br \/>  &lt;artifactId&gt;jacoco-maven-plugin&lt;\/artifactId&gt;<br \/>  &lt;configuration&gt;<\/p>\n<pre><code>&lt;rules&gt;&lt;rule&gt;&lt;limits&gt;&lt;limit&gt;LINE&lt;\/limit&gt;&lt;minimum&gt;70%&lt;\/minimum&gt;&lt;\/limits&gt;&lt;\/rule&gt;&lt;\/rules&gt;<\/code><\/pre>\n<p>&lt;\/configuration&gt;<br \/>&lt;\/plugin&gt;<br \/>\ud83d\udcca \u200c\u56db\u3001\u68c0\u67e5\u7ed3\u679c\u53ef\u89c6\u5316\u4e0e\u4f18\u5316\u200c<br \/>\u200c\u96c6\u4e2d\u5316\u62a5\u544a\u200c<br \/>SonarQube\u4eea\u8868\u76d8\u5c55\u793a\u4ee3\u7801\u8d28\u91cf\u8d8b\u52bf<br \/>Trivy\u626b\u63cf\u7ed3\u679c\u96c6\u6210\u81f3GitLab Security Tab<br \/>\u200c\u53cd\u9988\u95ed\u73af\u673a\u5236\u200c<br \/>\u5931\u8d25\u6784\u5efa\u81ea\u52a8\u901a\u77e5\u8d23\u4efb\u4eba\uff08Slack\/Jira\uff09<br \/>\u5b89\u5168\u6f0f\u6d1e\u5173\u8054\u6f0f\u6d1e\u5e93\u63d0\u4f9b\u4fee\u590d\u5efa\u8bae<br \/>\u200c\u6d41\u6c34\u7ebf\u6548\u80fd\u76d1\u63a7\u200c<br \/>\u8ddf\u8e2a\u68c0\u67e5\u9636\u6bb5\u8017\u65f6\uff08\u5982\u6d4b\u8bd5\u6267\u884c\u65f6\u95f4\uff09\u4f18\u5316\u8d44\u6e90\u5206\u914d<br \/>\u200c\u6570\u636e\u9a71\u52a8\u6539\u8fdb\u200c\uff1a\u901a\u8fc7\u5386\u53f2\u62a5\u544a\u5206\u6790\u9ad8\u9891\u9519\u8bef\u7c7b\u578b\uff08\u5982\u7a7a\u6307\u9488\u5f02\u5e38\uff09\uff0c\u53cd\u54fa\u5f00\u53d1\u89c4\u8303\u57f9\u8bad\u3002<\/p>\n<p>\ud83d\udca1 \u200c\u4e94\u3001\u6269\u5c55\u5b9e\u8df5\u5efa\u8bae\u200c<br \/>\u200c\u589e\u91cf\u626b\u63cf\u4f18\u5316\u200c\uff1a\u4ec5\u5bf9\u53d8\u66f4\u6587\u4ef6\u6267\u884cSonar\u626b\u63cf\uff0c\u52a0\u901f\u6d41\u6c34\u7ebf<br \/>\u200c\u6df7\u5408\u68c0\u67e5\u7b56\u7565\u200c\uff1a<br \/>PR\u5408\u5e76\u524d\uff1a\u8f7b\u91cf\u7ea7\u5feb\u901f\u68c0\u67e5\uff08\u57fa\u7840\u7f16\u8bd1+\u5355\u5143\u6d4b\u8bd5\uff09<br \/>\u4e3b\u5e72\u6784\u5efa\uff1a\u5168\u91cf\u6df1\u5ea6\u626b\u63cf\uff08\u5b89\u5168+\u96c6\u6210\u6d4b\u8bd5\uff09<br \/>\u200c\u5408\u89c4\u5373\u4ee3\u7801\u200c\uff1a\u7528Rego\u8bed\u8a00\u5b9a\u4e49\u5b89\u5168\u7b56\u7565\uff0c\u81ea\u52a8\u5316\u6821\u9a8cIaC\u6a21\u677f<br \/>\u901a\u8fc7\u4ee5\u4e0a\u5206\u5c42\u68c0\u67e5\u673a\u5236\uff0c\u53ef\u5c06\u4ee3\u7801\u7f3a\u9677\u62e6\u622a\u7387\u63d0\u534765%+\uff0c\u90e8\u7f72\u6545\u969c\u7387\u4e0b\u964d50%\uff08Google DevOps 2024\u62a5\u544a\uff09\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046806305\">https:\/\/segmentfault.com\/a\/1190000046806305<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5728CI\/CD\u6d41\u6c34\u7ebf\u4e2d\u6709\u6548\u5d4c\u5165\u68c0\u67e5\u73af\u8282\u7684\u5b9e\u8df5\u65b9\u6848\uff0c\u7ed3\u5408\u81ea\u52a8\u5316\u6d4b\u8bd5\u3001\u4ee3\u7801\u8d28\u91cf\u626b\u63cf\u3001\u5b89\u5168\u5408\u89c4\u53ca\u90e8\u7f72\u9a8c\u8bc1\uff1a  [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020863","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020863"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020863\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}