nuget-package - GitHub 操作 : automatically push NuGet package

标签 nuget-package github-actions github-package-registry

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

  • 在我的 nuget 组织帐户上,我生成了一个访问 token (用户名 - API key - 创建)
  • 在 Github 上(选择您的组织 - 查看组织 - 设置选项卡 - secret )我添加了一个名为 的 secret PUBLISH_TO_NUGET_ORG 和我的 nuget 访问 token

  • GitHub
  • 在我的个人帐户上,我生成了一个访问 token (帐户 - 设置 - 开发人员设置 - 个人访问 token - 生成)
  • 在 Github 上,我添加了一个名为 的 secret PUBLISH_TO_GITHUB_COM 和我的 github 访问 token

  • 这些是我的 Github 访问 token 的范围:
    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 提要失败并出现未经授权的错误。
    我查看了一些插件 like this one ,并且我想将此嵌入到我的操作中,以免多次构建我的项目。
    首先采取:
    dotnet nuget push *.nupkg --source https://nuget.pkg.github.com/MintPlayer/index.json --api-key ${{secrets.PUBLISH_TO_GITHUB_COM}} --skip-duplicate
    
    结果:
    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.
    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 的经验?
    Link to action configuration file
    编辑
    我尝试发送 POST 请求:
  • 网址:https://api.github.com/user
  • 授权:基本身份验证
  • 用户名:
  • 密码:<my-api-key>

  • 我正在获取我的用户信息,所以我的访问 token 绝对有效。
    编辑
    我还尝试在我的计算机上运行命令,用我自己的 token 替换 token ,这也有效。

    最佳答案

    原来我的解决方案中缺少一个 nuget.config 文件
    https://github.community/t/github-actions-automatically-push-nuget-package/128242/4
    配置文件

    <?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 Path/To/The.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: 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 }}
    

    关于nuget-package - GitHub 操作 : automatically push NuGet package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63489418/

    相关文章:

    c# - 无论如何,是否可以在 azure 函数中使用 AddSecretClient DI 扩展?

    visual-studio - 如何查看是否已为给定的 Nuget 包发布了符号包?

    github - 如何从 master 以外的分支运行 GitHub Action?

    reactjs - 每次我刷新我的 React 应用程序时,它都会显示 404 文件未找到

    visual-studio - 具有调试和发布功能的 Nuget 包

    c# - 我自己托管的 NuGet 服务器不再正常工作

    python-3.x - github Action yaml : how to refer an item in a matrix array?

    java - 如何将 maven 插件上传到 Github 包?

    github-package-registry - 如何从 Github 包注册表中删除/删除/取消链接/取消版本包

    github-package-registry - Verdaccio 和 Github 包注册表的比较