我们可以在Octopus Deploy中配置/安排Windows作业吗?
我想在Octopus Deploy中安排Windows作业。我的主要目标是自动化这些工作并减少人为干预的数量。
有没有办法在Octopus Deploy中配置Windows作业? 我试图搜索八达通图书馆,但我无法实施我发现的一些图书馆。
答案 0 :(得分:3)
如果您将此脚本打包到NuGet包中并将其命名为Deploy.ps1,它应该运行一个自定义部署来引导schtasks.exe。
# -------------------------------------------------
# Octopus Powershell For Installing Scheduled Tasks
# -------------------------------------------------
#
# Ver Who When What
# 1.0 Evolve Software Ltd 17-05-16 Initial Version
# Script Input Parameters
# $TaskName - The name of the task that we use to create / remove
# $TaskExecutableName - the scheduled task executeable name
# $TaskSchedule - MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE
# $TaskModifier - Specifies how frequently the task runs in its schedule type. This parameter is required for a MONTHLY schedule
# $TaskStartTime - StartTime Specifies the time of day that the task starts in HH:MM:SS 24-hour format. The default value is the current local
# $TaskDisable - Disable the task after it's installed
# Script Version
$CurrentScriptVersion = "1.0"
function Main()
{
Write-Host "================== Installing Scheduled Tasks - Version"$CurrentScriptVersion": START =================="
# Log input variables passed in
Log-Variables
Write-Host
# Tear down any existing stuff
Delete-Scheduled-Task $TaskName
# Create scheduled task
$TaskPath = $OctopusOriginalPackageDirectoryPath + "\" + $TaskExecutableName
Create-Scheduled-Task $TaskName $TaskPath $TaskSchedule $TaskModifier $TaskStartTime $TaskDisable
Write-Host "================== Installing Scheduled Tasks - Version"$CurrentScriptVersion": END =================="
}
function Log-Variables
{
Write-Host "TaskName" $TaskName
Write-Host "TaskExecutableName" $TaskExecutableName
Write-Host "TaskSchedule" $TaskSchedule
Write-Host "TaskModifier" $TaskModifier
Write-Host "TaskStartTime" $TaskStartTime
Write-Host "TaskDisable" $TaskDisable
Write-Host "Computername" (gc env:computername)
}
function Create-Scheduled-Task($taskName, $taskPath, $taskSchedule, $taskModifier, $taskStartTime, $taskDisable)
{
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357%28v=vs.85%29.aspx
Write-Host "Creating task" $taskName
Write-Host "Task path" $taskPath
Write-Host "Task schedule" $taskSchedule
Write-Host "Task modifier" $taskModifier
Write-Host "Task start time" $taskStartTime
Write-Host "Task disable" $taskDisable
if($taskModifier)
{
Write-Host "Cmd: schtasks.exe /Create /TN "$taskName" /SC "$taskSchedule" /MO "$taskModifier" /ST "$taskStartTime" /RL highest /TR "$taskPath" /RU SYSTEM"
#schtasks.exe /Create /TN $taskName /SC MINUTE /MO 5 /ST 00:00 /RL highest /TR $taskPath /RU SYSTEM
schtasks.exe /Create /TN $taskName /SC $taskSchedule /MO $taskModifier /ST $taskStartTime /RL highest /TR $taskPath /RU SYSTEM
}
else
{
Write-Host "Cmd: schtasks.exe /Create /TN "$taskName" /SC "$taskSchedule" /ST "$taskStartTime" /RL highest /TR "$taskPath" /RU SYSTEM"
#schtasks.exe /Create /TN $taskName /SC DAILY /ST 03:00:00 /RL highest /TR $taskPath /RU SYSTEM
schtasks.exe /Create /TN $taskName /SC $taskSchedule /ST $taskStartTime /RL highest /TR $taskPath /RU SYSTEM
}
if (IsTrue($taskDisable)) {
Write-Host "Disabling task: schtasks.exe /Change /TN $taskName /DISABLE"
schtasks.exe /Change /TN $taskName /DISABLE
Write-Host "Task disabled"
}
}
function Delete-Scheduled-Task($taskName)
{
# Stop Scheduled Task
Write-Host "Stopping task:" $taskName
try
{
schtasks.exe /end /TN $taskName
}
catch [System.Exception]
{
Write-Output $_
Write-Host "Unable to end task - may not exist"
}
# Wait for it to stop
while($true){
$i++
$status = schtasks /query | select-string -patt $taskName
if(!$status)
{
Write-Host "Task not running"
break
}
if($status.ToString().Contains("Running")) {
Write-Host "Task still running: "$status.ToString() -foreground red
} else {
Write-Host "Task has ended: "$status.ToString()
break
}
if($i -ge 60) # If task takes longer than 1 minute to stop bomb out
{
throw "ERROR: Unable to end task, please end it manually then kick of the deployment again"
return -1
}
Start-Sleep -s 1
}
# Remove Scheduled Task (force delete)
Write-Host "Deleting task:" $taskName
schtasks.exe /delete /TN $taskName /F
}
function IsTrue( $boolString )
{
if($boolString) #not null or not empty string
{
if($boolString -is [bool])
{
return $boolString
}
elseif($boolString -is [string])
{
if(($boolString -eq "true") -or ($boolString -eq "True") -or ($boolString -eq "1"))
{
return $true
}
}
elseif ($boolString -is [int])
{
if($boolString -eq 1)
{
return $true
}
}
}
return $false
}
Main
您现在可以设置一些变量来配置计划任务,例如这些。
希望这有帮助。