From f74de5f41f49fca5123e05b3aca0a6a32b8fb8f3 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 8 Nov 2021 19:07:18 -0800 Subject: [PATCH] Validate python docs packages using docker --- eng/pipelines/docindex.yml | 11 +++++++-- eng/scripts/Language-Settings.ps1 | 39 +++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/eng/pipelines/docindex.yml b/eng/pipelines/docindex.yml index 9d59564d6cc2..22f587a1e558 100644 --- a/eng/pipelines/docindex.yml +++ b/eng/pipelines/docindex.yml @@ -10,6 +10,7 @@ jobs: DocRepoLocation: $(Pipeline.Workspace)/docs DocRepoOwner: MicrosoftDocs DocRepoName: azure-docs-sdk-python + DocValidationImageId: azuresdkimages.azurecr.io/pyrefautocr:latest steps: # Docs CI uses Python 3.6.8 but that is not available on this image - task: UsePythonVersion@0 @@ -21,6 +22,12 @@ jobs: - pwsh: pip install --upgrade pip wheel setuptools displayName: Update python tools for package verification + # Pull and build the docker image. + - template: /eng/common/pipelines/templates/steps/docker-pull-image.yml + parameters: + ContainerRegistryClientId: $(azuresdkimages-cr-clientid) + ContainerRegistryClientSecret: $(azuresdkimages-cr-clientsecret) + ImageId: "$(DocValidationImageId)" # Sync docs repo onboarding files/folders - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml parameters: @@ -38,7 +45,7 @@ jobs: inputs: pwsh: true filePath: eng/common/scripts/Update-DocsMsPackages.ps1 - arguments: -DocRepoLocation $(DocRepoLocation) + arguments: -DocRepoLocation $(DocRepoLocation) -ImageId '$(DocValidationImageId)' displayName: Update Docs Onboarding condition: and(succeeded(), or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Force.MainUpdate'], 'true'))) # Push changes to docs repo @@ -70,7 +77,7 @@ jobs: inputs: pwsh: true filePath: eng/common/scripts/Update-DocsMsPackages.ps1 - arguments: -DocRepoLocation $(DocRepoLocation) -PackageSourceOverride "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/" + arguments: -DocRepoLocation $(DocRepoLocation) -PackageSourceOverride "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/" -ImageId '$(DocValidationImageId)' displayName: Update Docs Onboarding for Daily branch - template: /eng/common/pipelines/templates/steps/git-push-changes.yml parameters: diff --git a/eng/scripts/Language-Settings.ps1 b/eng/scripts/Language-Settings.ps1 index 581f6570a1a9..eb8bdcae81f7 100644 --- a/eng/scripts/Language-Settings.ps1 +++ b/eng/scripts/Language-Settings.ps1 @@ -162,18 +162,43 @@ function Get-python-GithubIoDocIndex() } function ValidatePackage($packageName, $packageVersion, $workingDirectory) { - $packageExpression = "$packageName$packageVersion" - Write-Host "Validating $packageExpression" - - $installTargetFolder = Join-Path $workingDirectory $packageName - New-Item -ItemType Directory -Force -Path $installTargetFolder | Out-Null - # Add more validation by replicating as much of the docs CI process as # possible # https://github.com/Azure/azure-sdk-for-python/issues/20109 + if (!$ImageId) { + Write-Host "Validating using pip command directly on $packageName." + FallbackValidation -packageName "$packageName" -packageVersion "$packageVersion" -workingDirectory $workingDirectory + } + else { + Write-Host "Validating using $ImageId on $packageName." + DockerValidation -packageName "$packageName" -packageVersion "$packageVersion" + } +} +function DockerValidation($packageName, $packageVersion) { + $packageExpression = "$packageName==$packageVersion" + docker run -e TARGET_PACKAGE=$packageExpression -t $ImageId + # The docker exit codes: https://docs.docker.com/engine/reference/run/#exit-status + # If the docker failed because of docker itself instead of the application, + # we should skip the validation and keep the packages. + if ($LASTEXITCODE -eq 125 -Or $LASTEXITCODE -eq 126 -Or $LASTEXITCODE -eq 127) { + Write-Host $commandLine + LogWarning "The `docker` command does not work with exit code $LASTEXITCODE. Fall back to npm install $packageName directly." + FallbackValidation -packageName "$packageName" -packageVersion "$packageVersion" + } + elseif ($LASTEXITCODE -ne 0) { + Write-Host $commandLine + LogWarning "Package $($Package.name) ref docs validation failed." + return $false + } + return $true +} + +function FallbackValidation($packageName, $packageVersion, $workingDirectory) { + $installTargetFolder = Join-Path $workingDirectory $packageName + New-Item -ItemType Directory -Force -Path $installTargetFolder | Out-Null + $packageExpression = "$packageName$packageVersion" try { $pipInstallOutput = "" - $extraIndexUrl = " --extra-index-url=$PackageSourceOverride" if ($PackageSourceOverride) { Write-Host "pip install $packageExpression --no-cache-dir --target $installTargetFolder --extra-index-url=$PackageSourceOverride" $pipInstallOutput = pip `