Skip to content

Commit

Permalink
Support installation of SQL Express 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
andyundso committed Jul 27, 2024
1 parent f53591f commit 7095a38
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ jobs:
- name: Run Action
uses: ./action
with:
components: sqlcmd
components: sqlcmd,sqlengine
sa-password: "bHuZH81%cGC6"

- name: Run tests
run: |
action/test.ps1
shell: pwsh
env:
SA_PASSWORD: "bHuZH81%cGC6"
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ inputs:
components:
description: "The components to install"
required: true
sa-password:
description: "The SA password for the SQL instance"
required: true
runs:
using: "composite"
steps:
- name: Set up Docker on macOS
uses: douglascamata/setup-docker-macos-action@v1-alpha
if: runner.os == 'macOS' && contains(inputs.components, 'sqlengine')
- shell: pwsh
run: |
$params = @{
Components = ("${{ inputs.components }}" -split ",").Trim()
SaPassword = "${{ inputs.sa-password }}"
}
${{ github.action_path }}/install.ps1 @params
env:
HOMEBREW_NO_AUTO_UPDATE: "1"
62 changes: 60 additions & 2 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
param (
[ValidateSet("sqlcmd")]
[string[]]$Components
[ValidateSet("sqlcmd", "sqlengine")]
[string[]]$Components,
[string]$SaPassword
)

function Wait-ForContainer {
$checkInterval = 5
$containerName = "sql"
$timeout = 120

$startTime = Get-Date
Write-Host "Waiting for the container '$containerName' to be healthy..."

while ($true) {
# Get the container's health status
$healthStatus = (docker inspect --format='{{.State.Health.Status}}' $containerName) 2>&1

if ($healthStatus -eq "healthy") {
Write-Host "Container '$containerName' is healthy."
break
}
elseif ((Get-Date) -gt $startTime.AddSeconds($timeout)) {
Write-Host "Timed out waiting for container '$containerName' to be healthy."
& docker logs sql
exit 1
}

# Wait for the check interval before checking again
Start-Sleep -Seconds $checkInterval
}
}

$Version = "2017"
$DockerRunSQLServer = "docker run --name=`"sql`" -e `"ACCEPT_EULA=Y`"-e `"SA_PASSWORD=$SaPassword`" -e `"MSSQL_PID=Express`" --health-cmd=`"/opt/mssql-tools/bin/sqlcmd -C -S localhost -U sa -P '$SaPassword' -Q 'SELECT 1' -b -o /dev/null`" --health-start-period=`"10s`" --health-retries=3 --health-interval=`"10s`" -p 1433:1433 -d `"mcr.microsoft.com/mssql/server:$Version-latest`""

if ("sqlengine" -in $Components) {
if ($IsLinux || $IsMacOS) {
Write-Output "Starting a Docker Container"
Invoke-Expression $DockerRunSQLServer
Wait-ForContainer
}

if ($IsWindows) {
Write-Output "Downloading and installing SQL Server"
New-Item -ItemType Directory -Path "C:\Downloads"

Invoke-WebRequest "https://download.microsoft.com/download/E/F/2/EF23C21D-7860-4F05-88CE-39AA114B014B/SQLEXPR_x64_ENU.exe" -OutFile "C:\Downloads\mssql.exe"
Start-Process -Wait -FilePath "C:\Downloads\mssql.exe" -ArgumentList /qs, /x:"C:\Downloads\setup"
C:\Downloads\setup\setup.exe /q /ACTION=Install /INSTANCENAME=SQLEXPRESS /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT='NT AUTHORITY\System' /SQLSYSADMINACCOUNTS='BUILTIN\ADMINISTRATORS' /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS

Write-Host "Configuring SQL Express ..."
stop-service MSSQL`$SQLEXPRESS
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value ''
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2

Write-Host "Starting SQL Express ..."
start-service MSSQL`$SQLEXPRESS
& sqlcmd -Q "ALTER LOGIN sa with password='$SaPassword'; ALTER LOGIN sa ENABLE;"
}
}

if ("sqlcmd" -in $Components) {
if ($IsMacOS) {
Write-Output "Installing sqlcmd tools"
Expand Down
3 changes: 3 additions & 0 deletions test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ if (Get-Command sqlcmd -ErrorAction SilentlyContinue) {
Write-Output "sqlcmd command does not exist."
exit 1
}

Write-Output "Checking if SQL Server is available ..."
& sqlcmd -S 127.0.0.1 -U sa -P $env:SA_PASSWORD -Q "SELECT 1"

0 comments on commit 7095a38

Please sign in to comment.