<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Monorepo on Jenny blog</title>
        <link>https://yue-jenny.github.io/tags/monorepo/</link>
        <description>Recent content in Monorepo on Jenny blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <lastBuildDate>Thu, 12 Jan 2023 14:56:21 +0800</lastBuildDate><atom:link href="https://yue-jenny.github.io/tags/monorepo/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>如何設計與實作 Monorepo 的 Github Workflow?</title>
        <link>https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/</link>
        <pubDate>Thu, 12 Jan 2023 14:56:21 +0800</pubDate>
        
        <guid>https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/</guid>
        <description>&lt;h2 id=&#34;如何設計與實作-monorepo-的-github-workflow&#34;&gt;如何設計與實作 Monorepo 的 Github Workflow?&lt;/h2&gt;
&lt;h3 id=&#34;前言&#34;&gt;前言&lt;/h3&gt;
&lt;p&gt;有鑑於越來越多人使用 monorepo，想研究一下，當只有針對某個 project 有程式碼的異動時，如何實踐 CI/CD&lt;/p&gt;
&lt;h3 id=&#34;什麼是-monorepo&#34;&gt;什麼是 monorepo?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;顧名思義，mono 表示一個，repo 表示 repository，也就是&lt;strong&gt;一個 repository 包含多個 projects&lt;/strong&gt;，並且清楚定義它們之間的關係&lt;/li&gt;
&lt;li&gt;例如前後端開發，將前後端的程式都放在同一個 repository，就是 &lt;code&gt;monorepo&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;結構大致上會呈現如下圖，這次舉例有兩個 app 在同一個 repository 中
&lt;ul&gt;
&lt;li&gt;&lt;img src=&#34;https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/monorepo-apps.jpg&#34;
	width=&#34;373&#34;
	height=&#34;250&#34;
	srcset=&#34;https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/monorepo-apps_hucdc1b9450550e74ad13d597903be3338_16536_480x0_resize_q75_box.jpg 480w, https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/monorepo-apps_hucdc1b9450550e74ad13d597903be3338_16536_1024x0_resize_q75_box.jpg 1024w&#34;
	loading=&#34;lazy&#34;
	
		alt=&#34;apps&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;149&#34;
		data-flex-basis=&#34;358px&#34;
	
&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;想知道必用的原因、好處或採坑紀錄，可先參考 👇
&lt;ul&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://monorepo.tools/#what-is-a-monorepo&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;what-is-a-monorepo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://blog.errorbaker.tw/posts/cwc329/monorepo/&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;monorepo 之我見&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;如何設計-github-workflow&#34;&gt;如何設計 Github workflow?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;參考&lt;a class=&#34;link&#34; href=&#34;https://blog.logrocket.com/creating-separate-monorepo-ci-cd-pipelines-github-actions/&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;Creating separate monorepo CI/CD pipelines with GitHub Actions&lt;/a&gt;，&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;以 project 來區分 workflows 檔案，有 db.yaml、gateway.yaml 以及worker.yaml，如下圖&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;當 gateway application 有程式碼異動，只能執行 gateway.yaml，測試或 build gateway image 等&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/structure.png&#34;
	width=&#34;730&#34;
	height=&#34;477&#34;
	srcset=&#34;https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/structure_hu572f924bb4941d326d5972d19c2d9ffb_167082_480x0_resize_box_3.png 480w, https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/structure_hu572f924bb4941d326d5972d19c2d9ffb_167082_1024x0_resize_box_3.png 1024w&#34;
	loading=&#34;lazy&#34;
	
		alt=&#34;ref: https://blog.logrocket.com/creating-separate-monorepo-ci-cd-pipelines-github-actions/&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;153&#34;
		data-flex-basis=&#34;367px&#34;
	
&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;實作&#34;&gt;實作&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Github Workflow YAML 檔案中是以 &lt;code&gt;on&lt;/code&gt; 定義，哪些 events 可以去 trigger 一個 workflow，因此我們需要在這邊動一些手腳，讓他只有在異動特定 application 時才會執行 workflow&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;利用 workflow syntax &lt;code&gt;paths&lt;/code&gt; 指定範圍，當 pet app 底下的檔案有異動，執行這個 workflow&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;  paths:
    - &amp;#39;apps/pet/**&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;如果想要再加入指定環境呢?比如指定 &lt;code&gt;stage&lt;/code&gt; 或 &lt;code&gt;main&lt;/code&gt;，可以利用 branches 的 workflow syntax&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;  branches: [&amp;#39;stage&amp;#39;]
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;結合以上，整個看起來應該會像這樣，指定在 &lt;code&gt;stage&lt;/code&gt; 的 pet app 有程式碼變化時，執行這個 workflow&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;  on:
      push:
          branches: [&amp;#39;stage&amp;#39;]
          paths:
              - &amp;#39;apps/pet/**&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;那如果是用 &lt;code&gt;pull request&lt;/code&gt; 呢?&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;  on:
      push:
          branches: [&amp;#39;stage&amp;#39;]
          paths:
              - &amp;#39;apps/pet/**&amp;#39;
      pull_request:
          branches: [&amp;#39;stage&amp;#39;]
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;我如何設計&#34;&gt;我如何設計?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;根據&lt;code&gt;兩種環境 stage 與 main&lt;/code&gt;，以及&lt;code&gt;兩種 app&lt;/code&gt;，總共會有 4 組 workflow yaml 檔案
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;stage-app1.yml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;stage-app2.yml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main-app1.yml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main-app1.yml&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;注意事項&#34;&gt;注意事項&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;❗ &amp;ldquo;You must store workflow files in the .github/workflows directory of your repository.&amp;rdquo;
&lt;ul&gt;
&lt;li&gt;Github Workflow 只支援第一層的 workflow yaml 檔案&lt;/li&gt;
&lt;li&gt;也就是，以下圖的結構來放 workflow 檔案，Github 是不會執行的任何 workflow 的&lt;/li&gt;
&lt;li&gt;&lt;img src=&#34;https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/inefficient-structure.jpg&#34;
	width=&#34;442&#34;
	height=&#34;82&#34;
	srcset=&#34;https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/inefficient-structure_hu4ca78239c6104ca3c35ff8e64c25b833_8785_480x0_resize_q75_box.jpg 480w, https://yue-jenny.github.io/2023/01/%E5%A6%82%E4%BD%95%E8%A8%AD%E8%A8%88%E8%88%87%E5%AF%A6%E4%BD%9C-monorepo-%E7%9A%84-github-workflow/inefficient-structure_hu4ca78239c6104ca3c35ff8e64c25b833_8785_1024x0_resize_q75_box.jpg 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;539&#34;
		data-flex-basis=&#34;1293px&#34;
	
&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;官方文件說明&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;心得&#34;&gt;心得&lt;/h2&gt;
&lt;p&gt;經過這次，除了了解 monorepo 外，對於 Github Actions 的 Workflow syntax 更多的認識。&lt;/p&gt;
&lt;h2 id=&#34;參考資料-&#34;&gt;參考資料 👐&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://blog.logrocket.com/creating-separate-monorepo-ci-cd-pipelines-github-actions/&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;Creating separate monorepo CI/CD pipelines with GitHub Actions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://monorepo.tools/#what-is-a-monorepo&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;what-is-a-monorepo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://blog.errorbaker.tw/posts/cwc329/monorepo/&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;monorepo 之我見&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;更多 workflow syntax 介紹&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;🍀 最後，若喜歡我的分享，可以免費幫我按讚，是對我最大的鼓勵! ✨ ✨ ✨&lt;/strong&gt;&lt;/p&gt;
</description>
        </item>
        
    </channel>
</rss>
