From ee324fc88d761a5acca37fd5a81b202e7d076c4e Mon Sep 17 00:00:00 2001 From: Chema1973 <58552869+Chema1973@users.noreply.github.com> Date: Sat, 1 Mar 2025 16:35:43 +0100 Subject: [PATCH 1/2] Add or update the Azure App Service build and deployment workflow config --- .../starter-no-infra_msdocs-core-sql-555.yml | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 .github/workflows/starter-no-infra_msdocs-core-sql-555.yml diff --git a/.github/workflows/starter-no-infra_msdocs-core-sql-555.yml b/.github/workflows/starter-no-infra_msdocs-core-sql-555.yml new file mode 100644 index 000000000..c6c930a27 --- /dev/null +++ b/.github/workflows/starter-no-infra_msdocs-core-sql-555.yml @@ -0,0 +1,68 @@ +# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy +# More GitHub Actions for Azure: https://github.com/Azure/actions + +name: Build and deploy ASP.Net Core app to Azure Web App - msdocs-core-sql-555 + +on: + push: + branches: + - starter-no-infra + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read #This is required for actions/checkout + + steps: + - uses: actions/checkout@v4 + + - name: Set up .NET Core + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '8.x' + + - name: Build with dotnet + run: dotnet build --configuration Release + + - name: dotnet publish + run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp + + - name: Upload artifact for deployment job + uses: actions/upload-artifact@v4 + with: + name: .net-app + path: ${{env.DOTNET_ROOT}}/myapp + + deploy: + runs-on: ubuntu-latest + needs: build + environment: + name: 'Production' + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} + permissions: + id-token: write #This is required for requesting the JWT + contents: read #This is required for actions/checkout + + steps: + - name: Download artifact from build job + uses: actions/download-artifact@v4 + with: + name: .net-app + + - name: Login to Azure + uses: azure/login@v2 + with: + client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_ACC2C22D7BF043FB87856F02B5214AA0 }} + tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_3A6EAB9591494B9090A94F1B96DE78B4 }} + subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_7813BF81C4234C13A9D2069A914E52C4 }} + + - name: Deploy to Azure Web App + id: deploy-to-webapp + uses: azure/webapps-deploy@v3 + with: + app-name: 'msdocs-core-sql-555' + slot-name: 'Production' + package: . + \ No newline at end of file From 082b0959c3ef2aa3c4e1a40027a58dcf68af099b Mon Sep 17 00:00:00 2001 From: Chema1973 <58552869+Chema1973@users.noreply.github.com> Date: Sat, 1 Mar 2025 19:24:53 +0000 Subject: [PATCH 2/2] Configure Azure database and cache connections --- .../starter-no-infra_msdocs-core-sql-555.yml | 31 ++++++++++++------- Program.cs | 20 ++++++------ Views/Home/Index.cshtml | 2 +- Views/Home/Privacy.cshtml | 2 +- Views/Shared/Error.cshtml | 4 +-- Views/Todos/Create.cshtml | 2 +- Views/Todos/Delete.cshtml | 2 +- Views/Todos/Details.cshtml | 2 +- Views/Todos/Edit.cshtml | 2 +- 9 files changed, 37 insertions(+), 30 deletions(-) diff --git a/.github/workflows/starter-no-infra_msdocs-core-sql-555.yml b/.github/workflows/starter-no-infra_msdocs-core-sql-555.yml index c6c930a27..4ad753731 100644 --- a/.github/workflows/starter-no-infra_msdocs-core-sql-555.yml +++ b/.github/workflows/starter-no-infra_msdocs-core-sql-555.yml @@ -22,6 +22,7 @@ jobs: uses: actions/setup-dotnet@v4 with: dotnet-version: '8.x' + include-prerelease: true - name: Build with dotnet run: dotnet build --configuration Release @@ -29,6 +30,12 @@ jobs: - name: dotnet publish run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp + - name: Install dotnet ef + run: dotnet tool install --global dotnet-ef --version 8.* + + - name: Create migrations bundle + run: dotnet ef migrations bundle --runtime linux-x64 -o ${{env.DOTNET_ROOT}}/myapp/migrationsbundle + - name: Upload artifact for deployment job uses: actions/upload-artifact@v4 with: @@ -38,25 +45,25 @@ jobs: deploy: runs-on: ubuntu-latest needs: build - environment: - name: 'Production' + environment: + name: 'Production' url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} - permissions: - id-token: write #This is required for requesting the JWT - contents: read #This is required for actions/checkout + permissions: + id-token: write #This is required for requesting the JWT + contents: read #This is required for actions/checkout steps: - name: Download artifact from build job uses: actions/download-artifact@v4 with: name: .net-app - - - name: Login to Azure - uses: azure/login@v2 - with: - client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_ACC2C22D7BF043FB87856F02B5214AA0 }} - tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_3A6EAB9591494B9090A94F1B96DE78B4 }} - subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_7813BF81C4234C13A9D2069A914E52C4 }} + + - name: Login to Azure + uses: azure/login@v2 + with: + client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_ACC2C22D7BF043FB87856F02B5214AA0 }} + tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_3A6EAB9591494B9090A94F1B96DE78B4 }} + subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_7813BF81C4234C13A9D2069A914E52C4 }} - name: Deploy to Azure Web App id: deploy-to-webapp diff --git a/Program.cs b/Program.cs index ffa365722..eb38ee159 100644 --- a/Program.cs +++ b/Program.cs @@ -9,16 +9,16 @@ options.UseSqlServer(builder.Configuration.GetConnectionString("MyDbConnection"))); builder.Services.AddDistributedMemoryCache(); } -// else -// { -// builder.Services.AddDbContext(options => -// options.UseSqlServer(builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING"))); -// builder.Services.AddStackExchangeRedisCache(options => -// { -// options.Configuration = builder.Configuration["AZURE_REDIS_CONNECTIONSTRING"]; -// options.InstanceName = "SampleInstance"; -// }); -// } +else +{ + builder.Services.AddDbContext(options => + options.UseSqlServer(builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING"))); + builder.Services.AddStackExchangeRedisCache(options => + { + options.Configuration = builder.Configuration["AZURE_REDIS_CONNECTIONSTRING"]; + options.InstanceName = "SampleInstance"; + }); +} // Add services to the container. builder.Services.AddControllersWithViews(); diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index f8821f259..b13419e3f 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -3,6 +3,6 @@ }
-

Welcome

+

Welcome Chema

Learn about building Web apps with ASP.NET Core.

diff --git a/Views/Home/Privacy.cshtml b/Views/Home/Privacy.cshtml index 2479fb7d7..df450de1d 100644 --- a/Views/Home/Privacy.cshtml +++ b/Views/Home/Privacy.cshtml @@ -3,4 +3,4 @@ }

@ViewData["Title"]

-

Use this page to detail your site's privacy policy.

+

Chema, Use this page to detail your site's privacy policy.

diff --git a/Views/Shared/Error.cshtml b/Views/Shared/Error.cshtml index 4fa9d2542..b188555d1 100644 --- a/Views/Shared/Error.cshtml +++ b/Views/Shared/Error.cshtml @@ -1,10 +1,10 @@ @model ErrorViewModel @{ - ViewData["Title"] = "Error"; + ViewData["Title"] = "Error - C"; }

Error.

-

An error occurred while processing your request.

+

Chema, An error occurred while processing your request.

@if (Model.ShowRequestId) { diff --git a/Views/Todos/Create.cshtml b/Views/Todos/Create.cshtml index 02974bb44..225de342e 100644 --- a/Views/Todos/Create.cshtml +++ b/Views/Todos/Create.cshtml @@ -1,7 +1,7 @@ @model DotNetCoreSqlDb.Models.Todo @{ - ViewData["Title"] = "Create"; + ViewData["Title"] = "Create - C"; }

Create

diff --git a/Views/Todos/Delete.cshtml b/Views/Todos/Delete.cshtml index fd987454d..baa4326b1 100644 --- a/Views/Todos/Delete.cshtml +++ b/Views/Todos/Delete.cshtml @@ -1,7 +1,7 @@ @model DotNetCoreSqlDb.Models.Todo @{ - ViewData["Title"] = "Delete"; + ViewData["Title"] = "Delete - C"; }

Delete

diff --git a/Views/Todos/Details.cshtml b/Views/Todos/Details.cshtml index 58e320f0e..5201b0056 100644 --- a/Views/Todos/Details.cshtml +++ b/Views/Todos/Details.cshtml @@ -1,7 +1,7 @@ @model DotNetCoreSqlDb.Models.Todo @{ - ViewData["Title"] = "Details"; + ViewData["Title"] = "Details - C"; }

Details

diff --git a/Views/Todos/Edit.cshtml b/Views/Todos/Edit.cshtml index 46f164bfb..50d879266 100644 --- a/Views/Todos/Edit.cshtml +++ b/Views/Todos/Edit.cshtml @@ -1,7 +1,7 @@ @model DotNetCoreSqlDb.Models.Todo @{ - ViewData["Title"] = "Edit"; + ViewData["Title"] = "Edit - C"; }

Edit