1. 程式人生 > 實用技巧 >Github自動構建Nuget包

Github自動構建Nuget包

工作流程

  1. 使用者git push
  2. 觸發github action
  3. 執行使用者配置的```yml``指令碼

如何配置?

  1. 首先要得到兩個憑證

    • Nuget Api Key

      • 點選建立 Create

      • 配置下 Copy 憑證 [找地方存起來]

    • Github Token

      • 進入github官網 https://github.com 並登入

      • 選中Settings

      • 選中Developer settings

      • 選中Personal access token

      • 點選Generate new token

      • 配置一下生成

      • 複製後[找地方存起來]

  2. 配置專案中

    如下圖所示配置憑證

  3. 新增Actions

    dotnet-core.yml

    name: .NET Core
    
    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v2
        - name: 建立.netcore環境
          uses: actions/setup-dotnet@v1
          with:
            dotnet-version: 3.1.301
        - name: 安裝依賴
          run: dotnet restore
        - name: 構建
          run: dotnet build --configuration Release --no-restore
        - name: 新增github倉庫
          run: |
            nuget sources add -name github -Source https://nuget.pkg.github.com/ORGANIZATION_NAME/index.json -Username ORGANIZATION_NAME -Password ${{ secrets.GitHubToken }}
        - name: 安裝nuget 
          uses: nuget/setup-nuget@v1
          with:        
            nuget-version: '5.x'
        - name: 釋出生成包到github和nuget
          run: |
            nuget push ./bin/Release/*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} -NoSymbol
            nuget push ./bin/Release/*.nupkg -Source github -SkipDuplicate