Skip to content

Commit d2e70fa

Browse files
committed
Refactor container build process to use Docker Buildx and add Dockerfile for multi-stage build
1 parent 0d7725f commit d2e70fa

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

.github/workflows/container-build.yml

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,17 @@ jobs:
4343
type=semver,pattern={{version}}
4444
type=semver,pattern={{major}}.{{minor}}
4545
type=semver,pattern={{major}}
46-
type=sha
47-
46+
type=sha
47+
48+
- name: Set up Docker Buildx
49+
uses: docker/setup-buildx-action@v3
50+
4851
- name: Build and push container
49-
run: |
50-
# Create tag arguments for Docker
51-
TAGS=""
52-
while IFS= read -r tag; do
53-
TAGS="$TAGS -t $tag"
54-
done <<< "${{ steps.meta.outputs.tags }}"
55-
56-
dotnet publish Web/Web.csproj \
57-
--os linux \
58-
--arch x64 \
59-
-c Release \
60-
--self-contained \
61-
/p:PublishProfile=DefaultContainer \
62-
/p:ContainerRepository=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} \
63-
/p:ContainerBuildArgs="$TAGS"
52+
uses: docker/build-push-action@v5
53+
with:
54+
context: .
55+
push: true
56+
tags: ${{ steps.meta.outputs.tags }}
57+
labels: ${{ steps.meta.outputs.labels }}
58+
cache-from: type=gha
59+
cache-to: type=gha,mode=max

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Build stage
2+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
3+
WORKDIR /src
4+
5+
# Copy csproj and restore dependencies
6+
COPY ["Web/Web.csproj", "Web/"]
7+
RUN dotnet restore "Web/Web.csproj"
8+
9+
# Copy the rest of the source code
10+
COPY . .
11+
12+
# Build and publish
13+
RUN dotnet publish "Web/Web.csproj" -c Release -o /app/publish /p:UseAppHost=false
14+
15+
# Runtime stage
16+
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
17+
WORKDIR /app
18+
EXPOSE 80
19+
EXPOSE 443
20+
21+
# Copy from build stage
22+
COPY --from=build /app/publish .
23+
ENTRYPOINT ["dotnet", "Web.dll"]

0 commit comments

Comments
 (0)