From a37a49830c180ceab3a32dd7a6958d639e44fee3 Mon Sep 17 00:00:00 2001 From: Calin Lupas Date: Mon, 21 Apr 2025 09:18:10 -0400 Subject: [PATCH 1/2] Add Docker support and update CI/CD workflows - Introduced Dockerfile for building the web application. - Added .dockerignore to exclude unnecessary files from Docker context. - Updated CI workflow to build and push Docker images to Azure Container Registry. - Enhanced CI/CD workflow to deploy Docker images to Azure Web App. - Modified launchSettings.json to support Docker configuration. - Updated webapp01.csproj to include Docker-related properties and dependencies. --- .github/workflows/ci.yml | 5 +++- .github/workflows/cicd.yml | 14 ++++++++++ src/webapp01/.dockerignore | 30 +++++++++++++++++++++ src/webapp01/Dockerfile | 30 +++++++++++++++++++++ src/webapp01/Properties/launchSettings.json | 29 +++++++++++++------- src/webapp01/webapp01.csproj | 4 +++ 6 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 src/webapp01/.dockerignore create mode 100644 src/webapp01/Dockerfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aa0c32c..aa83995 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,8 @@ env: SRC_PROJECT_PATH: '/webapp01/webapp01.csproj' AZURE_WEBAPP_PACKAGE_PATH: './src' # set this to the path to your web app project, defaults to the repository root DOTNET_VERSION: '9.0.x' # set this to the dot net version to use + imageName: "webapp01" + tag: ${{ github.sha }} jobs: ci_build: @@ -36,4 +38,5 @@ jobs: dotnet restore ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}${{ env.SRC_PROJECT_PATH }} dotnet build --configuration Release ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}${{ env.SRC_PROJECT_PATH }} - \ No newline at end of file + - name: Build the Docker image + run: docker build . --file Dockerfile --tag ${{ env.imageName }}:${{ env.tag }} diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 6b6bba3..2e2d319 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -52,6 +52,20 @@ jobs: app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/webapp01/bin/publish' + - uses: azure/docker-login@v1 + with: + login-server: crdevsecopscldev.azurecr.io + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + - run: | + docker build . -t crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }} + docker push crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }} + + - uses: azure/webapps-deploy@v2 + with: + app-name: ${{ env.AZURE_WEBAPP_NAME }} + images: 'crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }}' + - name: logout run: | az logout \ No newline at end of file diff --git a/src/webapp01/.dockerignore b/src/webapp01/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/src/webapp01/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/src/webapp01/Dockerfile b/src/webapp01/Dockerfile new file mode 100644 index 0000000..8f360a1 --- /dev/null +++ b/src/webapp01/Dockerfile @@ -0,0 +1,30 @@ +# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +# This stage is used when running from VS in fast mode (Default for Debug configuration) +FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base +USER $APP_UID +WORKDIR /app +EXPOSE 8080 +EXPOSE 8081 + + +# This stage is used to build the service project +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["webapp01.csproj", "."] +RUN dotnet restore "./webapp01.csproj" +COPY . . +WORKDIR "/src/." +RUN dotnet build "./webapp01.csproj" -c $BUILD_CONFIGURATION -o /app/build + +# This stage is used to publish the service project to be copied to the final stage +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./webapp01.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration) +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "webapp01.dll"] \ No newline at end of file diff --git a/src/webapp01/Properties/launchSettings.json b/src/webapp01/Properties/launchSettings.json index f24c911..2530cee 100644 --- a/src/webapp01/Properties/launchSettings.json +++ b/src/webapp01/Properties/launchSettings.json @@ -1,23 +1,34 @@ { - "$schema": "https://json.schemastore.org/launchsettings.json", "profiles": { "http": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5075", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5075" }, "https": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7058;http://localhost:5075", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7058;http://localhost:5075" + }, + "Container (Dockerfile)": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "environmentVariables": { + "ASPNETCORE_HTTPS_PORTS": "8081", + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true, + "useSSL": true } - } -} + }, + "$schema": "https://json.schemastore.org/launchsettings.json" +} \ No newline at end of file diff --git a/src/webapp01/webapp01.csproj b/src/webapp01/webapp01.csproj index 1acb179..54dfb41 100644 --- a/src/webapp01/webapp01.csproj +++ b/src/webapp01/webapp01.csproj @@ -4,11 +4,15 @@ net9.0 enable enable + 7f0355f0-e3cb-4a1e-bf2d-0431db9b93f8 + Linux + . + From 8b4d332b0293f229a87e9fe99b2dcae60b0c6db4 Mon Sep 17 00:00:00 2001 From: Calin Lupas Date: Mon, 21 Apr 2025 09:20:32 -0400 Subject: [PATCH 2/2] Fix Docker build context and file path in CI/CD workflows --- .github/workflows/ci.yml | 2 +- .github/workflows/cicd.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aa83995..9f07fd7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,4 +39,4 @@ jobs: dotnet build --configuration Release ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}${{ env.SRC_PROJECT_PATH }} - name: Build the Docker image - run: docker build . --file Dockerfile --tag ${{ env.imageName }}:${{ env.tag }} + run: docker build ./src/webapp01 --file ./src/webapp01/Dockerfile --tag ${{ env.imageName }}:${{ env.tag }} diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 2e2d319..3cee7b1 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -58,7 +58,7 @@ jobs: username: ${{ secrets.REGISTRY_USERNAME }} password: ${{ secrets.REGISTRY_PASSWORD }} - run: | - docker build . -t crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }} + docker build ./src/webapp01 --file ./src/webapp01/Dockerfile -t crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }} docker push crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }} - uses: azure/webapps-deploy@v2