Skip to content

Commit

Permalink
With prod dockerfile and workflow to generate the image
Browse files Browse the repository at this point in the history
  • Loading branch information
MertzAndreas committed Dec 9, 2024
1 parent 965f658 commit 54dbef9
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 302 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/build-and-push-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Publish Docker image prod

on:
pull_request:
branches:
- main # Trigger on PR to main branch
# You can still keep `workflow_dispatch` here for manual triggers later, if you need
# workflow_dispatch:

jobs:
push_to_registries:
name: Push Docker image to ghcr.io
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
attestations: write
id-token: write

steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/[email protected]
with:
install: true
driver-opts: network=host
platforms: linux/amd64,linux/arm64

- name: Log in to the Container registry
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{version}}
type=raw,value=latest
- name: Build and push Docker images
id: push
uses: docker/[email protected]
with:
context: .
file: Dockerfile.prod # Specify the production Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository }}:${{ steps.meta.outputs.tags }} # Version tags (e.g., v1.0.0)
ghcr.io/${{ github.repository }}:prod # Prod tag
labels: |
version=${{ steps.meta.outputs.tags }}
environment=production # Add production-related labels
platforms: linux/amd64,linux/arm64
101 changes: 52 additions & 49 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
# This workflow will build a Docker image and push it to GitHub Container Registry

name: Publish Docker image
name: Publish Docker image prod

on:
pull_request:
branches:
- main
push:
tags:
- "v*"
pull_request:
branches:
- main # Trigger on PR to main branch
# You can still keep `workflow_dispatch` here for manual triggers later, if you need
# workflow_dispatch:

jobs:
push_to_registries:
name: Push Docker image to ghcr.io
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
attestations: write
id-token: write
steps:
- name: Check out the repo
uses: actions/checkout@v4
push_to_registries:
name: Push Docker image to ghcr.io
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
attestations: write
id-token: write

steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/[email protected]
with:
install: true
driver-opts: network=host
platforms: linux/amd64,linux/arm64
- name: Set up Docker Buildx
uses: docker/[email protected]
with:
install: true
driver-opts: network=host
platforms: linux/amd64,linux/arm64

- name: Log in to the Container registry
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to the Container registry
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{version}}
type=raw,value=latest
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{version}}
type=raw,value=latest
- name: Build and push Docker images
id: push
uses: docker/[email protected]
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
- name: Build and push Docker images
id: push
uses: docker/[email protected]
with:
context: .
file: Dockerfile.prod # Specify the production Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository }}:${{ steps.meta.outputs.tags }} # Version tags (e.g., v1.0.0)
ghcr.io/${{ github.repository }}:prod # Prod tag
labels: |
version=${{ steps.meta.outputs.tags }}
environment=production # Add production-related labels
platforms: linux/amd64,linux/arm64
75 changes: 38 additions & 37 deletions .idea/.idea.weekplanner-api/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Stage 1: Build
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG TARGETARCH
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Production

# Copy only the required project files
COPY GirafAPI/*.csproj ./GirafAPI/
COPY GirafAPI/Data/Migrations/*.cs ./GirafAPI/Data/Migrations/
RUN dotnet restore ./GirafAPI/GirafAPI.csproj

# Build and publish the application
COPY . .
RUN dotnet build ./GirafAPI/GirafAPI.csproj -c Release -o /app/build -a $TARGETARCH
RUN dotnet publish ./GirafAPI/GirafAPI.csproj -c Release -o /app/publish -a $TARGETARCH --no-restore

# Stage 2: Runtime
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app

# Copy the published app from the build stage
COPY --from=build /app/publish .

# Expose the application port
EXPOSE 5171
ENV ASPNETCORE_URLS=http://+:5171

# Set the entry point
ENTRYPOINT ["dotnet", "/app/GirafAPI.dll"]
Loading

0 comments on commit 54dbef9

Please sign in to comment.