【问题标题】:Getting version number for package in Github Actions在 Github Actions 中获取包的版本号
【发布时间】:2023-02-15 09:40:32
【问题描述】:

我正在尝试设置我的“第一个”GitHub 操作,以将 .NET 库的 NuGet 包发布到我的私人 GitHub Packages 注册表。

我希望我的操作从 .csproj 文件中获取包的版本号。我正在尝试按照此处的说明进行操作,但看起来他们对版本号进行了硬编码: https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry

如何从.csproj 文件中获取版本号?

到目前为止,这是我的release.yml 文件:

name: Publish MyApp NuGet to GitHub Packages

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

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
      
  package:
  
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    - name: Create NuGet package
      run: dotnet pack --configuration Release
  
  publish:
  
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    - name: Publish to GitHub Packages
      run: dotnet nuget push "bin/Release/MyApp.1.0.0.nupkg" --source "github"

这是nuget.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <packageSources>
        <clear />
        <add key="github" value="https://nuget.pkg.github.com/MY_GITHUB_COMPANY_ACCOUNT/index.json" />
    </packageSources>
    <packageSourceCredentials>
        <github>
            <add key="Username" value="MY_GITHUB_USERNAME" />
            <add key="ClearTextPassword" value="MY_GITHUB_PERSONAL_ACCESS_TOKEN" />
        </github>
    </packageSourceCredentials>
</configuration>

这是 .csproj 文件中我定义包相关信息的部分:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <Authors>MyCompany, LLC</Authors>
    <Company>MyCompany, LLC</Company>
    <Description>MyApp Library</Description>
    <Version>1.2.1</Version>
    <RepositoryUrl>https://github.com/MY_GITHUB_COMPANY_ACCOUNT/my-app</RepositoryUrl>
    <Copyright>MyCompany, LLC (c) 2015 - 2023</Copyright>
</PropertyGroup>

【问题讨论】:

    标签: .net github nuget github-actions github-package-registry


    【解决方案1】:

    尝试使用 Get CSProj Version GH 操作。此操作应从 .NET .csproj 项目文件中检索当前版本,并提供可在后续步骤中使用的输出。

    例子:

    ...
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v3
    
          - name: Get version
            id: package_version
            uses: KageKirin/get-csproj-version@v1.0.0
            with:
              file: src/project.csproj # Specify your .csproj file path
    
          - name: Publish to GitHub Packages
            run: dotnet nuget push "bin/Release/MyApp.${{ steps.test.package_version.version }}.nupkg" --source "github"
    

    类似动作: get-net-sdk-project-versions-action

    【讨论】:

    • 我是 GitHub 操作的新手,所以我从来不知道它存在。这很酷,而且很有魅力。谢谢你!
    【解决方案2】:

    有一个更好的方法来获得版本号,因此您不必考虑手动设置它。您可以使用GitVersion

    - name: Install GitVersion
      uses: gittools/actions/gitversion/setup@v0.9.7
      with:
        versionSpec: '5.x'
    
    - name: Determine Version
      uses: gittools/actions/gitversion/execute@v0.9.7
      with:
        useConfigFile: true
    

    这将设置 env var $GITVERSION_SEMVER 然后在你的推送步骤中你会说:

    - name: Your project
      run: |
        dotnet pack -p:Version=$GITVERSION_SEMVER projectToPack.csproj
        dotnet push ...
    

    还有一件事。如果您在.csproj 中手动指定版本,pack/push 将考虑该数字,因此不需要 -p:Version 东西。

    【讨论】:

      【解决方案3】:

      另一种方法是使用this action

      它不适用于 C#,但您可以通过指定特定路径和特定版本 prop 来处理它

      【讨论】:

        最近更新 更多