Skip to content

fix: use dotnet nuget update source to fix 401 on private NuGet feed in Docker#1063

Open
BenjaminMichaelis wants to merge 2 commits intomainfrom
fix/docker-nuget-update-source
Open

fix: use dotnet nuget update source to fix 401 on private NuGet feed in Docker#1063
BenjaminMichaelis wants to merge 2 commits intomainfrom
fix/docker-nuget-update-source

Conversation

@BenjaminMichaelis
Copy link
Copy Markdown
Member

@BenjaminMichaelis BenjaminMichaelis commented May 7, 2026

Cleanup

Both previous approaches wrote credentials to the wrong NuGet config
path (~/. nuget/config/ is not scanned on Linux; NuGet reads
~/.nuget/NuGet/NuGet.Config). Credentials were silently ignored -> 401.

New approach:
- CI writes raw PAT to a temp file, passed as BuildKit secret id=nuget_pat
- Dockerfile uses `dotnet nuget update source` to inject credentials
  directly into nuget.config (the correct config file) before restore
- nuget.config is backed up and restored in the same RUN instruction so
  credentials never appear in the committed layer
- packageSourceMapping in nuget.config is fully preserved (only the
  <packageSourceCredentials> section is modified by update source)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 7, 2026 00:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the container build pipeline to fix 401 Unauthorized restores against the private Azure DevOps NuGet feed by injecting credentials into the repo’s nuget.config during the Docker build via dotnet nuget update source.

Changes:

  • Switch Docker build authentication from a Linux-ignored ~/.nuget/config/credentials.config approach to dotnet nuget update source ... --configfile nuget.config using a BuildKit secret.
  • Update the GitHub Actions workflow to pass a PAT as a BuildKit secret (id=nuget_pat) instead of generating a credentials config file.
  • Restore nuget.config back to its original state within the same Docker layer after restore/build/publish.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
EssentialCSharp.Web/Dockerfile Injects NuGet feed credentials into nuget.config during build using dotnet nuget update source and a BuildKit secret, then restores the original config.
.github/workflows/Build-Test-And-Deploy.yml Writes the Azure DevOps PAT to a temp file and passes it into the Docker build as a BuildKit secret.

Comment thread EssentialCSharp.Web/Dockerfile Outdated
mkdir -p ~/.nuget/config && \
cp /run/secrets/nugetconfig ~/.nuget/config/credentials.config; \
RUN --mount=type=secret,id=nuget_pat,required=false \
if [ "$ACCESS_TO_NUGET_FEED" = "true" ] && [ -f /run/secrets/nuget_pat ]; then \
</packageSourceCredentials>
</configuration>
EOF
run: echo -n "${{ secrets.AZURE_DEVOPS_PAT }}" > ${{ runner.temp }}/nuget-pat.txt
- Remove temp PAT file entirely; pass secret inline to docker/build-push-action
  using "nuget_pat=${{ secrets.AZURE_DEVOPS_PAT }}" format (eliminates
  temp file creation, permissions concern, and cleanup step)
- Add fail-fast in Dockerfile: if ACCESS_TO_NUGET_FEED=true but secret
  missing, exit 1 with a clear error message instead of silently failing
  with a 401 later

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +25 to 31
cp nuget.config nuget.config.bak && \
if [ "$ACCESS_TO_NUGET_FEED" = "true" ]; then \
dotnet nuget update source EssentialCSharp \
--username az \
--password "$(cat /run/secrets/nuget_pat)" \
--store-password-in-clear-text; \
fi && \
Comment on lines +22 to +23
if [ "$ACCESS_TO_NUGET_FEED" = "true" ] && [ ! -f /run/secrets/nuget_pat ]; then \
echo "ERROR: ACCESS_TO_NUGET_FEED=true but nuget_pat secret is missing" >&2; exit 1; \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants