Skip to main content

Running SQL Server Init Scripts in GitHub Actions

Question

How can I run init scripts on SQL Server when using GitHub Actions services instead of the Atlas docker blocks?

Answer

If you're using the init attribute in the docker block in your Atlas HCL config to run the init script when starting the dev database (see below):

docker "sqlserver" "devdb" {
image = "mcr.microsoft.com/mssql/server:2019-latest"
mode = "database"
database = "master"
init = file("./init.sql")
}
env {
name = atlas.env
dev = docker.sqlserver.devdb.url
}

But you start the dev database using a service in the GitHub action, you can still execute your init script using the following configuration:

name: CI - Verify schema migration

on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
validate:
runs-on: ubuntu-latest
env:
MSSQL_SA_PASSWORD: 'P@ssw0rd0995'
services:
sqlserver-2019:
image: mcr.microsoft.com/mssql/server:2019-latest
env:
ACCEPT_EULA: Y
MSSQL_PID: Developer
MSSQL_SA_PASSWORD: ${{ env.MSSQL_SA_PASSWORD }}
ports:
- 1433:1433
options: >-
--health-cmd "/opt/mssql-tools18/bin/sqlcmd -C -U sa -P \"${MSSQL_SA_PASSWORD}\" -Q \"SELECT 1\""
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: run init.sql on the SQL Server
run: |
cat ./init.sql | docker exec -i $(docker ps -qa -f 'publish=1433') bash -c '/opt/mssql-tools18/bin/sqlcmd -C -U sa -P $MSSQL_SA_PASSWORD -i /dev/stdin'
- name: atlas migrate lint
run: |
atlas migrate lint --dev-url='sqlserver://sa:${{ env.MSSQL_SA_PASSWORD }}@localhost:1433?database=master'