Azure pipeline deployment

 

Azure pipeline deployment


Tasks for background service



Set Environment Variables

steps:
- powershell: |
   [System.Environment]::SetEnvironmentVariable('DOTNET_ENVIRONMENT', $env:EnvironmentName, 'machine')
   
  displayName: 'Set Environment Variable'
  env:
    environmentName: $(EnvironmentName)


Extract Files

variables:
  Machine.Deploy.Path: 'c:\rm_agent\deploy'
  ServiceName: 'QT.EmailRouter'
steps:
  displayName: 'Extract files '
  inputs:
    archiveFilePatterns: '$(System.DefaultWorkingDirectory)/_EmailRouter/QT.EmailRouter/QT.EmailRouter.zip'
    destinationFolder: '$(Machine.Deploy.Path)\$(ServiceName)'
- task: ExtractFiles@1


Service Install

steps:
- powershell: |
Write-Host $env:sourcePath
Write-Host $env:directoryPath
Write-Host $env:exeName
Write-Host $env:serviceAccount
Write-Host $env:serviceName
Write-Host $env:serviceDescription
Write-Host $env:serviceStart

$exePath="$env:directoryPath\$env:exeName";
Write-Host $exePath

$serviceName = $env:serviceName
$serviceDescription = $env:serviceDescription
Write-Host "Uninstalling Previous Service"
Stop-Service $serviceName -Force -ErrorAction SilentlyContinue
Write-Host "Services Stopped"
Start-Sleep -Milliseconds 5000

if($Null -ne (get-process $serviceName -ErrorAction SilentlyContinue))
{
Write-Host "Stopping Process"
Get-Process -Name $serviceName | Wait-Process -Timeout 300 -ErrorAction SilentlyContinue
Get-Process -Name $serviceName | Stop-Process -Force -ErrorAction SilentlyContinue
Write-Host "Process Stopped"
}

if( $Null -ne (get-service $serviceName -ErrorAction SilentlyContinue))
{
(Get-WmiObject win32_service -Filter "name='$serviceName'").Delete();
}
Write-Host "Uninstall Successfully Complete"

if (Test-Path -Path "$env:directoryPath")
{
Remove-Item "$env:directoryPath\*" -Exclude "Logs" -Recurse -Force -EA SilentlyContinue -Verbose
} else {
New-Item -Path "$env:directoryPath" -ItemType directory
}

Copy-Item "$env:sourcePath\*" "$env:directoryPath" -Force -Recurse
Remove-Item "$env:sourcePath" -Force -Recurse

sc.exe create $serviceName displayname= $serviceName binpath= $exePath start= $env:serviceStart obj= $env:serviceAccount
sc.exe description $serviceName $serviceDescription
sc.exe failure $serviceName reset= 900 actions= restart/60000/restart/300000/restart/300000

Write-Host $serviceName
Write-Host $serviceDescription
Start-Service $serviceName

Write-Host "Install Service Complete"

displayName: 'Service Install'
env:
serviceAccount: $(ServiceAccount)
directoryPath: $(Service.Deploy.Path)
serviceName: $(ServiceName)
exeName: $(ExeName)
sourcePath: $(Machine.Deploy.Path)\$(ServiceName)
serviceStart: $(ServiceStart)


=====================================================

                                MSI Deployments


For those requiring a MSI package:

steps:
- powershell: |
Write-Host $env:MSIFilename

$exitCode = 0
trap
{
$e = $error[0].Exception
$e.Message
$e.StackTrace
}
function GetMsiProperty([string]$MsiFile, [string]$Property)
{
$installer = New-Object -comObject WindowsInstaller.Installer
$database = $installer.GetType().InvokeMember("OpenDatabase", [System.Reflection.BindingFlags]::InvokeMethod, $null, $installer, ($MsiFile, 0))
$view = $database.GetType().InvokeMember("OpenView", [System.Reflection.BindingFlags]::InvokeMethod, $null, $database, "SELECT `Value` FROM `Property` WHERE `Property`='$Property'")
$view.GetType().InvokeMember("Execute", [System.Reflection.BindingFlags]::InvokeMethod, $null, $view, $null)
$record = $view.GetType().InvokeMember("Fetch", [System.Reflection.BindingFlags]::InvokeMethod, $null, $view, $null)
return $record.GetType().InvokeMember("StringData", [System.Reflection.BindingFlags]::GetProperty, $null, $record, 1)
}
$msiFile = Resolve-Path $env:MSIFilename
$msiexec = "msiexec.exe"
$msiexecUninstallArgs = '/x {0} /qn /L* "' + $msiFile + '.log"'
$msiexecInstallArgs = '/i "{0}" /qn ACCEPT=YES /L*+ "' + $msiFile + '.log"'
if (![string]::IsNullOrEmpty($MsiCustomArgs))
{
$msiexecInstallArgs += ' ' + $MsiCustomArgs
}
$productName = $PrevProductName
if ([string]::IsNullOrEmpty($productName))
{
$productName = GetMsiProperty -MsiFile $msiFile -Property "ProductName"
}
"Product Name:" + $productName + "`n"
if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "64-bit")
{
"Running on a 64 bit OS.`n"
$uninstallRegKey = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
}
else
{
"Running on a 32 bit OS.`n"
$uninstallRegKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
}
$uninstallDriveName = "Uninstall"
$uninstallDrive = $uninstallDriveName + ":"
trap [System.IO.DriveNotFoundException]
{
$uninstallPSDrive = Get-PSDrive $uninstallDriveName
}
if (!$uninstallPSDrive)
{
New-PSDrive -Name $uninstallDriveName -PSProvider Registry -Root $uninstallRegKey | Out-Null
}
$uninstallCmd = Get-ChildItem -Path $uninstallDrive | Where-Object { $productName -eq $_.GetValue("DisplayName") } | ForEach-Object -Process { $_.GetValue("UninstallString") }
if ($uninstallCmd -ne $null)
{
"Uninstalling..."
$productCode = ([regex]'.*(?<ProductCode>\{[0-9A-Z-]*\}).*').Matches($uninstallCmd) | foreach {$_.Groups[1].Value}
if ($productCode -ne $null)
{
$msiexecArgs = $msiexecUninstallArgs -f $productCode
$msiexec + " " + $msiexecArgs
$process = [Diagnostics.Process]::Start($msiexec, $msiexecArgs)
$process.WaitForExit()
$exitCode = $process.ExitCode
$process.Close()
}
"Done.`n"
}
if ($exitCode -eq 0)
{
"Installing..."
$msiexecArgs = $msiexecInstallArgs -f $msiFile
$msiexec + " " + 'msiexecArgs'
$process = [Diagnostics.Process]::Start($msiexec, $msiexecArgs)
$process.WaitForExit()
$exitCode = $process.ExitCode
$process.Close()
"Done.`n"
}
if ($exitCode -gt 0)
{
"Exiting with error: " + $exitCode + "`n"
}
else
{
"The script completed successfully.`n"
}
exit $exitCode
displayName: 'PowerShell Script'
env:
MSIFilename: $(System.DefaultWorkingDirectory)\$(MsiPackage)
MsiCustomArgs: SERVICEACCOUNT=QT\appced.rbt.svc1$




Comments

Popular posts from this blog

Upgrading to .NET8 from desktop versions 4.8.X

JSON Web Tokens

GHL > Set website so shorter URL address