Skip to content

Commit

Permalink
adding ci store script
Browse files Browse the repository at this point in the history
  • Loading branch information
jiri-malec committed Oct 22, 2024
1 parent db0c0fb commit 93891af
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 11 deletions.
23 changes: 12 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
PROJECT_NAME: DancingGoat
ASPNETCORE_URLS: https://localhost:14070
STATUS_CHECK_URL: https://localhost:14070/status
DATABASE_USER: "sa"
DATABASE_PASSWORD: "Pass@12345"
DATABASE_NAME: "XByK_DancingGoat_Zapier"

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -77,26 +80,24 @@ jobs:
uses: potatoqualitee/mssqlsuite@9a0136e208df60b8ecb62909f076bc34854fa55a # set as a commit hash for security - v1.7
with:
install: sqlpackage, sqlengine
# TODO: Heslo v otevřené podobě?
sa-password: Pass@12345
sa-password: ${{ env.DATABASE_PASSWORD }}
version: 2022

- name: Restore Database .bak
# TODO: Heslo v otevřené podobě?
run: |
docker exec sql mkdir /var/opt/mssql/backup
docker cp "./database/${{ env.DATABASE_BACKUP_FILENAME }}" sql:/var/opt/mssql/backup
sqlcmd `
-S localhost `
-d master `
-U "sa" `
-P "Pass@12345" `
-U ${{ env.DATABASE_USER }} `
-P ${{ env.DATABASE_PASSWORD }} `
-Q "RESTORE DATABASE [XByK_DancingGoat_Zapier] FROM DISK='/var/opt/mssql/backup/${{ env.DATABASE_BACKUP_FILENAME }}' WITH MOVE 'XByK_DancingGoat_Zapier' TO '/var/opt/mssql/data/XByK_DancingGoat_Zapier.mdf', MOVE 'XByK_DancingGoat_Zapier_log' TO '/var/opt/mssql/data/XByK_DancingGoat_Zapier_log.ldf'"
# - name: Restore CI Repository TODO
# working-directory: "./scripts"
# run: |
# ./Restore-CI.ps1
- name: Restore CI Repository
working-directory: "./scripts"
run: |
./Restore-CI.ps1
- name: Publish Application
run: |
Expand Down Expand Up @@ -132,11 +133,11 @@ jobs:
cd ../
# The ASP.NET Core app can take a few seconds to start, so we delay running tests
# until it is ready, and fail if we go over a maximum wait time
$limit = 15
$limit = 10
$attempts = 0
$success = $false
while ($attempts -lt $limit -and -not $success) {
Start-Sleep -Seconds 5
Start-Sleep -Seconds 1
try {
$response = Invoke-WebRequest -Uri ${{ env.STATUS_CHECK_URL }} -Method Get -SkipCertificateCheck
if ($response.StatusCode -eq 200) {
Expand Down
55 changes: 55 additions & 0 deletions scripts/Restore-CI.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Import-Module (Resolve-Path Utilities) `
-Function `
Get-WebProjectPath, `
Invoke-ExpressionWithException, `
Write-Status `
-Force

$projectPath = Get-WebProjectPath
$repositoryPath = Join-Path $projectPath "App_Data/CIRepository"
$launchProfile = $Env:ASPNETCORE_ENVIRONMENT -eq "CI" ? "Zapier.WebCI" : "DancingGoat"
$configuration = $Env:ASPNETCORE_ENVIRONMENT -eq "CI" ? "Release" : "Debug"
$dbName = $Env:DATABASE_NAME
$dbUser = $Env:DATABASE_USER
$dbPassword = $Env:DATABASE_PASSWORD

$turnOffCI = "sqlcmd " + `
"-S localhost " + `
"-d $dbName " + `
"-U $dbUser " + `
"-P $dbPassword " + `
"-Q `"UPDATE CMS_SettingsKey SET KeyValue = N'False' WHERE KeyName = N'CMSEnableCI'`""

$turnOnCI = "sqlcmd " + `
"-S localhost " + `
"-d $dbName " + `
"-U $dbUser " + `
"-P $dbPassword " + `
"-Q `"UPDATE CMS_SettingsKey SET KeyValue = N'True' WHERE KeyName = N'CMSEnableCI'`""

$updateCommand = "dotnet run " + `
"--launch-profile $launchProfile " + `
"-c $configuration " + `
"--no-build " + `
"--project $projectPath " + `
"--kxp-update " + `
"--skip-confirmation"

$restoreCommand = "dotnet run " + `
"--launch-profile $launchProfile " + `
"-c $configuration " + `
"--no-build " + `
"--no-restore " + `
"--project $projectPath " + `
"--kxp-ci-restore"
Invoke-ExpressionWithException $restoreCommand
Invoke-ExpressionWithException $turnOffCI
Invoke-ExpressionWithException $updateCommand
Invoke-ExpressionWithException $turnOnCI
# Invoke-ExpressionWithException $storeCommand



Write-Host "`n"
Write-Status 'CI files processed'
Write-Host "`n"
139 changes: 139 additions & 0 deletions scripts/Utilities/Utilities.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Utilities

$scriptConfig = @{}
$scriptConfig.WorkspaceFolder = ".."
$scriptConfig.SolutionFileName = "Kentico.Xperience.Zapier.sln"
$scriptConfig.AssemblyName = "DancingGoat"

<#
.DESCRIPTION
Returns shared configuration for PowerShell scripts
#>
function Get-ScriptConfig {
return $scriptConfig
}

<#
.DESCRIPTION
Returns the main solution file path
#>
function Get-SolutionPath {
return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) $($scriptConfig.SolutionFileName))
}

<#
.DESCRIPTION
Returns the web application folder path from the workspace root
#>
function Get-WebProjectPath {
return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "examples/DancingGoat")
}

<#
.DESCRIPTION
Returns the admin application folder path from the workspace root
#>
<#function Get-AdminProjectPath {
return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "src/Kentico.Community.Portal.Admin")
}#>

<#
.DESCRIPTION
Returns the admin client application folder path from the workspace root
#>
<#function Get-AdminClientProjectPath {
return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "src/Kentico.Community.Portal.Admin/Client")
}#>

<#
.DESCRIPTION
Returns the Core project folder path from the workspace root
#>
<#function Get-CoreProjectPath {
return Resolve-Path(Join-Path $($scriptConfig.WorkspaceFolder) "src/Kentico.Community.Portal.Core")
}#>


<#
.DESCRIPTION
Gets the database connection string from the user secrets or appsettings.json file
#>
<#function Get-ConnectionString {
$projectPath = Get-WebProjectPath
# Try to get the connection string from user secrets first
Write-Host "Checking for a connection string user secrets for project: $projectPath"
$connectionString = dotnet user-secrets list --project $projectPath `
| Select-String -Pattern "ConnectionStrings:" `
| ForEach-Object { $_.Line -replace '^ConnectionStrings:CMSConnectionString \= ', '' }
if (-not [string]::IsNullOrEmpty($connectionString)) {
Write-Host 'Using ConnectionString from user-secrets'
return $connectionString
}
$appSettingFileName = $Env:ASPNETCORE_ENVIRONMENT -eq "CI" ? 'appsettings.CI.json' : 'appsettings.json'
$jsonFilePath = Join-Path $projectPath $appSettingFileName
Write-Host "Using settings from $jsonFilePath"
if (!(Test-Path $jsonFilePath)) {
throw "Could not find file $jsonFilePath"
}
$appSettingsJson = Get-Content $jsonFilePath | Out-String | ConvertFrom-Json
$connectionString = $appSettingsJson.ConnectionStrings.CMSConnectionString;
if (!$connectionString) {
throw "Connection string not found in $jsonFilePath"
}
return $connectionString;
}#>

<#
.DESCRIPTION
Ensures the expression successfully exits and throws an exception
with the failed expression if it does not.
#>
function Invoke-ExpressionWithException {
param(
[string]$expression
)

Write-Host "$expression"

Invoke-Expression -Command $expression

if ($LASTEXITCODE -ne 0) {
$errorMessage = "[ $expression ] failed`n`n"

throw $errorMessage
}
}
function Write-Status {
param(
[string]$message
)

Write-Host $message -ForegroundColor Blue
}

function Write-Notification {
param(
[string]$message
)

Write-Host $message -ForegroundColor Magenta
}

function Write-Error {
param(
[string]$message
)

Write-Host $message -ForegroundColor Red
}

0 comments on commit 93891af

Please sign in to comment.