Skip to content

Commit 35a5cfc

Browse files
authored
publish npm package to specified feed (dotnet#1168)
1 parent d9d9169 commit 35a5cfc

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

azure-pipelines.yml

+12
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,15 @@ stages:
395395
enableSymbolValidation: false
396396
# SourceLink improperly looks for generated files. See https://github.com/dotnet/arcade/issues/3069
397397
enableSourceLinkValidation: false
398+
399+
#---------------------------------------------------------------------------------------------------------------------#
400+
# NPM Publish #
401+
#---------------------------------------------------------------------------------------------------------------------#
402+
- ${{ if and(ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest')) }}:
403+
- template: eng/publish/publish-npm.yml
404+
parameters:
405+
packageName: microsoft-dotnet-interactive-*.tgz
406+
registryUrl: pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/npm/registry/
407+
registryUser: dnceng
408+
registryEmail: [email protected]
409+
publishingBranch: main

eng/package/PackNpmPackage.ps1

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ function Build-NpmPackage() {
1818
# pack
1919
Write-Host "Packing package"
2020
npm pack
21-
Copy-Item -Path (Join-Path (Get-Location) "dotnet-interactive-$packageVersionNumber.tgz") -Destination $outDir
21+
Copy-Item -Path (Join-Path (Get-Location) "microsoft-dotnet-interactive-$packageVersionNumber.tgz") -Destination $outDir
2222
}
2323

2424
try {
2525
. "$PSScriptRoot\PackUtilities.ps1"
2626

27-
# create destination
27+
# copy publish scripts
2828
EnsureCleanDirectory -location $outDir
29+
Copy-Item -Path $PSScriptRoot\..\publish\* -Destination $outDir -Recurse
2930

3031
Build-NpmPackage
3132
}

eng/publish/PublishNPMPackage.ps1

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[CmdletBinding(PositionalBinding = $false)]
2+
param (
3+
[string]$packageName,
4+
[string]$registryUrl,
5+
[string]$registryUser,
6+
[string]$registryEmail,
7+
[string]$currentBranch,
8+
[string]$publishingBranch,
9+
[string]$artifactDirectory,
10+
[string]$registryPublishToken
11+
)
12+
13+
Set-StrictMode -version 2.0
14+
$ErrorActionPreference = "Stop"
15+
16+
try {
17+
# create .npmrc with package feed
18+
$registryPublishTokenBase64 = [Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($registryPublishToken))
19+
$npmrcContents = "
20+
; begin auth token
21+
registry=https://$registryUrl
22+
username=$registryUser
23+
email=$registryEmail
24+
_password=$registryPublishTokenBase64
25+
_accessToken=$registryPublishToken
26+
; end auth token"
27+
$npmrcContents | Out-File "$artifactDirectory/.npmrc"
28+
29+
# publish to feed
30+
if (($currentBranch -Eq $publishingBranch) -Or ($currentBranch -Eq "refs/heads/$publishingBranch")) {
31+
$singlePackageName = Get-ChildItem "$artifactDirectory/$packageName" | Select-Object -First 1
32+
Write-Host "Publishing $singlePackageName to $registryUrl"
33+
npm publish "$singlePackageName" --access public
34+
if ($LASTEXITCODE -ne 0) {
35+
exit $LASTEXITCODE
36+
}
37+
}
38+
else {
39+
Write-Host "Branch '$currentBranch' does not match publishing branch '$publishingBranch', skipping publish."
40+
}
41+
}
42+
catch {
43+
Write-Host $_
44+
Write-Host $_.Exception
45+
Write-Host $_.ScriptStackTrace
46+
exit 1
47+
}

eng/publish/publish-npm.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
parameters:
2+
packageName: ''
3+
registryUrl: ''
4+
publishingBranch: ''
5+
registryUser: ''
6+
registryEmail: ''
7+
8+
stages:
9+
- stage: publish
10+
dependsOn: build
11+
displayName: Publish NPM package to feed
12+
jobs:
13+
- job: Publish_NPM
14+
pool:
15+
vmImage: windows-latest
16+
variables:
17+
- group: AzureDevOps-Artifact-Feeds-Pats
18+
- name: NodeJSVersion
19+
value: '12.16.1'
20+
steps:
21+
- task: NodeTool@0
22+
displayName: Add NodeJS/npm
23+
inputs:
24+
versionSpec: $(NodeJSVersion)
25+
- task: DownloadBuildArtifacts@0
26+
displayName: Download build NPM artifacts
27+
inputs:
28+
buildType: current
29+
artifactName: npm
30+
downloadPath: $(Build.ArtifactStagingDirectory)
31+
- task: PowerShell@2
32+
displayName: Publish NPM package
33+
inputs:
34+
filePath: $(Build.SourcesDirectory)/eng/publish/PublishNPMPackage.ps1
35+
arguments: -packageName "${{ parameters.packageName }}" -registryUrl "${{ parameters.registryUrl }}" -currentBranch "$env:BUILD_SOURCEBRANCH" -publishingBranch "${{ parameters.publishingBranch }}" -artifactDirectory "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/npm" -registryPublishToken "$env:PUBLISH_TOKEN" -registryUser "${{ parameters.registryUser }}" -registryEmail "${{ parameters.registryEmail }}"
36+
workingDirectory: '$(Build.ArtifactStagingDirectory)/npm'
37+
pwsh: true
38+
env:
39+
PUBLISH_TOKEN: $(dn-bot-all-orgs-artifact-feeds-rw)

src/dotnet-interactive-npm/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dist/
22
node_modules/
33

4-
dotnet-interactive-*.tgz
4+
microsoft-dotnet-interactive-*.tgz

src/dotnet-interactive-npm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "dotnet-interactive",
2+
"name": "@microsoft/dotnet-interactive",
33
"displayName": ".NET Interactive Notebooks",
44
"//description": "During build, the description gets the git SHA appended to it.",
55
"description": ".NET Interactive Notebook APIs for NodeJS.",

0 commit comments

Comments
 (0)