web-dev-qa-db-ja.com

GitHubアクションでnugetパッケージをプッシュする方法

GitHubアクションを使用してプロジェクトからNuGetパッケージを生成し、それを(プライベート)GitHubレジストリにプッシュしようとしています。

私のスクリプト([NAME]フィールドは編集済み):

name: Update NuGet

on: [Push]

jobs:
  build:
    runs-on: ubuntu-latest

    name: Update NuGet 
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '2.2.105'
      - name: Package Release
        run: |  
          cd [SOLUTION_FOLDER]
          dotnet pack -c Release -o out
      - name: Publish Nuget to GitHub registry
        run: dotnet nuget Push ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out/$(ls ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out) -s https://nuget.pkg.github.com/[USERNAME]/index.json -k ${GITHUB_TOKEN}  
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 

ログ出力:

info : Pushing [PROJECT_FOLDER].3.4.23.nupkg to 'https://nuget.pkg.github.com/[USERNAME]'...
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
error: An error occurred while sending the request.
error:   The server returned an invalid or unrecognized response.
##[error]Process completed with exit code 1.

これは対応するGitHubの問題です(回避策オプションあり): https://github.com/NuGet/Home/issues/858

13
jwillmer

プロジェクトファイルに以下が含まれていることを確認してください

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <OutputType>Library</OutputType>
    <PackageId>Example.PackageName</PackageId>
    <Version>1.0.0</Version>
    <Authors>Author Engineering</Authors>
    <Company>Company Inc</Company>
    <PackageDescription>This package for ...!</PackageDescription>
    <RepositoryUrl>
https://github.com/YOUR_ACCOUNT/Example.PackageName</RepositoryUrl>
  </PropertyGroup>

これは、ビルド、パッケージ化、公開、バージョン管理のためのmain.yamlである必要があります。

name: Continuous Integration

on:
  Push:
    branches:
      - master

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Setup Dotnet Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.100
        source-url: https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json
      env:
        NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

    - name: Setup Nuget Config
      run: sed 's/GITHUB_TOKEN/${{ secrets.GITHUB_TOKEN }}/g' .nuget.config > nuget.config

    - name: Build
      run: dotnet build --configuration Release

    - name: Version and Tag
      id: bump_version
      uses: mathieudutour/github-tag-action@v1
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

    - name: Prep Version String
      run: echo ::set-env name=VERSION_NUMBER::$(echo ${{ steps.bump_version.outputs.new_tag }} | sed 's/[v]//g')

    - name: Define Package Name
      run: echo ::set-env name=PACKAGE_NAME::$"Example.PackageName/bin/Release/Example.PackageName.${{ env.VERSION_NUMBER }}.nupkg"

    - name: Set Nuget Package Version
      uses: roryprimrose/set-vs-sdk-project-version@v1
      with:
        version: ${{ env.VERSION_NUMBER }}

    - name: Pack
      run: dotnet pack --configuration Release Example.PackageName

    - name: Publish Package
      run: dotnet nuget Push Example.PackageName/bin/Release/*.nupkg --source https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json

    - name: Create Release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.bump_version.outputs.new_tag }}
        release_name: Release ${{ github.ref }}

0
Delor_Tshimanga

私の作業ソリューション:

  • 'usernamecompanyname'をリポジトリの有効な値に置き換えます
  • ビルドとパックを分離して、何か問題が発生した場合のデバッグを容易にしました
  • githubシークレットのACTIONS_RUNNER_DEBUG変数をtrueに設定すると、より詳細なデバッグが可能になります
  • dotnet-versionを目的のdotnet-sdkバージョンに変更します
  • Github repo secrestsでGITHUB_TOKENを指定する必要はありません。このトークンはデフォルトで存在します

build_and_publish_nuget.yml:

name: Build and publish package

# Controls when the action will run. Triggers the workflow on Push or pull request 
# events but only for the master branch
on:
  Push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@master

      - name: Setup .NET environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '3.1.102'
          source-url: https://nuget.pkg.github.com/usernamecompanyname/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

      - name: Build project
        run: dotnet build -c Release

      - name: Generate a NuGet package
        run: dotnet pack --no-build -c Release -o .

      - name: Push to GitHub package registry
        run: dotnet nuget Push *.nupkg
0
Alby