ArtifactoryCache.ps1

$script:ArtifactoryApiKey = "X-JFrog-Art-Api"

function Save-ArtifactoryCache {
    [CmdletBinding()]
    param (
        # interface
        [ValidatePattern('[a-zA-Z_]+')]
        [Parameter(Mandatory=$true)]
        [string] $Id,
        [Parameter(Mandatory=$true)][string[]] $Paths,
        [string[]] $PathsToComputeHash = @(),

        # configuration
        [string] $ArtifactoryServerUrl = $env:CI_CACHE_ARTIFACTORY_SERVER_URL,
        [string] $Repository = $env:CI_CACHE_ARTIFACTORY_REPOSITORY_NAME,
        [string] $ApiToken = $env:CI_CACHE_ARTIFACTORY_API_TOKEN,
        [int] $RepeatCount = 3
    )

    $authHeader = @{ $script:ArtifactoryApiKey = $ApiToken }

    return Save-CacheInternal `
        -Id $Id `
        -Paths $Paths `
        -PathsToComputeHash $PathsToComputeHash `
        -SaveHandler { 
            param($path)

            $fullname = Split-Path -Path $path -Leaf
            $fullname = "$ArtifactoryServerUrl/artifactory/$Repository/$fullname"
        
            Invoke-WebRequest -Uri $fullname -Method Put -InFile $path -Headers $authHeader | Out-Null
        } `
        -TestHandler { 
            param($key)
            Invoke-WebRequest -Uri "$ArtifactoryServerUrl/artifactory/api/storage/$Repository/$key" -Method Get -Headers $authHeader | Out-Null
        } `
        -RepeatCount $RepeatCount
}

function Test-ArtifactoryCache {
    [CmdletBinding()]
    param (
        # interface
        [ValidatePattern('[a-zA-Z_]+')]
        [Parameter(Mandatory=$true)]
        [string] $Id,
        [string[]] $PathsToComputeHash = @(),

        # configuration
        [string] $ArtifactoryServerUrl = $env:CI_CACHE_ARTIFACTORY_SERVER_URL,
        [string] $Repository = $env:CI_CACHE_ARTIFACTORY_REPOSITORY_NAME,
        [string] $ApiToken = $env:CI_CACHE_ARTIFACTORY_API_TOKEN,
        [int] $RepeatCount = 3
    )

    $authHeader = @{ $script:ArtifactoryApiKey = $ApiToken }

    return Test-CacheInternal `
        -Id $Id `
        -PathsToComputeHash $PathsToComputeHash `
        -TestHandler { 
            param($key)
            $url = "$ArtifactoryServerUrl/artifactory/api/storage/$Repository/$key"
            Invoke-WebRequest -Uri $url -Method Get -Headers $authHeader | Out-Null
        } `
        -RepeatCount $RepeatCount
}

function Restore-ArtifactoryCache {
    [CmdletBinding()]
    param (
        # interface
        [ValidatePattern('[a-zA-Z_]+')]
        [Parameter(Mandatory=$true)]
        [string] $Id,
        [string[]] $PathsToComputeHash = @(),
        [string] $RestoreHashKey,
        [string] $OutputPath,

        # configuration
        [string] $ArtifactoryServerUrl = $env:CI_CACHE_ARTIFACTORY_SERVER_URL,
        [string] $Repository = $env:CI_CACHE_ARTIFACTORY_REPOSITORY_NAME,
        [string] $ApiToken = $env:CI_CACHE_ARTIFACTORY_API_TOKEN,
        [int] $RepeatCount = 3
    )

    $authHeader = @{ $script:ArtifactoryApiKey = $ApiToken }

    return Restore-CacheInternal `
        -Id $Id `
        -PathsToComputeHash $PathsToComputeHash `
        -RestoreHashKey $RestoreHashKey `
        -OutputPath $OutputPath `
        -RestoreHandler { 
            param($key)
            $fullname = "$ArtifactoryServerUrl/artifactory/$Repository/$key"
            Invoke-WebRequest -Uri $fullname -OutFile $key -Headers $authHeader -Method Get | Out-Null
        } `
        -RepeatCount $RepeatCount
}