Skip to content

Commit d0f25d6

Browse files
committed
Improved Daily-Maintenance.ps1
1 parent 76cefe9 commit d0f25d6

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ nuget.config
1515
global.json
1616
*.g.json
1717
*.g.ps1
18+
scripts/build-agents/last-execution.txt
Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
$ErrorActionPreference = 'Stop'
22

3-
# Remove all Docker images that have not been used for 7 days.
4-
docker image prune -a --filter "until=168h" --force
3+
# Get the script's directory
4+
$scriptDir = $PSScriptRoot
5+
$lastExecutionFile = Join-Path $scriptDir "last-execution.txt"
56

6-
# Pull this repo.
7-
git pull
7+
# Check if the script has been executed in the last 24 hours
8+
$shouldExecute = $true
9+
if (Test-Path $lastExecutionFile) {
10+
$lastExecutionTime = (Get-Item $lastExecutionFile).LastWriteTime
11+
$timeSinceLastExecution = (Get-Date) - $lastExecutionTime
12+
13+
if ($timeSinceLastExecution.TotalHours -lt 24) {
14+
Write-Host "Daily maintenance was already executed $([math]::Round($timeSinceLastExecution.TotalHours, 2)) hours ago. Skipping execution."
15+
$shouldExecute = $false
16+
}
17+
}
18+
19+
if ($shouldExecute) {
20+
Write-Host "Executing daily maintenance..."
21+
22+
# Update the last execution timestamp by touching the file
23+
"Last maintenance executed at $(Get-Date)" | Out-File $lastExecutionFile -Encoding UTF8
24+
25+
# Remove all Docker images that have not been used for 7 days.
26+
docker image prune -a --filter "until=168h" --force
27+
28+
# Pull this repo.
29+
git pull
30+
31+
Write-Host "Daily maintenance completed successfully."
32+
}

0 commit comments

Comments
 (0)