Code Monkey home page Code Monkey logo

azure-sql-db-ci-cd's Introduction

page_type languages products description urlFragment
sample
tsql
sql
csharp
azure
azure-sql-database
dotnet
Azure SQL Database CI/CD Pipeline with GitHub Actions
azure-sql-db-ci-cd

Azure SQL Database CI/CD Pipeline with GitHub Actions

License

This sample shows how to use GitHub Actions to create a CI/CD pipeline using DbUp, by sequentially applying .sql scripts to an existing database.

This approach is different than the one provided by usage of SqlPackage + .BacPac or by Database Migrations (by .NET EF Core or Python Django for example).

To read more about this, look here: Philosophy Behind DbUp.

Create a new Azure SQL database

Create a new empty Azure SQL database (if you need help: Deploy Azure SQL Database ). Make sure you allow Azure Services to access the created database, as described here: Allow Azure services

Create a user used for running deployment and tests

Create a user that has enough rights to execute all the needed statements used to deploy the database. For example

CREATE USER [github_action_user] WITH PASSWORD = 'S0meVery_Very+Str0ngPazzworD!';
ALTER ROLE db_owner ADD MEMBER [github_action_user];

Please note that db_owner membership is required for this specific samples as authority to ALTER DATABASE is needed. If you just need to create, alter and drop objects, the following permission would be more than enough, without the need to be db_owner, which is a very high-privileged account.

ALTER ROLE db_ddladmin ADD MEMBER [github_action_user];
ALTER ROLE db_datareader ADD MEMBER [github_action_user];
ALTER ROLE db_datawriter ADD MEMBER [github_action_user];

Get Azure SQL Connection string

Get the ADO.NET connection string to that database you just created, either via the Portal or AZ CLI or Powershell.

The connection will be something like:

Server=tcp:<myserver>.database.windows.net,1433;Database=github_action_user;User ID=github_action_user;Password=S0meVery_Very+Str0ngPazzworD!;Encrypt=true;Connection Timeout=30;

Create a fork of this GitHub repo

Create a fork of this repository.

Once done, in Settings/Secrets create a secret named AZURE_SQL_CONNECTION_STRING and store the connection string of the Azure SQL database you created in the previous step.

In the forked repository go to the "Actions" tab and enable GitHub Actions.

Now push your repository. As an example you can make a small change to the README right on GitHub, or add a new - even empty - file. Commit and push the change. The GitHub Action will start.

GitHub Action

The GitHub Action defined in .github folder will kick in, starting a two-step process to deploy and test database using DbUp and NUnit. Deployment is done via the application in the db-deploy folder, while the tests are in the db-test folder.

Monitor the GitHub action. If everything worked you will see the deployment done correctly, but the tests failing.

This is expected as there is an error in the deployed stored procedure.

Release a fix

The stored procedure has a little bug, and in fact the test is failing. A new procedure with a fix is available in the 04-fixed-stored-procedure.sql.fix files in the db-deploy/sql folder. Remove the .fix extension so that the new extension will be just sql and push this change to the repo.

GitHub Action will start again, deploying only the new script and running the test again.

The test will now both succeed. Well done!

Debugging Deployment and Testing locally

You can also run deployment and test locally, if you want or need to debug them. To do create a .env file starting from the .env.template file provided in each folder, and put the Azure SQL connection string, or a connection string to a local SQL Server database if you want to debug everything on-premises.

Then debug the programs a usual using Visual Studio or Visual Studio Code.

Next Steps

If you want a more complex scenario, you can apply what you have learned to the following sample, forking it into new repository and adding the GitHub action for a complete CI/CD deployment pipeline

https://github.com/Azure-Samples/azure-sql-db-sync-api-change-tracking

azure-sql-db-ci-cd's People

Contributors

microsoft-github-operations[bot] avatar microsoftopensource avatar mtrilbybassett avatar yorek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.