Canarys | IT Services

Blogs

Database deployment using GitHub Actions

Share

In this blog, I will be showing how to export the database file from SQL Server and import the same in another SQL server. When we do the export, we will be having 2 different file formats i.e DACPAC and BACPAC. The major difference between is DACPAC copies only schema whereas BACPAC copies both schema and data.

Export the bacpac file from sql server using SSMS
1.    Connect to SQL server using SSMS by providing the credentialsn
2.    Right Click on database -> Task -> Export data tier application
3.    Save the bacpac at required location.
11

At this point we have extracted the schema along with data from the database and stored in bacpac file.

Note: If we select Extract data tier application from the task, we will only be getting dacpac file i.e. we will only be getting schema of the database.

Next, we will push our backup file stored in GitHub along with the source code of the respective application in GitHub.

Before restoring the database, we need to have sqlpackage installed on the machine from where we will be initiating the import task, once the package is installed, we can proceed with the restoration part. 

What is SQLPACKAGE??

SQLPACKAGE.EXE is a command line utility that can be helpful for automations of various database deployment task. Mainly it can be used to export and import Data-tier Applications.

For more information on sql package checkout the below reference:

https://docs.microsoft.com/en-us/sql/tools/sqlpackage

When using the above utility, you must be using various options along with it. Above link can be helpful in using the utility and it also provides few examples which can be handy while using the package. Installing the sqlpackage can be done manually (for self-hosted runners) using this link or else we can do the same using the setup-sqlpackage action from the GitHub marketplace.

Below is the sample snippet for installing the sqlpackage using action from marketplace and command/action to import the database on the target sql server.

jobs:
  build:
    runs-on: windows-latest
    steps:
    - name: Checkout the source code
      uses: actions/checkout@v2

    - name: setup-sqlpackage
      uses: coreywebber/setup-sqlpackage@v1.0.0

    - name: Restoring the database
      run: sqlpackage /Action:Import /sf:${{github.workspace}}partsnew.bacpac /tsn:${{secrets.DB_SERVER}} /tdn:${{env.database}} /tu:${{env.db_user}} /tp:${{ secrets.db_password }} /p:DatabaseMaximumSize=2 /p:DatabaseEdition=Basic

In the above snippet, I am executing all my tasks on windows runner machine (GitHub runner). Then in the tasks section, we need to check out the code from the repository using checkout action and then we will be installing the sqlpacakage utility on the same machine. Next, step will be the important step where we need to prepare the command to restore the database. I have prepared a command based on my requirement by providing the required options. Here, various options for the command have been passed as a GitHub action variable (using secrets) and some variables as environment variables in my workflow file (hidden in above snippet) and values for the variable are provided in respective sections.

Once everything is setup, we can trigger the action.

Special note: When importing to Azure SQL Server you will need to validate the database configuration that will be used with the utility (sqlpackage). Most important are the max size of the database which will define the size and the edition.

 For example, using /p:DatabaseMaximumSize=150 will result in a premium SKU edition with a database size of 150 GB. Please do not forget this because Azure SQL DB can be costly.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.