{"id":61473,"date":"2024-10-13T11:21:37","date_gmt":"2024-10-13T03:21:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61473\/"},"modified":"2024-10-13T11:21:37","modified_gmt":"2024-10-13T03:21:37","slug":"github-%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab-ci-cd-%e5%84%aa%e7%a7%80%e5%af%a6%e8%b8%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61473\/","title":{"rendered":"GitHub \u7684\u6578\u64da\u5eab CI\/CD \u512a\u79c0\u5be6\u8e10"},"content":{"rendered":"<h1 id=\"github-%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab-ci-cd-%e5%84%aa%e7%a7%80%e5%af%a6%e8%b8%90-tMLihcgrZn\">GitHub \u7684\u6578\u64da\u5eab CI\/CD \u512a\u79c0\u5be6\u8e10<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u74b0\u5883\u4e2d\uff0c\u6301\u7e8c\u96c6\u6210\uff08CI\uff09\u548c\u6301\u7e8c\u4ea4\u4ed8\uff08CD\uff09\u5df2\u6210\u70ba\u63d0\u9ad8\u958b\u767c\u6548\u7387\u548c\u8edf\u4ef6\u8cea\u91cf\u7684\u95dc\u9375\u5be6\u8e10\u3002GitHub \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u4ee3\u78bc\u6258\u7ba1\u5e73\u53f0\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5de5\u5177\u548c\u529f\u80fd\u4f86\u652f\u6301\u9019\u4e9b\u5be6\u8e10\u3002\u672c\u6587\u5c07\u63a2\u8a0e GitHub \u5728\u6578\u64da\u5eab CI\/CD \u4e2d\u7684\u512a\u79c0\u5be6\u8e10\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u6578\u64da\u5eab\u8b8a\u66f4\u548c\u90e8\u7f72\u6d41\u7a0b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ci-cd%ef%bc%9f-tMLihcgrZn\">\u4ec0\u9ebc\u662f CI\/CD\uff1f<\/h2>\n<p>\u6301\u7e8c\u96c6\u6210\uff08CI\uff09\u662f\u6307\u958b\u767c\u8005\u983b\u7e41\u5730\u5c07\u4ee3\u78bc\u8b8a\u66f4\u5408\u4f75\u5230\u4e3b\u5206\u652f\uff0c\u4e26\u901a\u904e\u81ea\u52d5\u5316\u6e2c\u8a66\u4f86\u6aa2\u67e5\u9019\u4e9b\u8b8a\u66f4\u7684\u6b63\u78ba\u6027\u3002\u6301\u7e8c\u4ea4\u4ed8\uff08CD\uff09\u5247\u662f\u5728 CI \u7684\u57fa\u790e\u4e0a\uff0c\u5c07\u4ee3\u78bc\u81ea\u52d5\u90e8\u7f72\u5230\u751f\u7522\u74b0\u5883\uff0c\u78ba\u4fdd\u7528\u6236\u80fd\u5920\u96a8\u6642\u7372\u5f97\u6700\u65b0\u7684\u529f\u80fd\u548c\u4fee\u5fa9\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-github-%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab-ci-cd%ef%bc%9f-tMLihcgrZn\">\u70ba\u4ec0\u9ebc\u9078\u64c7 GitHub \u9032\u884c\u6578\u64da\u5eab CI\/CD\uff1f<\/h2>\n<ul>\n<li><strong>\u96c6\u6210\u6027\uff1a<\/strong>GitHub \u63d0\u4f9b\u4e86\u8207\u591a\u7a2e CI\/CD \u5de5\u5177\u7684\u96c6\u6210\uff0c\u5982 GitHub Actions\u3001Travis CI \u548c CircleCI\uff0c\u9019\u4f7f\u5f97\u8a2d\u7f6e\u81ea\u52d5\u5316\u6d41\u7a0b\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002<\/li>\n<li><strong>\u7248\u672c\u63a7\u5236\uff1a<\/strong>GitHub \u7684\u7248\u672c\u63a7\u5236\u529f\u80fd\u4f7f\u5f97\u6578\u64da\u5eab\u8b8a\u66f4\u53ef\u4ee5\u88ab\u8ffd\u8e64\u548c\u7ba1\u7406\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u56de\u6efe\u5230\u5148\u524d\u7684\u7248\u672c\u3002<\/li>\n<li><strong>\u793e\u5340\u652f\u6301\uff1a<\/strong>GitHub \u64c1\u6709\u9f90\u5927\u7684\u958b\u767c\u8005\u793e\u5340\uff0c\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u8cc7\u6e90\u548c\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/li>\n<\/ul>\n<h2 id=\"github-actions-%e7%9a%84%e4%bd%bf%e7%94%a8-tMLihcgrZn\">GitHub Actions \u7684\u4f7f\u7528<\/h2>\n<p>GitHub Actions \u662f GitHub \u63d0\u4f9b\u7684\u4e00\u500b\u5f37\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u81ea\u52d5\u5316\u5de5\u4f5c\u6d41\u7a0b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 GitHub Actions \u9032\u884c\u6578\u64da\u5eab CI\/CD \u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%ae%e5%b7%a5%e4%bd%9c%e6%b5%81%e7%a8%8b-tMLihcgrZn\">1. \u8a2d\u7f6e\u5de5\u4f5c\u6d41\u7a0b<\/h3>\n<p>\u9996\u5148\uff0c\u5728\u4f60\u7684 GitHub \u5009\u5eab\u4e2d\u5275\u5efa\u4e00\u500b\u540d\u70ba <code>.github\/workflows<\/code> \u7684\u76ee\u9304\uff0c\u7136\u5f8c\u5728\u5176\u4e2d\u5275\u5efa\u4e00\u500b YAML \u6587\u4ef6\uff0c\u4f8b\u5982 <code>ci-cd.yml<\/code>\u3002<\/p>\n<pre><code>name: CI\/CD for Database\n\non:\n  push:\n    branches:\n      - main\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - name: Checkout code\n        uses: actions\/checkout@v2\n\n      - name: Set up database\n        run: |\n          sudo apt-get install postgresql\n          sudo service postgresql start\n          psql -c \"CREATE DATABASE test_db;\"\n<\/code><\/pre>\n<h3 id=\"2-%e8%87%aa%e5%8b%95%e5%8c%96%e6%b8%ac%e8%a9%a6-tMLihcgrZn\">2. \u81ea\u52d5\u5316\u6e2c\u8a66<\/h3>\n<p>\u5728\u5de5\u4f5c\u6d41\u7a0b\u4e2d\u6dfb\u52a0\u81ea\u52d5\u5316\u6e2c\u8a66\u6b65\u9a5f\uff0c\u4ee5\u78ba\u4fdd\u6578\u64da\u5eab\u8b8a\u66f4\u4e0d\u6703\u5f15\u5165\u932f\u8aa4\u3002<\/p>\n<pre><code>      - name: Run tests\n        run: |\n          psql -d test_db -f tests.sql\n<\/code><\/pre>\n<h3 id=\"3-%e9%83%a8%e7%bd%b2%e5%88%b0%e7%94%9f%e7%94%a2%e7%92%b0%e5%a2%83-tMLihcgrZn\">3. \u90e8\u7f72\u5230\u751f\u7522\u74b0\u5883<\/h3>\n<p>\u6700\u5f8c\uff0c\u6839\u64da\u6e2c\u8a66\u7d50\u679c\u81ea\u52d5\u90e8\u7f72\u6578\u64da\u5eab\u8b8a\u66f4\u5230\u751f\u7522\u74b0\u5883\u3002<\/p>\n<pre><code>      - name: Deploy to production\n        if: success()\n        run: |\n          psql -d production_db -f migrations.sql\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%81%b7%e7%a7%bb%e7%ae%a1%e7%90%86-tMLihcgrZn\">\u6578\u64da\u5eab\u9077\u79fb\u7ba1\u7406<\/h2>\n<p>\u5728 CI\/CD \u6d41\u7a0b\u4e2d\uff0c\u6578\u64da\u5eab\u9077\u79fb\u662f\u81f3\u95dc\u91cd\u8981\u7684\u4e00\u6b65\u3002\u4f7f\u7528\u5de5\u5177\u5982 <code>Flyway<\/code> \u6216 <code>Liquibase<\/code> \u53ef\u4ee5\u5e6b\u52a9\u7ba1\u7406\u6578\u64da\u5eab\u7d50\u69cb\u7684\u8b8a\u66f4\u3002\u9019\u4e9b\u5de5\u5177\u53ef\u4ee5\u8207 GitHub Actions \u96c6\u6210\uff0c\u5be6\u73fe\u81ea\u52d5\u5316\u9077\u79fb\u3002<\/p>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-tMLihcgrZn\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u4fdd\u6301\u6578\u64da\u5eab\u8b8a\u66f4\u7684\u539f\u5b50\u6027\uff1a<\/strong>\u6bcf\u6b21\u8b8a\u66f4\u61c9\u8a72\u662f\u7368\u7acb\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u932f\u8aa4\u7684\u5f71\u97ff\u7bc4\u570d\u3002<\/li>\n<li><strong>\u5b9a\u671f\u5099\u4efd\u6578\u64da\u5eab\uff1a<\/strong>\u5728\u9032\u884c\u4efb\u4f55\u8b8a\u66f4\u4e4b\u524d\uff0c\u78ba\u4fdd\u6709\u6700\u65b0\u7684\u5099\u4efd\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li><strong>\u4f7f\u7528\u74b0\u5883\u8b8a\u6578\u7ba1\u7406\u654f\u611f\u4fe1\u606f\uff1a<\/strong>\u5728 CI\/CD \u6d41\u7a0b\u4e2d\uff0c\u4f7f\u7528\u74b0\u5883\u8b8a\u6578\u4f86\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u5b57\u7b26\u4e32\u7b49\u654f\u611f\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tMLihcgrZn\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528 GitHub \u9032\u884c\u6578\u64da\u5eab\u7684 CI\/CD\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u958b\u767c\u6548\u7387\u548c\u8edf\u4ef6\u8cea\u91cf\u3002\u5229\u7528 GitHub Actions \u548c\u5176\u4ed6\u5de5\u5177\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u81ea\u52d5\u5316\u6e2c\u8a66\u548c\u90e8\u7f72\u6d41\u7a0b\uff0c\u78ba\u4fdd\u6578\u64da\u5eab\u8b8a\u66f4\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u7ba1\u7406\u6578\u64da\u5eab\u7684\u5718\u968a\u4f86\u8aaa\uff0c\u9019\u4e9b\u5be6\u8e10\u7121\u7591\u662f\u503c\u5f97\u501f\u9452\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u958b\u767c\u5de5\u4f5c\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 GitHub \u6578\u64da\u5eab\u7684 CI\/CD \u512a\u79c0\u5be6\u8e10\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u78ba\u4fdd\u4ee3\u78bc\u8cea\u91cf\uff0c\u5be6\u73fe\u6301\u7e8c\u4ea4\u4ed8\u7684\u6700\u4f73\u7b56\u7565\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-61473","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=61473"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61473\/revisions"}],"predecessor-version":[{"id":61474,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61473\/revisions\/61474"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}