1. 程式人生 > 其它 >dotnet 部署 GitHub 的 Action Runner 製作自託管執行器

dotnet 部署 GitHub 的 Action Runner 製作自託管執行器

技術標籤:c#gitgithubdotnetC#csharp

本文告訴大家如何在自己的 CI 伺服器上部署一個私有的 GitHub Action Runner 用來執行 GitHub 上的倉庫的構建

安裝

為了作為一個 dotnet 的 GitHub Action Runner 的伺服器,首先需要在自己的 CI 伺服器上安裝足夠的負載。我下載了 VS 安裝了所有能裝的功能

而 GitHub 的 Action Runner 執行器需要從 GitHub 倉庫拉下來程式碼,此時就需要本地有全域性配置了 Git 工具,在 https://git-scm.com/ 載安裝最新版本的 Git 工具

部署

根據官方文件 新增自託管的執行器 - GitHub Docs 進行部署

以下是我的部署程式碼

# Create a folder under the drive root
$ mkdir actions-runner; cd actions-runner
# Download the latest runner package
$ Invoke-WebRequest -Uri https://github.com/actions/runner/releases/download/v2.274.2/actions-runner-win-x64-2.274.2.zip -OutFile actions-runner-win-x64-2.274.2.zip
# Extract the installer
$ Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD/actions-runner-win-x64-2.274.2.zip", "$PWD")

上面程式碼是放在 PowerShell 上一條條執行的,在 PowerShell 裡面 # 代表這一行是註釋。而 $ 表示這是一行命令,因此咱只需要在 PowerShell 裡面輸入 $ 後面的內容

其實上面程式碼只是從 https://github.com/actions/runner/releases/download/v2.274.2/actions-runner-win-x64-2.274.2.zip 下載執行器,下載完成之後解壓縮到資料夾

而使用 Invoke-WebRequest 的下載速度不夠快,此時我和大家安利的下載工具 dotnetCampus.FileDownloader 工具。這是一個純 dotnet 開發的 dotnet tool 工具,在 GitHub 上完全開源,請看

https://github.com/dotnet-campus/dotnetCampus.FileDownloader

安裝此下載工具可以使用下面命令

dotnet tool install -g dotnetCampus.FileDownloader.Tool

安裝完成之後,可以使用下面命令下載,這個下載器提供了多執行緒下載

downloadfile -u https://github.com/actions/runner/releases/download/v2.274.2/actions-runner-win-x64-2.274.2.zip -o actions-runner-win-x64-2.274.2.zip 

下載完成之後,解壓縮到資料夾,呼叫 config.cmd 進行配置

配置

在你的 GitHub 的 Action 配置介面裡面,可以看到配置的命令內容,不同的開發者的配置內容不同

./config.cmd --url https://github.com/dotnet-campus --token AD2PSJSDOSETWXBS3M7GEVK73ATKS

上面程式碼請不要抄,因此你的配置一定和我的引數不相同

配置基本上一路下一步按回車就可以

為了在伺服器上有足夠的許可權執行指令碼,還需要使用管理員許可權開啟 PowerShell 輸入下面程式碼進行配置

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope LocalMachine

以上命令能解決構建的時候提示 The file C:\dotnet-campus\actions-runner\_work\_temp\ea5a132c-3f36-45cc-afc6-1bced18a0cd1.ps1 is not digitally signed. You cannot run this script on the current system. 的錯誤

如下面程式碼

Run dotnet build --configuration Release
. : File C:\dotnet-campus\actions-runner\_work\_temp\ea5a132c-3f36-45cc-afc6-1bced18a0cd1.ps1 cannot be loaded. The 
file C:\dotnet-campus\actions-runner\_work\_temp\ea5a132c-3f36-45cc-afc6-1bced18a0cd1.ps1 is not digitally signed. You 
cannot run this script on the current system. For more information about running scripts and setting execution policy, 
see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\dotnet-campus\actions-runner\_work\_temp\ea5a132c-3f36-45cc-afc ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Error: Process completed with exit code 1.

但是上面的 PowerShell 是不安全的,在輸入之前,還請大家先閱讀官方文件 about_Execution_Policies - PowerShell

執行

下一步呼叫 run.cmd 執行

./run.cmd

在咱自己的專案裡面,可以用上剛才自己搭建的伺服器,在 GitHub 的 Action 上,需要通過在 Yaml 配置檔案上設定在哪個伺服器上執行

dotnet 部署 github 的 Action 進行持續整合 可以瞭解是如何寫 YAML 配置檔案的,而咱需要在這個配置檔案裡面修改使用自己部署的執行器

jobs:
  build:
    # 將 windows-latest 換為 self-hosted 就可以了
    # runs-on: windows-latest
    runs-on: [self-hosted]

    steps:
    - uses: actions/[email protected]
 
    - name: Build with dotnet
      run: dotnet build --configuration Release

    - name: Test
      run: dotnet test --configuration Release

修改 runs-on 的內容就可以了

我搭建了自己的部落格 https://blog.lindexi.com/ 歡迎大家訪問,裡面有很多新的部落格。只有在我看到部落格寫成熟之後才會放在csdn或部落格園,但是一旦釋出了就不再更新

如果在部落格看到有任何不懂的,歡迎交流,我搭建了 dotnet 職業技術學院 歡迎大家加入

如有不方便在部落格評論的問題,可以加我 QQ 2844808902 交流

知識共享許可協議
本作品採用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。歡迎轉載、使用、重新發布,但務必保留文章署名林德熙(包含連結:http://blog.csdn.net/lindexi_gd ),不得用於商業目的,基於本文修改後的作品務必以相同的許可釋出。如有任何疑問,請與我聯絡。