Skip to content

Commit 21d199a

Browse files
fix: use dotnet nuget update source to fix 401 on private NuGet feed in Docker (#1063)
Cleanup --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 621e630 commit 21d199a

2 files changed

Lines changed: 11 additions & 28 deletions

File tree

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

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

66-
- name: Generate NuGet auth config for Docker build
67-
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
82-
8366
# Build but no push with a PR
8467
- name: Docker build (no push)
8568
if: github.event_name == 'pull_request' || github.event_name == 'merge_group'
@@ -99,7 +82,7 @@ jobs:
9982
file: ./EssentialCSharp.Web/Dockerfile
10083
context: .
10184
secrets: |
102-
"id=nugetconfig,src=/tmp/nuget-auth.config"
85+
"nuget_pat=${{ secrets.AZURE_DEVOPS_PAT }}"
10386
outputs: type=docker,dest=${{ github.workspace }}/essentialcsharpwebimage.tar
10487
cache-from: type=gha
10588
cache-to: type=gha,mode=max
@@ -109,10 +92,6 @@ jobs:
10992
name: essentialcsharpwebimage
11093
path: ${{ github.workspace }}/essentialcsharpwebimage.tar
11194

112-
- name: Clean up NuGet auth config
113-
if: always()
114-
run: rm -f /tmp/nuget-auth.config
115-
11695
deploy-development:
11796
if: github.event_name != 'pull_request_target' && github.event_name != 'pull_request'
11897
runs-on: ubuntu-latest

EssentialCSharp.Web/Dockerfile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#syntax=docker/dockerfile:1.2
1+
# syntax=docker/dockerfile:1.4
22

33
FROM mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled AS base
44
WORKDIR /app
@@ -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" ] && [ ! -s /run/secrets/nuget_pat ]; then \
23+
echo "ERROR: ACCESS_TO_NUGET_FEED=true but nuget_pat secret is missing or empty" >&2; exit 1; \
24+
fi && \
25+
if [ "$ACCESS_TO_NUGET_FEED" = "true" ]; then \
26+
mkdir -p /root/.nuget/NuGet && \
27+
printf '<?xml version="1.0" encoding="utf-8"?>\n<configuration>\n <packageSourceCredentials>\n <EssentialCSharp>\n <add key="Username" value="az" />\n <add key="ClearTextPassword" value="%s" />\n </EssentialCSharp>\n </packageSourceCredentials>\n</configuration>\n' \
28+
"$(cat /run/secrets/nuget_pat)" > /root/.nuget/NuGet/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+
rm -f /root/.nuget/NuGet/NuGet.Config
3034

3135
FROM base AS final
3236
WORKDIR /app

0 commit comments

Comments
 (0)