This post is about enabling continuous database deployment with GitHub Actions for your ASP.NET Core applications with the help of EF Core migrations. I am following the EF Core migration script approach in this as well. But compared to Azure DevOps, I found GitHub actions is more fast. I am deploying an ASP.NET Core 5.0 application to Azure App Service and along with it I am deploying the database changes as well.
While creating App Service, I configured Continuous deployment and I selected my GitHub repository. This will help you to initialize the GitHub repo with a Azure App Service deployment Action workflow file.
You can preview the Github Action file, by clicking on Preview button. Next you can continue creating your app service.
Next in the SQL Server, select the Firewalls and virtual networks option and select Allow Azure services and resources to access this server and set it to True.
Now you’re ready with your configuration changes, let go to your Github repository and select the yaml file under .github directory. By default you will be able to see a file with following content.
For running EF Commands you need to first install EF Tools. Instead of installing the EF Tools globally, I am installing it as a local tool. To do that I am adding a run command which install the dotnet ef tool.
Next I am generating the SQL Script using dotnet ef command, I am adding a run command again with the following code.
Since you’re already building the app, you can include --no-build parameter, so that you can save the build time.
Next I need to add an action to deploy the generated script to Azure SQL DB. Before that I am creating a GitHub secret for the SQL server connection string. You can do it from Settings > Secrets, and add a new repository secret.
Next in the marketplace, search for SQL. And select the Azure SQL Deploy action.
In the configuration, you need provide the following details.
Modify it like this.
Now the configuration is completed for Deploy Database with GitHub Actions. Next you can commit some changes which will deploy to the SQL Server.
Here is the complete script.
And that is it! Now you can relax and commit your code and migrations as the deployment process is carried out automatically every time you push your code to your repository. Continuous Delivery is a great tool on its own, but be sure to use it together Continuous Integration to unlock its full potential.