Skip to content

Commit 691f4af

Browse files
fix: use dotnet nuget update source with raw PAT secret
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>
1 parent 621e630 commit 691f4af

2 files changed

Lines changed: 14 additions & 23 deletions

File tree

.github/workflows/Build-Test-And-Deploy.yml

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,9 @@ jobs:
6363
- name: Set up Docker Buildx
6464
uses: docker/setup-buildx-action@v4
6565

66-
- name: Generate NuGet auth config for Docker build
66+
- name: Write NuGet PAT for Docker build
6767
if: github.event_name != 'pull_request' && github.event_name != 'merge_group'
68-
env:
69-
NUGET_AUTH_TOKEN: ${{ secrets.AZURE_DEVOPS_PAT }}
70-
run: |
71-
cat > /tmp/nuget-auth.config << EOF
72-
<?xml version="1.0" encoding="utf-8"?>
73-
<configuration>
74-
<packageSourceCredentials>
75-
<EssentialCSharp>
76-
<add key="Username" value="docker" />
77-
<add key="ClearTextPassword" value="${NUGET_AUTH_TOKEN}" />
78-
</EssentialCSharp>
79-
</packageSourceCredentials>
80-
</configuration>
81-
EOF
68+
run: echo -n "${{ secrets.AZURE_DEVOPS_PAT }}" > ${{ runner.temp }}/nuget-pat.txt
8269

8370
# Build but no push with a PR
8471
- name: Docker build (no push)
@@ -99,7 +86,7 @@ jobs:
9986
file: ./EssentialCSharp.Web/Dockerfile
10087
context: .
10188
secrets: |
102-
"id=nugetconfig,src=/tmp/nuget-auth.config"
89+
"id=nuget_pat,src=${{ runner.temp }}/nuget-pat.txt"
10390
outputs: type=docker,dest=${{ github.workspace }}/essentialcsharpwebimage.tar
10491
cache-from: type=gha
10592
cache-to: type=gha,mode=max
@@ -109,9 +96,9 @@ jobs:
10996
name: essentialcsharpwebimage
11097
path: ${{ github.workspace }}/essentialcsharpwebimage.tar
11198

112-
- name: Clean up NuGet auth config
99+
- name: Clean up NuGet PAT file
113100
if: always()
114-
run: rm -f /tmp/nuget-auth.config
101+
run: rm -f ${{ runner.temp }}/nuget-pat.txt
115102

116103
deploy-development:
117104
if: github.event_name != 'pull_request_target' && github.event_name != 'pull_request'

EssentialCSharp.Web/Dockerfile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ ENV ACCESS_TO_NUGET_FEED=$ACCESS_TO_NUGET_FEED
1818
WORKDIR /src
1919
COPY . .
2020
COPY --from=frontend-build /frontend/EssentialCSharp.Web/wwwroot/dist ./EssentialCSharp.Web/wwwroot/dist
21-
RUN --mount=type=secret,id=nugetconfig,required=false \
22-
if [ "$ACCESS_TO_NUGET_FEED" = "true" ] && [ -f /run/secrets/nugetconfig ]; then \
23-
mkdir -p ~/.nuget/config && \
24-
cp /run/secrets/nugetconfig ~/.nuget/config/credentials.config; \
21+
RUN --mount=type=secret,id=nuget_pat,required=false \
22+
if [ "$ACCESS_TO_NUGET_FEED" = "true" ] && [ -f /run/secrets/nuget_pat ]; then \
23+
cp nuget.config nuget.config.bak && \
24+
dotnet nuget update source EssentialCSharp \
25+
--username docker \
26+
--password "$(cat /run/secrets/nuget_pat)" \
27+
--store-password-in-clear-text \
28+
--configfile nuget.config; \
2529
fi && \
2630
dotnet restore "EssentialCSharp.Web.slnx" -p:AccessToNugetFeed=$ACCESS_TO_NUGET_FEED && \
2731
dotnet build "EssentialCSharp.Web.slnx" -c Release --no-restore -p:AccessToNugetFeed=$ACCESS_TO_NUGET_FEED -p:ReleaseDateAttribute=True -p:SkipFrontendBuild=true && \
2832
dotnet publish "EssentialCSharp.Web.slnx" -c Release -p:PublishDir=/app/publish -p:UseAppHost=false -p:SkipFrontendBuild=true --no-build && \
29-
rm -f ~/.nuget/config/credentials.config
33+
if [ -f nuget.config.bak ]; then mv nuget.config.bak nuget.config; fi
3034

3135
FROM base AS final
3236
WORKDIR /app

0 commit comments

Comments
 (0)