【2021鐵人賽】CI/CD從這裡:開始之前的準備(範例介紹)

前面在介紹Git Repo的時候有上傳了幾個C#的Project,裡面只有幾行簡單的程式,是為了接下來的CI/CD Pipeline文章而準備,不過當時只有上傳到Git Repo,並沒有介紹說明它們,這邊就用簡短的篇幅來了解一下到底有哪些內容吧!

首先,有個2021ironman的Solution方案檔,裡面有3個C#專案,分別是ConsoleApp、ModuleA、ModuleBase:

ModuleBase中只有一個IModuleInfo.cs,程式碼如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ModuleBase
{
    public interface IModuleInfo
    {
        public string Name { get; }
        public string Description { get; }

        void EntryPoint();
    }
}

ModuleA裡面只有一個Startup.cs,程式碼如下:

using System;

namespace ModuleA
{
    public class Startup : ModuleBase.IModuleInfo
    {
        public string Name => "ModuleA";
        public string Description => "ModuleA is a test module.";

        public void EntryPoint()
        {
            Console.WriteLine("Hello world, this is ModuleA.");
        }
    }
}

接著是ConsoleApp的Program.cs,程式碼如下:

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.Loader;
using ModuleBase;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo modulesDir = new DirectoryInfo("modules");
            Console.WriteLine("Hello World! This is Console app.");

            if (modulesDir.Exists)
            {
                var dlls = modulesDir.GetFiles("*.dll", SearchOption.AllDirectories);
                List<Assembly> assemblies = new List<Assembly>();

                foreach (var dll in dlls)
                {
                    var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(dll.FullName);

                    foreach (var type in assembly.GetTypes())
                    {
                        if (typeof(IModuleInfo).IsAssignableFrom(type) && !type.IsInterface)
                        {
                            IModuleInfo moduleInfo = Activator.CreateInstance(type) as IModuleInfo;
                            Console.WriteLine($"Load {moduleInfo.Name}, {moduleInfo.Description}.");
                            moduleInfo.EntryPoint();
                        }
                    }
                }
            }

            Console.ReadLine();
        }
    }
}

ModuleBase這個Class Library作為定義Module的基礎,雖然目前只有ModuleA,但是之後的ModuleB、ModuleC都要以ModuleBase為基底,所以ModuleBase會在後續用來製作成nuget package,在Azure DevOps中建立私有的nuget套件庫。

ModuleA實作ModuleBase中所定義的介面,如上面所說,之後會有ModuleB、ModuleC,如果每一個Module都替它們建立一個專屬的Pipeline,那數量會很可觀,所以之後建立的Pipeline中,會有一個是設計成可以讓使用者來告訴Pipeline應該要選擇哪一個來建置(Build)。

ConsoleApp的部份則是扮演主程式的角色,這裡也只是把Module的dll載入之後執行輸出結果而已,暫時沒有特別的用途。

初期就先以上面這些東西來準備接下來的Pipeline範例,下一篇我們就要實際來建立Pipeline了。

發佈留言

%d 位部落客按了讚: