diff --git a/.github/workflows/main_vhlr2.yml b/.github/workflows/main_vhlr2.yml new file mode 100644 index 000000000..c625d3d16 --- /dev/null +++ b/.github/workflows/main_vhlr2.yml @@ -0,0 +1,65 @@ +# 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 - VHLR2 + +on: + push: + branches: + - main + 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 + 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_CD9400597E8D4878A2DB8746D8CBC3A0 }} + tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_19BA0BCCDD0E4686961924AA90B3E565 }} + subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_A443A7B2260D4B948EBD9885F96883FD }} + + - name: Deploy to Azure Web App + id: deploy-to-webapp + uses: azure/webapps-deploy@v3 + with: + app-name: 'VHLR2' + slot-name: 'Production' + package: . + \ No newline at end of file diff --git a/.github/workflows/main_webappanddatabase.yml b/.github/workflows/main_webappanddatabase.yml new file mode 100644 index 000000000..82af2bf6f --- /dev/null +++ b/.github/workflows/main_webappanddatabase.yml @@ -0,0 +1,74 @@ +name: Build and deploy ASP.Net Core app to Azure Web App - WebAppAndDataBase + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + + 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 + + # --- EF CORE MIGRATIONS (Ç ÀÂÒÎÏÎØÓÊÎÌ) --- + - name: Install EF Core Tool + run: dotnet tool install -g dotnet-ef --version 7.0.14 + + - name: Create migration bundle + run: | + PROJECT_PATH=$(find . -name "*.csproj" -print -quit) + echo "Found project at: $PROJECT_PATH" + dotnet ef migrations bundle --runtime linux-x64 -p "$PROJECT_PATH" -o ${{env.DOTNET_ROOT}}/myapp/migrate + # ------------------------------------------ + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: .net-app + path: ${{env.DOTNET_ROOT}}/myapp + + deploy: + runs-on: ubuntu-latest + needs: build + permissions: + id-token: write + contents: read + + steps: + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: .net-app + + - name: Login to Azure + uses: azure/login@v2 + with: + # ÍÀÇÂÈ ÑÅÊÐÅҲ ÂÇßÒ² Ç ÂÀØÎÃÎ ÑÊÐ²ÍØÎÒÓ: + client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_CD9400597E8D4878A2DB8746D8CBC3A0 }} + tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_19BA0BCCDD0E4686961924AA90B3E565 }} + subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_A443A7B2260D4B948EBD9885F96883FD }} + + - name: Deploy to Azure Web App + id: deploy-to-webapp + uses: azure/webapps-deploy@v3 + with: + app-name: 'VHLR2' + slot-name: 'Production' + package: . + startup-command: 'dotnet DotNetCoreSqlDb.dll' \ No newline at end of file diff --git a/Program.cs b/Program.cs index cd5e3cea8..70a8fa2a1 100644 --- a/Program.cs +++ b/Program.cs @@ -2,24 +2,14 @@ using DotNetCoreSqlDb.Data; var builder = WebApplication.CreateBuilder(args); -// Add database context and cache -if(builder.Environment.IsDevelopment()) +builder.Services.AddDbContext(options => + options.UseSqlServer(builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING"))); +builder.Services.AddStackExchangeRedisCache(options => { - builder.Services.AddDbContext(options => - 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.Configuration = Environment.GetEnvironmentVariable("AZURE_REDIS_REDISCONNECTOR_CONNECTIONSTRING") + ?? builder.Configuration.GetConnectionString("AZURE_REDIS_REDISCONNECTOR_CONNECTIONSTRING"); options.InstanceName = "SampleInstance"; - }); -} - +}); // Add services to the container. builder.Services.AddControllersWithViews(); @@ -29,7 +19,13 @@ var app = builder.Build(); -// Configure the HTTP request pipeline. +using (var scope = app.Services.CreateScope()) +{ + var services = scope.ServiceProvider; + var context = services.GetRequiredService(); + context.Database.Migrate(); +} + if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); diff --git a/appsettings.json b/appsettings.json index ae3a5b52e..9e5b35def 100644 --- a/appsettings.json +++ b/appsettings.json @@ -7,9 +7,7 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - // The following connection string works for running the .NET application in the dev container. - // If running this project in Visual Studio in Windows, use the commented connection string instead. - "MyDbConnection": "Server=localhost;Database=ApplicationDB;User Id=sa;Password=P@ssw0rd;TrustServerCertificate=True;MultipleActiveResultSets=true" - //"MyDbConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabaseContext-8e1630ac-d3ae-4719-955b-5d3235f3fadf;Trusted_Connection=True;MultipleActiveResultSets=true" + "AZURE_SQL_CONNECTIONSTRING": "Data Source=webappanddatabase-server.database.windows.net,1433;Initial Catalog=webappanddatabase-database;User ID=webappanddatabase-server-admin;Password=xLspxkWrNpiqp$kY", + "AZURE_REDIS_REDISCONNECTOR_CONNECTIONSTRING": "vhlr-cache.redis.cache.windows.net:6380,password=ByFyKv1DX1yMLHrOdLp0VOelwtEmvnidkAzCaBkpzkE=,ssl=True,abortConnect=False" } } \ No newline at end of file