Search code examples
c#.netazureazure-web-app-servicegithub-actions

Deploy .Net Web API to Azure App Service using GitHub Actions and include email template file


I have a .Net Web API project for which I have configured CI/CD using Github actions, it is being deployed on Azure App service. Recently, I have setup an endpoint that sends an email using a .htm template, since this template cannot be resolved in deployed API, this endpoint throws an error -
Could not find a part of the path C:\\home\\site\\wwwroot\\EmailTemplates\\OtpValidation.htm'.
I checked scm in app service and was not able to find the template. I'm not sure how can I include a particular file when deploying through GitHub Actions.

Repository - https://github.com/Jatinchhabra21/bookkeeper-api
Deployed swagger - https://bookkeeper.azurewebsites.net/swagger/index.html
API endpoint - /api/users/new/otp


Solution

  • Thank you @Thiago Custodio for your comments.

    In your .csproj file add the below code to copy the EmailTemplates folder to output directory always.

    .csproj file:-

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
        <PackageReference Include="Azure.Identity" Version="1.10.2" />
        <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.24" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.22" />
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
        <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
      </ItemGroup>
        <ItemGroup>
            <Content Include="EmailTemplates\OtpValidation.htm">
                <CopyToOutputDirectory>Always</CopyToOutputDirectory>
            </Content>
        </ItemGroup>
    
        <PropertyGroup>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
        <NoWarn>$(NoWarn);1591</NoWarn>
      </PropertyGroup>
    
    
      <ItemGroup>
        <Folder Include="Utility\" />
      </ItemGroup>
    
    </Project>
    

    Also, In your Program.cs make sure you enable static serving by adding > app.UseStaticFiles option like below:-

    I created one Asp.NET Core API with .Net 7.0 runtime and created one folder with EmailTemplates by adding OtpValidation.htm and your code inside it, Then I edited my Program.cs to serve the OtpValidation.htm like below:-

    My Folder:-

    enter image description here

    My Program.cs:-

    using Microsoft.Extensions.FileProviders;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddControllers();
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }
    
    app.UseHttpsRedirection();
    
    // Configure static file serving from the EmailTemplates folder
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "EmailTemplates")), // Adjust the path as needed
        RequestPath = "/EmailTemplates" // Virtual path to access files
    });
    
    app.UseAuthorization();
    app.MapControllers();
    
    app.Run();
    

    Local Output:-

    enter image description here

    I added this code to my Github Repository and used github action workflow below that build and Deploys the entire dotnet api in Azure Web app:-

    Open your solution in the Visual Studio and Publish it via Github Actions Workflow CI/CD like below:-

    enter image description here

    Github Action workflow got created and ran successfully like below:-

    enter image description here