GitHub Actions:自动推送 NuGet 包

4
我正在尝试配置我的Github存储库,以便自动构建并将NuGet包推送到nuget.org和github.com。所以我想要的是,每次在主分支上进行提交,或者另一个分支合并到主分支时,github都会发布一个最新的Nuget包到Nuget和Github。

NuGet

  1. 在我的nuget组织帐户上,我生成了一个访问令牌(用户名-API密钥-创建)
  2. 在Github上(选择您的组织-查看组织-设置选项卡-秘密),我添加了一个名为PUBLISH_TO_NUGET_ORG 的秘密和我的nuget访问令牌

Github

  1. 在我的个人帐户上,我生成了一个访问令牌(帐户-设置-开发人员设置-个人访问令牌-生成)
  2. 在Github上,我添加了一个名为PUBLISH_TO_GITHUB_COM 的秘密和我的Github访问令牌

这些是我Github访问令牌的范围:

Scopes for my Github access token

设置

在我的Github存储库中,我已经设置了一个操作来还原、构建、测试、打包和发布:

name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.301
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal
#    - name: Publish
#      uses: brandedoutcast/publish-nuget@v2.5.2
#      with:
#          PROJECT_FILE_PATH: MintPlayer.SeasonChecker/MintPlayer.SeasonChecker.csproj
#          NUGET_KEY: ${{secrets.PUBLISH_TO_NUGET_ORG}}
#          INCLUDE_SYMBOLS: true
    - name: Pack
      run: dotnet pack --no-build --configuration Release MintPlayer.SeasonChecker/MintPlayer.SeasonChecker.csproj --output .
    - name: PushNuget
      run: dotnet nuget push *.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{secrets.PUBLISH_TO_NUGET_ORG}} --skip-duplicate
    - name: AddGithubSource
      run: dotnet nuget add source --username PieterjanDeClippel --password ${{secrets.PUBLISH_TO_GITHUB_COM}} --name github https://nuget.pkg.github.com/MintPlayer/index.json
    - name: PushGithub
      run: dotnet nuget push *.nupkg --source github --skip-duplicate

将内容推送到nuget.org没有问题,但将内容推送到我的GitHub供稿失败,出现未经授权的错误。

我查看了一些插件例如这个, 我想将其嵌入到我的操作中,以避免多次构建我的项目。

第一个方案:

dotnet nuget push *.nupkg --source https://nuget.pkg.github.com/MintPlayer/index.json --api-key ${{secrets.PUBLISH_TO_GITHUB_COM}} --skip-duplicate

结果:

结果: 警告:GitHub Packages 服务无法对您的请求进行身份验证。请确保您的访问令牌有效并已配置适当的范围。

warn : Your request could not be authenticated by the GitHub Packages service. Please ensure your access token is valid and has the appropriate scopes configured.

第二次尝试多个命令:

dotnet nuget add source --username PieterjanDeClippel --password ${{secrets.PUBLISH_TO_GITHUB_COM}} --name github https://nuget.pkg.github.com/MintPlayer/index.json
dotnet nuget push *.nupkg --source github --skip-duplicate

这个失败了,并显示以下(显而易见的)消息:
error: Password encryption is not supported on .NET Core for this platform. The following feed try to use an encrypted password: 'github'. You can use a clear text password as a workaround.
error:   Encryption is not supported on non-Windows platforms.

有人有将Nuget软件包自动发布到Github的经验吗?

链接到操作配置文件

编辑

我尝试发送POST请求:

我得到了我的用户信息,所以我的访问令牌绝对有效。

编辑

我还尝试在我的计算机上运行该命令,替换令牌为我的令牌,这也可以正常工作。


你如何自动递增NuGet软件包版本号? - user1034912
目前我还没有。你可以添加一个msbuild任务,使版本号的最后一个数字为日期yyyyMMddHHii,并提供一个<VersionPrefix>标签,生成完整的<Version>标签。 - Pieterjan
这里有一个MSBuild片段可以生成datetime.now,只需在前面加上一些<VersionPrefix>标签,并将其作为<Version>标签生成:<Version>$(VersionPrefix).$([System.DateTime]::Now.ToString(yyyyMMddHHii))</Version> - Pieterjan
2个回答

2
原来我的解决方案中缺少了一个 nuget.config 文件。

https://github.community/t/github-actions-automatically-push-nuget-package/128242/4

nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

我的工作流文件:
name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1.5.0
      with:
        dotnet-version: 3.1.301
        # Authenticates packages to push to GPR
        source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
      env:
        NUGET_AUTH_TOKEN: '%NUGET_AUTH_TOKEN%'
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal
    - name: Pack
      run: dotnet pack --no-build --configuration Release
    - name: PushNuget
      run: dotnet nuget push **/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.PUBLISH_TO_NUGET_ORG }} --skip-duplicate
    - name: PushGithub
      # The github token is automatically being pulled from the workflow
      run: dotnet nuget push **/*.nupkg --no-symbols --skip-duplicate
      env:
        NUGET_AUTH_TOKEN: ${{ github.token }}

0
根据 Github Actions 文档
<packageSourceCredentials>
    <github>
        <add key="Username" value="USERNAME" />
        <add key="ClearTextPassword" value="TOKEN" />
    </github>
</packageSourceCredentials>

所以我认为你只需要在你的nuget add source命令中设置-StorePasswordInClearText,因为你目前正在加密令牌。

参考资料:

Github Actions - https://docs.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-dotnet-cli-for-use-with-github-packages#authenticating-to-github-packages

Nuget Config Docs - https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接