|
| 1 | +name: Build-Database-Run-tSQLt-Tests |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - feature/* |
| 8 | + - cicd/* |
| 9 | + |
| 10 | +jobs: |
| 11 | + create-build-deploy-test-tSQLcron: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + # Use an Environment Variable to store the SQL SA_Password |
| 15 | + # this ENV variable can be referenced in the Various, step |
| 16 | + |
| 17 | + env: |
| 18 | + SA_PASSWORD: P@ssw0rd! # A secret is not necessary here as this is only a BuildTime SQL instance |
| 19 | + |
| 20 | + # Create a Service Named db running SQL Server Container running on 14333 |
| 21 | + services: |
| 22 | + db: |
| 23 | + image: mcr.microsoft.com/mssql/server |
| 24 | + env: |
| 25 | + SA_PASSWORD: ${{ env.SA_PASSWORD }} |
| 26 | + ACCEPT_EULA: Y |
| 27 | + ports: |
| 28 | + - '14333:1433' |
| 29 | + |
| 30 | + # Steps to Build/Deploy tsql code |
| 31 | + steps: |
| 32 | + |
| 33 | + - uses: actions/checkout@v2 |
| 34 | + name: Checkout Source Code |
| 35 | + |
| 36 | + # Using aletasystems/tsqlrunner to Initialize the SQL Server |
| 37 | + # - Create a Database |
| 38 | + # - Along with other scripts located in .\tsqlrunner.initscripts |
| 39 | + - shell: pwsh |
| 40 | + name: Initialize the SQL Database using aletasystems/tsqlrunner |
| 41 | + run: | |
| 42 | + # Get the DockerNetwork of the db service |
| 43 | + $dockerContainer = (docker ps -a --format '{{ json . }}' | ConvertFrom-Json | %{ $_ } | Where-Object -Property Image -EQ 'mcr.microsoft.com/mssql/server' | Select-Object networks).Networks |
| 44 | +
|
| 45 | + # Attach and Execute aletasystems/tsqlrunner |
| 46 | + docker run --network=$dockerContainer --rm --name tsqlrunner -e SQLCMDSERVER=db -e SQLCMDUSER=sa -e SQLCMDPASSWORD=$env:SA_PASSWORD -v $env:GITHUB_WORKSPACE/tsqlrunner.initscripts:/tsqlscripts aletasystems/tsqlrunner /tooling/execute-sql-scripts.sh |
| 47 | + |
| 48 | + # Deploy SQL code using Flyway |
| 49 | + # - We attach the flyway container to the SQL Server Network |
| 50 | + # - Configure the SQL and Config Paths |
| 51 | + - shell: pwsh |
| 52 | + name: Build-Deploy tSQL using flyway/flyway |
| 53 | + run: | |
| 54 | + # Get the DockerNetwork of the db service |
| 55 | + $dockerContainer = (docker ps -a --format '{{ json . }}' | ConvertFrom-Json | %{ $_ } | Where-Object -Property Image -EQ 'mcr.microsoft.com/mssql/server' | Select-Object networks).Networks |
| 56 | +
|
| 57 | + # Execute Flyway Build/Deploy |
| 58 | + docker run --network=$dockerContainer --rm --name flyway -v $env:GITHUB_WORKSPACE/flyway.tSQLcron/sql:/flyway/sql -v $env:GITHUB_WORKSPACE/flyway.tSQLcron/conf:/flyway/conf flyway/flyway -user=sa -password="$($env:SA_PASSWORD)" -url="jdbc:sqlserver://db;databaseName=tSQLcron" -connectRetries=60 migrate |
| 59 | +
|
| 60 | + # Install SQL Server PS Module |
| 61 | + # - Set PSGallery to Trusted so Install-Module runs fine |
| 62 | + - shell: pwsh |
| 63 | + name: Install and Import SqlServer PS1 Module |
| 64 | + run : | |
| 65 | + # We need to trust PSGallery or Install-Module will ask for Trust |
| 66 | + Get-PSRepository | Set-PSRepository -InstallationPolicy Trusted |
| 67 | +
|
| 68 | + # Install SqlServer PS Module if it doesn't exist |
| 69 | + if (!(Get-Module -Name SqlServer -ListAvailable)) { |
| 70 | + $null = Install-Module -Name SqlServer -Scope CurrentUser -AllowClobber |
| 71 | + } |
| 72 | +
|
| 73 | + # Import the Module |
| 74 | + $null = Import-Module -Name SqlServer |
| 75 | +
|
| 76 | + # Execute the Utility Script that runs tSQLt |
| 77 | + # - Runs tsqlt.Runall but ignores failures |
| 78 | + # - Formats the result of the test cases to console and EXITS with the correct exit code. |
| 79 | + - shell: pwsh |
| 80 | + name: Execute tSQLt Tests |
| 81 | + run: | |
| 82 | + .\Utility\invoke-tSQLt.ps1 |
0 commit comments