Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use docker to validate packages in js docindex #18405

Merged
merged 12 commits into from
Nov 5, 2021
16 changes: 13 additions & 3 deletions eng/pipelines/docindex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
DocRepoLocation: $(Pipeline.Workspace)/docs
DocRepoOwner: MicrosoftDocs
DocRepoName: azure-docs-sdk-node
ImageId: azuresdkimages.azurecr.io/jsrefautocr:latest
steps:
# Sync docs repo (this can be sparse)
- template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
Expand All @@ -22,15 +23,23 @@ jobs:
Repositories:
- Name: $(DocRepoOwner)/$(DocRepoName)
WorkingDirectory: $(DocRepoLocation)
# 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: '$(ImageId)'


# Call update docs ci script to onboard packages
- task: Powershell@2
inputs:
pwsh: true
filePath: eng/common/scripts/Update-DocsMsPackages.ps1
arguments: -DocRepoLocation $(DocRepoLocation)
arguments: -DocRepoLocation $(DocRepoLocation) -ImageId '$(ImageId)'
displayName: Update Docs Onboarding

condition: and(succeeded(), or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Force.MainUpdate'], 'true')))

# Push changes to docs repo
- template: /eng/common/pipelines/templates/steps/set-default-branch.yml
parameters:
Expand Down Expand Up @@ -60,8 +69,9 @@ jobs:
inputs:
pwsh: true
filePath: eng/common/scripts/Update-DocsMsPackages.ps1
arguments: -DocRepoLocation $(DocRepoLocation)
arguments: -DocRepoLocation $(DocRepoLocation) -ImageId '$(ImageId)'
displayName: Update Docs Onboarding for Daily branch

- template: /eng/common/pipelines/templates/steps/git-push-changes.yml
parameters:
BaseRepoBranch: $(DailyDocsBranchName)
Expand Down
2 changes: 1 addition & 1 deletion eng/scripts/Language-Settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function ValidatePackagesForDocs($packages) {
# Get value for variables outside of the Foreach-Object scope
$scriptRoot = "$using:scriptRoot"
$workingDirectory = "$using:tempDirectory"
return ."$scriptRoot\validate-docs-package.ps1" -Package $_ -WorkingDirectory $workingDirectory
return ."$scriptRoot\validate-docs-package.ps1" -Package $_ -DocValidationImageId "$using:imageId" -WorkDirectory $workingDirectory
}

# Clean up temp folder
Expand Down
70 changes: 52 additions & 18 deletions eng/scripts/validate-docs-package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ Validates packages by simulating docs CI steps

.PARAMETER Package
An object describing a package configuration to validate in the format used by
docs CI onboarding format.

docker image, which is the wrapping of docs CI build commands.
Required attributes:
- name: The name of the package

Supported optional attributes:
- registry: The registry to use for the package if it's not NPM

Example:

```
@{
name = "@azure/attestation@dev";
folder = "./types";
registry = "<url>";
...
}
```
.PARAMETER DocValidationImageId
The image name for package validation in format of '$containerRegistry/$imageName:$tag'.
e.g. azuresdkimages.azurecr.io/jsrefautocr:latest

.PARAMETER WorkingDirectory
Supplied to `npm install ... --prefix $WorkingDirectory`. The place where the
Expand All @@ -30,27 +28,63 @@ node_modules folder is created and the package is installed

param(
[object] $Package,
[string] $DocValidationImageId,
[string] $WorkingDirectory
)
."$PSScriptRoot\..\common\scripts\common.ps1"

function GetResult($success, $package, $output) {
return @{ Success = $success; Package = $package; Output = $output }
}
function FallbackValidation() {
$additionalParameters = @()
$prefixDirectory = New-Item -ItemType Directory -Force -Path "$WorkingDirectory\$($Package.name)"
if ($Package.registry) {
Write-Host $Package.registry
$additionalParameters += @("--registry", $Package.registry)
}

$prefixDirectory = New-Item -ItemType Directory -Force -Path "$WorkingDirectory\$($Package.name)"

$additionalParameters = @()
if ($Package.registry) {
Write-Host $Package.registry
$additionalParameters += @("--registry", $Package.registry)
Write-Host "npm install $($Package.name) --prefix $prefixDirectory $additionalParameters"
$installOutput = npm install $Package.name --prefix $prefixDirectory @additionalParameters 2>&1
if ($LASTEXITCODE -ne 0) {
LogWarning "Package install failed: $($Package.name)"
return GetResult $false $Package $installOutput
}
return GetResult $true $Package $installOutput
}
function DockerValidation() {
$registry = " -e TARGET_REGISTRY=`'$($Package.registry)`'"
$folder = " -e TARGET_FOLDER=`'$($Package.folder)`'"
$commandLine = "docker run --restart=on-failure:3 -e TARGET_PACKAGE='$($Package.name)'"
if ("$($Package.registry)") {
$commandLine = "$commandLine$registry"
}
if ("$($Package.folder)") {
$commandLine = "$commandLine$folder"
}
$commandLine = "$commandLine $DocValidationImageId 2>&1"
$installOutput = Invoke-Expression $commandLine

Write-Host "npm install $($Package.name) --prefix $prefixDirectory $additionalParameters"
$installOutput = npm install $Package.name --prefix $prefixDirectory @additionalParameters 2>&1
if ($LASTEXITCODE -ne 0) {
LogWarning "Package install failed: $($Package.name)"
return GetResult $false $package $installOutput
# 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 $($Package.name) directly."
FallbackValidation
}
elseif ($LASTEXITCODE -ne 0) {
Write-Host $commandLine
LogWarning "Package $($Package.name) ref docs validation failed."
return GetResult $false $Package $installOutput
}
return GetResult $true $Package $installOutput
}

return GetResult $true $package $installOutput

if (-not $DocValidationImageId) {
FallbackValidation
}
else {
DockerValidation
}