Github Action添加NuGet源失败:已加明文参数仍报加密不支持
GitHub Action Ubuntu环境添加GitHub NuGet源失败的解决方法
问题详情
在GitHub Action的ubuntu-latest环境中执行添加GitHub NuGet源步骤时,即便已添加--store-password-in-clear-text参数,仍触发密码加密不支持的错误,Windows环境则正常。
Action配置
name: buildTest on: push: branches: [ main ] paths: - '**.cs' - '**.csproj' - '**build-test.yml' pull_request: branches: [ main ] paths: - '**.cs' - '**.csproj' - '**build-test.yml' env: DOTNET_VERSION: '6.0.401' # The .NET SDK version to use jobs: build: name: build-${{matrix.os}} runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 - name: Setup .NET Core uses: actions/setup-dotnet@v1 with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Add Github nuget source run: dotnet nuget add source --username ${{ github.actor }} --password ${{ secrets.BH_GH_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/PT/index.json" - name: Install dependencies run: dotnet restore - name: Build run: dotnet build --configuration Release --no-restore
报错信息
Run dotnet nuget add source "https://nuget.pkg.github.com/PT/index.json" --name github --username user-william -*** 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.
解决方案
1. 调整dotnet restore步骤,直接传入源和凭证(推荐)
跳过添加源的步骤,在dotnet restore时直接指定私有源和凭证,避免修改NuGet配置带来的跨平台问题:
- name: Install dependencies run: dotnet restore --source "https://api.nuget.org/v3/index.json" --source "https://nuget.pkg.github.com/PT/index.json" --username ${{ github.actor }} --password ${{ secrets.BH_GH_TOKEN }}
这种方式在CI环境中更可靠,不需要持久化修改NuGet源配置。
2. 清理NuGet配置文件(若存在旧配置干扰)
Ubuntu环境中若存在遗留的NuGet加密配置,会导致命令报错,可先清理配置再添加源:
- name: Clean existing NuGet config if: runner.os == 'Linux' run: rm -rf ~/.nuget/NuGet/NuGet.Config - name: Add Github nuget source run: dotnet nuget add source --username ${{ github.actor }} --password ${{ secrets.BH_GH_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/PT/index.json"
3. 升级actions/setup-dotnet版本
将actions/setup-dotnet从v1升级到v3,确保.NET SDK安装的兼容性和稳定性:
- name: Setup .NET Core uses: actions/setup-dotnet@v3 with: dotnet-version: ${{ env.DOTNET_VERSION }}
内容的提问来源于stack exchange,提问作者BillHaggerty
相关产品推荐
相关产品推荐

