fix: use dotnet nuget update source to fix 401 on private NuGet feed in Docker#1063
Open
BenjaminMichaelis wants to merge 2 commits intomainfrom
Open
fix: use dotnet nuget update source to fix 401 on private NuGet feed in Docker#1063BenjaminMichaelis wants to merge 2 commits intomainfrom
BenjaminMichaelis wants to merge 2 commits intomainfrom
Conversation
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>
Contributor
There was a problem hiding this comment.
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.configapproach todotnet nuget update source ... --configfile nuget.configusing 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.configback 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. |
| 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>
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; \ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cleanup