You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CircleCI集成GitVersion生成NuGet包失败问题排查求助

问题描述

原本正常运行的CircleCI流水线(用于生成NuGet包并上传至nuget.org)突然失效,报错显示dotnet gitversion未输出合法JSON对象,替换jq为fx后仍触发SyntaxError: JSON value expected on line 1错误。当前环境已升级至.NET 8.0,对应GitVersion版本为6.0,流水线的config.yml配置如下:

version: 2.1

jobs:

  build-and-test:
    docker:
      - image: mcr.microsoft.com/dotnet/sdk:8.0
    steps:

      - checkout

      - run:
          name: Cambiar directorio
          command: cd ./Repo
         
      - run:
          name: Instalar Node
          command: apk add nodejs

      - run:
          name: Instalar NPM
          command: apk add npm

      - run:
          working_directory: ./Repo
          name: Ejecutar tests
          command: |
            dotnet test
            << pipeline.git.tag >>
      - run:
          working_directory: ./Repo
          name: Compilar version Release
          command: |
            dotnet build --configuration Release

      - run:
          name: Patchear imagen
          command: |
            apk add openssh git jq

      - run:
          name: Generar manifest
          command: |
            dotnet new tool-manifest

      - run:
          name: Instalar GitVersion
          command: |
            dotnet tool install GitVersion.Tool

      - run:
          working_directory: ./Repo/Components
          name: Generar paquete nuget
          command: |
            VERSION=$(dotnet gitversion | jq -r ".SemVer") 
            dotnet pack --configuration Release -p:Version=$VERSION -o ./outputs/packages/
            
      - store_artifacts:
          path: ./Repo/Components/outputs/packages
          destination: packages

      - persist_to_workspace:
          root: ./Repo/Components/outputs/packages
          paths:
            - "*.nupkg"
            - "*.snupkg"
  push:
    docker:
      - image: mcr.microsoft.com/dotnet/sdk:8.0
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run:
          name: Pushear a Nuget.org
          command: dotnet nuget push /tmp/workspace/*.* --api-key someapikey --skip-duplicate --source https://api.nuget.org/v3/index.json

workflows:
  setup:
    when:
      and:
        - equal: [ main, << pipeline.git.branch >> ]
    jobs:
      - build-and-test:
          filters:
            tags:
              only: /.*/
      - push:
          requires:
            - build-and-test
          context:
            - NuGet
          filters:
            tags:
              only: /^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
解决办法
  • 强制GitVersion输出JSON格式:GitVersion 6.x默认输出是人类可读文本而非JSON,必须显式指定--output json参数才能让jq/fx解析。修改生成NuGet包步骤的命令:
    VERSION=$(dotnet gitversion --output json | jq -r ".SemVer")
    
  • 统一工作目录:当前dotnet new tool-manifest和dotnet tool install在根目录执行,但后续在./Repo/Components运行dotnet gitversion,可能导致GitVersion无法正确识别仓库根目录。要么把工具安装步骤移到./Repo/Components目录,要么指定仓库根路径:
    VERSION=$(dotnet gitversion --output json --repository-root ./../.. | jq -r ".SemVer")
    
  • 排查GitVersion执行日志:在流水线中添加一步单独执行dotnet gitversion --output json,直接查看输出内容,确认是否有错误提示(比如仓库无提交、配置文件缺失等):
    dotnet gitversion --output json
    
  • 检查GitVersion配置文件:确保仓库根目录存在GitVersion.yml,且配置适配6.x版本(比如mode: Mainline这类核心配置是否存在)。
  • 修复流水线步骤逻辑:
    • Cambiar directorio步骤的cd ./Repo不会影响后续步骤的工作目录,后续步骤必须通过working_directory指定路径,或者把所有步骤统一到./Repo下执行。
    • Patchear imagen步骤安装git的时机太晚,checkout步骤已执行,可能导致Git环境异常,建议把安装git的步骤移到checkout之前。

内容的提问来源于stack exchange,提问作者Mauro Alvarez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 04:05:16