From 617af759ae5f9a0565d9706702435c80eeb24013 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 4 Feb 2016 22:09:41 -0600 Subject: [PATCH] Cache NuGet packages on our CI server between builds. Currently on our CI builds, we point our NuGet cache under the repo. In between builds the repo gets deleted, thus the cache is lost. This change moves the cache to %userprofile%\.nuget\packages on CI and dev boxes. On CI, we expire the cache after a day by default. --- scripts/build/restore-packages.ps1 | 21 +++++++++++++++++++++ scripts/build/restore-packages.sh | 20 ++++++++++++++++++++ scripts/ci_build.sh | 2 ++ scripts/common/_nuget.ps1 | 12 +++++------- scripts/common/_nuget.sh | 12 ++++++++++-- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/scripts/build/restore-packages.ps1 b/scripts/build/restore-packages.ps1 index 7ec12dced..9bce280c5 100644 --- a/scripts/build/restore-packages.ps1 +++ b/scripts/build/restore-packages.ps1 @@ -5,6 +5,27 @@ . $PSScriptRoot\..\common\_common.ps1 +if ($env:CI_BUILD -eq "1") { + # periodically clear out the package cache on the CI server + $PackageCacheFile = "$env:NUGET_PACKAGES\packageCacheTime.txt" + + if(!(Test-Path $PackageCacheFile)) { + Get-Date | Out-File -FilePath $PackageCacheFile + } + else { + $PackageCacheTimeProperty = Get-ItemProperty -Path $PackageCacheFile -Name CreationTimeUtc + $PackageCacheTime = [datetime]($PackageCacheTimeProperty).CreationTimeUtc + + if ($PackageCacheTime -lt ([datetime]::UtcNow).AddHours(-$env:NUGET_PACKAGES_CACHE_TIME_LIMIT)) { + header "Clearing package cache" + + Remove-Item -Recurse -Force "$env:NUGET_PACKAGES" + mkdir $env:NUGET_PACKAGES | Out-Null + Get-Date | Out-File -FilePath $PackageCacheFile + } + } +} + # Restore packages # NOTE(anurse): I had to remove --quiet, because NuGet3 is too quiet when that's provided :( header "Restoring packages" diff --git a/scripts/build/restore-packages.sh b/scripts/build/restore-packages.sh index f5235c4aa..44975fbf6 100755 --- a/scripts/build/restore-packages.sh +++ b/scripts/build/restore-packages.sh @@ -16,6 +16,26 @@ DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" source "$DIR/../common/_common.sh" +if [ ! -z "$CI_BUILD" ]; then + # periodically clear out the package cache on the CI server + PackageCacheFile="$NUGET_PACKAGES/packageCacheTime.txt" + if [ ! -f $PackageCacheFile ]; then + date > $PackageCacheFile + else + #$NUGET_PACKAGES_CACHE_TIME_LIMIT is in hours + CacheTimeLimitInSeconds=$(($NUGET_PACKAGES_CACHE_TIME_LIMIT * 3600)) + CacheExpireTime=$(($(date +%s) - $CacheTimeLimitInSeconds)) + + if [ $(date +%s -r $PackageCacheFile) -lt $CacheExpireTime ]; then + header "Clearing package cache" + + rm -Rf $NUGET_PACKAGES + mkdir -p $NUGET_PACKAGES + date > $PackageCacheFile + fi + fi +fi + header "Restoring packages" dotnet restore "$REPOROOT/src" --runtime "$RID" $DISABLE_PARALLEL diff --git a/scripts/ci_build.sh b/scripts/ci_build.sh index c7604c65e..488e3192a 100755 --- a/scripts/ci_build.sh +++ b/scripts/ci_build.sh @@ -6,6 +6,8 @@ set -e +export CI_BUILD=1 + SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" diff --git a/scripts/common/_nuget.ps1 b/scripts/common/_nuget.ps1 index 8fe724905..df3428d69 100644 --- a/scripts/common/_nuget.ps1 +++ b/scripts/common/_nuget.ps1 @@ -3,14 +3,12 @@ # Licensed under the MIT license. See LICENSE file in the project root for full license information. # -if ($env:CI_BUILD -eq "1") { - $env:NUGET_PACKAGES = (Join-Path $RepoRoot "artifacts\home\.nuget\packages") -} else { - $env:NUGET_PACKAGES = (Join-Path $env:USERPROFILE ".nuget\packages") -} - +$env:NUGET_PACKAGES = (Join-Path $env:USERPROFILE ".nuget\packages") $env:DOTNET_PACKAGES = $env:NUGET_PACKAGES $env:DNX_PACKAGES = $env:NUGET_PACKAGES if(!(Test-Path $env:NUGET_PACKAGES)) { mkdir $env:NUGET_PACKAGES | Out-Null -} \ No newline at end of file +} + +# default the package cache expiration to 1 week, in hours +setEnvIfDefault "NUGET_PACKAGES_CACHE_TIME_LIMIT" (7 * 24) diff --git a/scripts/common/_nuget.sh b/scripts/common/_nuget.sh index 8261dd446..3c03f7523 100644 --- a/scripts/common/_nuget.sh +++ b/scripts/common/_nuget.sh @@ -4,7 +4,15 @@ # Licensed under the MIT license. See LICENSE file in the project root for full license information. # -## Temporarily redirect to the NuGet package installation location export NUGET_PACKAGES=~/.nuget/packages export DOTNET_PACKAGES=$NUGET_PACKAGES -export DNX_PACKAGES=$NUGET_PACKAGES \ No newline at end of file +export DNX_PACKAGES=$NUGET_PACKAGES + +if [ ! -d $NUGET_PACKAGES ]; then + mkdir -p $NUGET_PACKAGES +fi + +if [ -z "$NUGET_PACKAGES_CACHE_TIME_LIMIT" ]; then + # default the package cache expiration to 1 week, in hours + export NUGET_PACKAGES_CACHE_TIME_LIMIT=$(( 7 * 24 )) +fi