...

Text file src/github.com/Azure/azure-sdk-for-go/eng/common/scripts/copy-docs-to-blobstorage.ps1

Documentation: github.com/Azure/azure-sdk-for-go/eng/common/scripts

     1# Note, due to how `Expand-Archive` is leveraged in this script,
     2# powershell core is a requirement for successful execution.
     3param (
     4  $AzCopy,
     5  $DocLocation,
     6  $SASKey,
     7  $BlobName,
     8  $ExitOnError=1,
     9  $UploadLatest=1,
    10  $PublicArtifactLocation = "",
    11  $RepoReplaceRegex = ""
    12)
    13
    14. (Join-Path $PSScriptRoot common.ps1)
    15
    16# Regex inspired but simplified from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
    17$SEMVER_REGEX = "^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-?(?<prelabel>[a-zA-Z-]*)(?:\.?(?<prenumber>0|[1-9]\d*))?)?$"
    18
    19function ToSemVer($version){
    20    if ($version -match $SEMVER_REGEX)
    21    {
    22        if(-not $matches['prelabel']) {
    23            # artifically provide these values for non-prereleases to enable easy sorting of them later than prereleases.
    24            $prelabel = "zzz"
    25            $prenumber = 999;
    26            $isPre = $false;
    27        }
    28        else {
    29            $prelabel = $matches["prelabel"]
    30            $prenumber = 0
    31
    32            # some older packages don't have a prenumber, should handle this
    33            if($matches["prenumber"]){
    34                $prenumber = [int]$matches["prenumber"]
    35            }
    36
    37            $isPre = $true;
    38        }
    39
    40        New-Object PSObject -Property @{
    41            Major = [int]$matches['major']
    42            Minor = [int]$matches['minor']
    43            Patch = [int]$matches['patch']
    44            PrereleaseLabel = $prelabel
    45            PrereleaseNumber = $prenumber
    46            IsPrerelease = $isPre
    47            RawVersion = $version
    48        }
    49    }
    50    else
    51    {
    52        if ($ExitOnError)
    53        {
    54            throw "Unable to convert $version to valid semver and hard exit on error is enabled. Exiting."
    55        }
    56        else
    57        {
    58            return $null
    59        }
    60    }
    61}
    62
    63function SortSemVersions($versions)
    64{
    65    return $versions | Sort -Property Major, Minor, Patch, PrereleaseLabel, PrereleaseNumber -Descending
    66}
    67
    68function Sort-Versions
    69{
    70    Param (
    71        [Parameter(Mandatory=$true)] [string[]]$VersionArray
    72    )
    73
    74    # standard init and sorting existing
    75    $versionsObject = New-Object PSObject -Property @{
    76        OriginalVersionArray = $VersionArray
    77        SortedVersionArray = @()
    78        LatestGAPackage = ""
    79        RawVersionsList = ""
    80        LatestPreviewPackage = ""
    81    }
    82
    83    if ($VersionArray.Count -eq 0)
    84    {
    85        return $versionsObject
    86    }
    87
    88    $versionsObject.SortedVersionArray = @(SortSemVersions -versions ($VersionArray | % { ToSemVer $_}))
    89    $versionsObject.RawVersionsList = $versionsObject.SortedVersionArray | % { $_.RawVersion }
    90
    91    # handle latest and preview
    92    # we only want to hold onto the latest preview if its NEWER than the latest GA.
    93    # this means that the latest preview package either A) has to be the latest value in the VersionArray
    94    # or B) set to nothing. We'll handle the set to nothing case a bit later.
    95    $versionsObject.LatestPreviewPackage = $versionsObject.SortedVersionArray[0].RawVersion
    96    $gaVersions = $versionsObject.SortedVersionArray | ? { !$_.IsPrerelease }
    97
    98    # we have a GA package
    99    if ($gaVersions.Count -ne 0)
   100    {
   101        # GA is the newest non-preview package
   102        $versionsObject.LatestGAPackage = $gaVersions[0].RawVersion
   103
   104        # in the case where latest preview == latestGA (because of our default selection earlier)
   105        if ($versionsObject.LatestGAPackage -eq $versionsObject.LatestPreviewPackage)
   106        {
   107            # latest is newest, unset latest preview
   108            $versionsObject.LatestPreviewPackage = ""
   109        }
   110    }
   111
   112    return $versionsObject
   113}
   114
   115function Get-Existing-Versions
   116{
   117    Param (
   118        [Parameter(Mandatory=$true)] [String]$PkgName
   119    )
   120    $versionUri = "$($BlobName)/`$web/$($Language)/$($PkgName)/versioning/versions"
   121    LogDebug "Heading to $versionUri to retrieve known versions"
   122
   123    try {
   124        return ((Invoke-RestMethod -Uri $versionUri -MaximumRetryCount 3 -RetryIntervalSec 5) -Split "\n" | % {$_.Trim()} | ? { return $_ })
   125    }
   126    catch {
   127        # Handle 404. If it's 404, this is the first time we've published this package.
   128        if ($_.Exception.Response.StatusCode.value__ -eq 404){
   129            LogDebug "Version file does not exist. This is the first time we have published this package."
   130        }
   131        else {
   132            # If it's not a 404. exit. We don't know what's gone wrong.
   133            LogError "Exception getting version file. Aborting"
   134            LogError $_
   135            exit(1)
   136        }
   137    }
   138}
   139
   140function Update-Existing-Versions
   141{
   142    Param (
   143        [Parameter(Mandatory=$true)] [String]$PkgName,
   144        [Parameter(Mandatory=$true)] [String]$PkgVersion,
   145        [Parameter(Mandatory=$true)] [String]$DocDest
   146    )
   147    $existingVersions = @(Get-Existing-Versions -PkgName $PkgName)
   148
   149    LogDebug "Before I update anything, I am seeing $existingVersions"
   150
   151    if (!$existingVersions)
   152    {
   153        $existingVersions = @()
   154        $existingVersions += $PkgVersion
   155        LogDebug "No existing versions. Adding $PkgVersion."
   156    }
   157    else
   158    {
   159        $existingVersions += $pkgVersion
   160        LogDebug "Already Existing Versions. Adding $PkgVersion."
   161    }
   162
   163    $existingVersions = $existingVersions | Select-Object -Unique
   164
   165    # newest first
   166    $sortedVersionObj = (Sort-Versions -VersionArray $existingVersions)
   167
   168    LogDebug $sortedVersionObj
   169    LogDebug $sortedVersionObj.LatestGAPackage
   170    LogDebug $sortedVersionObj.LatestPreviewPackage
   171
   172    # write to file. to get the correct performance with "actually empty" files, we gotta do the newline
   173    # join ourselves. This way we have absolute control over the trailing whitespace.
   174    $sortedVersionObj.RawVersionsList -join "`n" | Out-File -File "$($DocLocation)/versions" -Force -NoNewLine
   175    $sortedVersionObj.LatestGAPackage | Out-File -File "$($DocLocation)/latest-ga" -Force -NoNewLine
   176    $sortedVersionObj.LatestPreviewPackage | Out-File -File "$($DocLocation)/latest-preview" -Force -NoNewLine
   177
   178    & $($AzCopy) cp "$($DocLocation)/versions" "$($DocDest)/$($PkgName)/versioning/versions$($SASKey)" --cache-control "max-age=300, must-revalidate"
   179    & $($AzCopy) cp "$($DocLocation)/latest-preview" "$($DocDest)/$($PkgName)/versioning/latest-preview$($SASKey)" --cache-control "max-age=300, must-revalidate"
   180    & $($AzCopy) cp "$($DocLocation)/latest-ga" "$($DocDest)/$($PkgName)/versioning/latest-ga$($SASKey)" --cache-control "max-age=300, must-revalidate"
   181    return $sortedVersionObj
   182}
   183
   184function Upload-Blobs
   185{
   186    Param (
   187        [Parameter(Mandatory=$true)] [String]$DocDir,
   188        [Parameter(Mandatory=$true)] [String]$PkgName,
   189        [Parameter(Mandatory=$true)] [String]$DocVersion,
   190        [Parameter(Mandatory=$false)] [String]$ReleaseTag
   191    )
   192    #eg : $BlobName = "https://azuresdkdocs.blob.core.windows.net"
   193    $DocDest = "$($BlobName)/`$web/$($Language)"
   194
   195    LogDebug "DocDest $($DocDest)"
   196    LogDebug "PkgName $($PkgName)"
   197    LogDebug "DocVersion $($DocVersion)"
   198    LogDebug "DocDir $($DocDir)"
   199    LogDebug "Final Dest $($DocDest)/$($PkgName)/$($DocVersion)"
   200    LogDebug "Release Tag $($ReleaseTag)"
   201
   202    # Use the step to replace default branch link to release tag link 
   203    if ($ReleaseTag) {
   204        foreach ($htmlFile in (Get-ChildItem $DocDir -include *.html -r)) 
   205        {
   206            $fileContent = Get-Content -Path $htmlFile -Raw
   207            $updatedFileContent = $fileContent -replace $RepoReplaceRegex, "`${1}$ReleaseTag"
   208            if ($updatedFileContent -ne $fileContent) {
   209                Set-Content -Path $htmlFile -Value $updatedFileContent -NoNewLine
   210            }
   211        }
   212    } 
   213    else {
   214        LogWarning "Not able to do the default branch link replacement, since no release tag found for the release. Please manually check."
   215    } 
   216   
   217    LogDebug "Uploading $($PkgName)/$($DocVersion) to $($DocDest)..."
   218    & $($AzCopy) cp "$($DocDir)/**" "$($DocDest)/$($PkgName)/$($DocVersion)$($SASKey)" --recursive=true --cache-control "max-age=300, must-revalidate"
   219    
   220    LogDebug "Handling versioning files under $($DocDest)/$($PkgName)/versioning/"
   221    $versionsObj = (Update-Existing-Versions -PkgName $PkgName -PkgVersion $DocVersion -DocDest $DocDest)
   222    $latestVersion = $versionsObj.LatestGAPackage 
   223    if (!$latestVersion) {
   224        $latestVersion = $versionsObj.LatestPreviewPackage 
   225    }
   226    LogDebug "Fetching the latest version $latestVersion"
   227    
   228    if ($UploadLatest -and ($latestVersion -eq $DocVersion))
   229    {
   230        LogDebug "Uploading $($PkgName) to latest folder in $($DocDest)..."
   231        & $($AzCopy) cp "$($DocDir)/**" "$($DocDest)/$($PkgName)/latest$($SASKey)" --recursive=true --cache-control "max-age=300, must-revalidate"
   232    }
   233}
   234
   235if ($PublishGithubIODocsFn -and (Test-Path "Function:$PublishGithubIODocsFn"))
   236{
   237    &$PublishGithubIODocsFn -DocLocation $DocLocation -PublicArtifactLocation $PublicArtifactLocation
   238}
   239else
   240{
   241    LogWarning "The function for '$PublishGithubIODocsFn' was not found.`
   242    Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
   243    See https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/common_engsys.md#code-structure"
   244}
   245

View as plain text