Skip to content

Commit a4a569e

Browse files
Use default npmrc userprofile (#11045)
This pull request standardizes the creation and authentication of `.npmrc` files across multiple pipelines by introducing a new reusable template in `eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml`. It replaces older, package-specific implementations and updates various pipeline YAML files to use this new template, ensuring consistency and maintainability in how authenticated npm registry access is handled. **Centralization and Standardization of npm Authentication:** * Added a new, parameterized template `create-authenticated-npmrc.yml` in `eng/common/pipelines/templates/steps/` to create and authenticate `.npmrc` files for npm registry access. This template supports customizable parameters and sets up authentication using the `npmAuthenticate@0` task. * Updated multiple pipeline files (including `ci.yml`, `build-step.yml`, `test-step.yml`, `install.yml`, and others) to use the new common template for npm authentication, replacing direct script steps and previous template usages. [[1]](diffhunk://#diff-50653fd56ca297fe36cce63c692f3bb54b6e0c6ec4845cec48f3f9e31e03a89fR32-R34) [[2]](diffhunk://#diff-384e4a070f1a434ae1a958c73f40ac5fcd1c73bf88d9c343c6de05eab19be5ccR62-R63) [[3]](diffhunk://#diff-ebf19ec9b8a02e0eeb5ae1b56d102bf0c066de16b7882fbabe4c2ae107c62f4dR54-R55) [[4]](diffhunk://#diff-4e7c6928f4ef45999e81f7e57bbc59d49275f6496fd2cfa7476dc218555d23afR27) [[5]](diffhunk://#diff-9e0ecf3eecec8baef1816b5d44be550486a40f4738b975bffd2efd55e90c4512R45-R46) [[6]](diffhunk://#diff-00bf0f8f6f160e1195dda4724f0d3b1c6c097136b8d413c2be53c7484bd844bdR128-R130) [[7]](diffhunk://#diff-dda08ab39d3fa47345cdb9dc131fca57b121bd5630dbce9116b1838054b04232R389) **Cleanup and Removal of Redundant Code:** * Removed the old `create-authenticated-npmrc.yml` template from `eng/emitters/pipelines/templates/steps/`, which previously handled `.npmrc` creation and authentication in a less flexible manner. * Updated emitter pipeline stages to reference the new centralized template instead of the deleted emitter-specific version. These changes improve maintainability by reducing duplication, making it easier to update npm authentication logic in one place, and ensuring all pipelines use the same approach for authenticated npm registry access.
1 parent 89fb500 commit a4a569e

11 files changed

Lines changed: 78 additions & 30 deletions

File tree

‎eng/common/pipelines/ci.yml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ extends:
2929
displayName: Install Node.js
3030
inputs:
3131
versionSpec: 22.x
32-
- script: npm install -g pnpm # Pnpm manage-package-manager-versions will respect packageManager field
32+
33+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
34+
35+
- pwsh: |
36+
$packageJson = Get-Content -Raw "$(Build.SourcesDirectory)/package.json" | ConvertFrom-Json
37+
npm install -g $packageJson.packageManager # Pnpm manage-package-manager-versions will respect packageManager field
3338
displayName: Install pnpm
3439
3540
- script: pnpm install
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
parameters:
2+
- name: npmrcPath
3+
type: string
4+
# When empty, defaults to the agent user's .npmrc ($HOME/.npmrc on
5+
# Linux/macOS, %USERPROFILE%\.npmrc on Windows) so every subsequent
6+
# npm / pnpm / npx call in the job inherits the registry + auth.
7+
default: ""
8+
- name: registryUrl
9+
type: string
10+
default: "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js/npm/registry/"
11+
- name: CustomCondition
12+
type: string
13+
default: succeeded()
14+
- name: ServiceConnection
15+
type: string
16+
default: ""
17+
18+
steps:
19+
- pwsh: |
20+
$npmrcPath = '${{ parameters.npmrcPath }}'
21+
if (-not $npmrcPath) { $npmrcPath = Join-Path $HOME '.npmrc' }
22+
23+
Write-Host "Creating .npmrc file $npmrcPath for registry ${{ parameters.registryUrl }}"
24+
$parentFolder = Split-Path -Path $npmrcPath -Parent
25+
26+
if ($parentFolder -and -not (Test-Path $parentFolder)) {
27+
Write-Host "Creating folder $parentFolder"
28+
New-Item -Path $parentFolder -ItemType Directory | Out-Null
29+
}
30+
31+
"registry=${{ parameters.registryUrl }}" | Out-File $npmrcPath
32+
Write-Host "##vso[task.setvariable variable=resolvedNpmrcPath]$npmrcPath"
33+
displayName: "Create .npmrc"
34+
condition: ${{ parameters.CustomCondition }}
35+
36+
- task: npmAuthenticate@0
37+
displayName: Authenticate .npmrc
38+
condition: ${{ parameters.CustomCondition }}
39+
inputs:
40+
workingFile: $(resolvedNpmrcPath)
41+
azureDevOpsServiceConnection: ${{ parameters.ServiceConnection }}

‎eng/emitters/pipelines/templates/stages/emitter-stages.yml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,7 @@ stages:
326326
displayName: Download build artifacts
327327

328328
# Always publish to internal feed
329-
- template: /eng/emitters/pipelines/templates/steps/create-authenticated-npmrc.yml
330-
parameters:
331-
npmrcPath: $(buildArtifactsPath)/packages/.npmrc
332-
registryUrl: https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js/npm/registry/
329+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
333330

334331
# publish to devops feed
335332
- pwsh: |
@@ -389,6 +386,7 @@ stages:
389386
inputs:
390387
versionSpec: "22.x"
391388
checkLatest: true
389+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
392390
- script: npm ci
393391
displayName: Install emitter dependencies for playground bundle
394392
workingDirectory: $(Build.SourcesDirectory)/${{ parameters.PackagePath }}

‎eng/emitters/pipelines/templates/steps/build-step.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ steps:
5959
6060
- checkout: self
6161

62+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
63+
6264
- task: NodeTool@0
6365
displayName: Install Node.js
6466
retryCountOnTaskFailure: 3

‎eng/emitters/pipelines/templates/steps/create-authenticated-npmrc.yml‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎eng/emitters/pipelines/templates/steps/test-step.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ parameters:
5151
steps:
5252
- checkout: self
5353

54+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
55+
5456
- download: current
5557
artifact: ${{ parameters.BuildArtifactName }}
5658
displayName: Download build artifacts

‎eng/tsp-core/pipelines/jobs/cli/build-tsp-cli.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ jobs:
4242
# - script: npm install -g bun
4343
# displayName: Install bun
4444

45+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
46+
4547
- script: npm install -g pnpm
4648
displayName: Install pnpm
4749

‎eng/tsp-core/pipelines/templates/install.yml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ steps:
2424
inputs:
2525
version: 8.0.x
2626

27-
- script: npm install -g pnpm # Pnpm manage-package-manager-versions will respect packageManager field
27+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
28+
29+
- pwsh: |
30+
$packageJson = Get-Content -Raw "$(Build.SourcesDirectory)/package.json" | ConvertFrom-Json
31+
npm install -g $packageJson.packageManager # Pnpm manage-package-manager-versions will respect packageManager field
2832
displayName: Install pnpm
33+
2934
- script: pnpm config set store-dir ${{ parameters.pnpmStorePath }}
3035
displayName: Setup pnpm cache dir
3136

‎packages/http-client-csharp/eng/pipeline/publish.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ extends:
125125
npm_config_cache: $(Agent.TempDirectory)/npm-cache
126126
steps:
127127
- checkout: self
128+
129+
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
130+
128131
- pwsh: |
129132
# Determine the TypeSpec PR URL
130133
$repoUrl = '$(Build.Repository.Uri)'.TrimEnd('/')

‎packages/http-client-csharp/eng/scripts/Test-Packages.ps1‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ try {
3636
if ($GenerationChecks) {
3737
Set-StrictMode -Version 1
3838

39+
$packageJson = Get-Content -Raw "..\..\package.json" | ConvertFrom-Json
40+
3941
Write-Host "Installing pnpm" -ForegroundColor Cyan
40-
Invoke-LoggedCommand "npm install -g pnpm" -GroupOutput
42+
Invoke-LoggedCommand "npm install -g $($packageJson.packageManager)" -GroupOutput
4143

4244
Write-Host "Setting up workspace" -ForegroundColor Cyan
4345
Invoke-LoggedCommand "pnpm setup:min" $packageRoot/../..

0 commit comments

Comments
 (0)