Skip to content

Commit 20c4bd4

Browse files
authored
ci: use npm to publish directly rather than yarn (#2613)
1 parent 5962636 commit 20c4bd4

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

.ado/jobs/npm-publish.yml

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ jobs:
88
variables:
99
- name: BUILDSECMON_OPT_IN
1010
value: true
11+
- name: USE_YARN_FOR_PUBLISH
12+
value: false
13+
1114
timeoutInMinutes: 90
1215
cancelTimeoutInMinutes: 5
1316
templateContext:
@@ -50,8 +53,14 @@ jobs:
5053
# Disable Nightly publishing on the main branch
5154
- ${{ if endsWith(variables['Build.SourceBranchName'], '-stable') }}:
5255
- script: |
53-
yarn config set npmPublishRegistry "https://registry.npmjs.org"
54-
yarn config set npmAuthToken $(npmAuthToken)
56+
if [ "$(USE_YARN_FOR_PUBLISH)" = "true" ]; then
57+
echo "Configuring yarn for npm publishing"
58+
yarn config set npmPublishRegistry "https://registry.npmjs.org"
59+
yarn config set npmAuthToken $(npmAuthToken)
60+
else
61+
echo "Configuring npm for publishing"
62+
echo "//registry.npmjs.org/:_authToken=$(npmAuthToken)" > ~/.npmrc
63+
fi
5564
node .ado/scripts/prepublish-check.mjs --verbose --tag $(publishTag)
5665
displayName: Set and validate npm auth
5766
condition: and(succeeded(), eq(variables['publish_react_native_macos'], '1'))
@@ -65,19 +74,33 @@ jobs:
6574
condition: and(succeeded(), eq(variables['publish_react_native_macos'], '1'))
6675
6776
- script: |
77+
set -eox pipefail
6878
if [[ -f .rnm-publish ]]; then
6979
# https://github.com/microsoft/react-native-macos/issues/2580
7080
# `nx release publish` gets confused by the output of RNM's prepack script.
71-
# Let's call `yarn npm publish` directly instead on the packages we want to publish.
81+
# Let's call publish directly instead on the packages we want to publish.
7282
# yarn nx release publish --tag $(publishTag) --excludeTaskDependencies
73-
yarn ./packages/virtualized-lists npm publish --tag $(publishTag)
74-
yarn ./packages/react-native npm publish --tag $(publishTag)
83+
if [ "$(USE_YARN_FOR_PUBLISH)" = "true" ]; then
84+
echo "Publishing with yarn npm publish"
85+
yarn ./packages/virtualized-lists npm publish --tag $(publishTag)
86+
yarn ./packages/react-native npm publish --tag $(publishTag)
87+
else
88+
echo "Publishing with npm publish"
89+
(cd ./packages/virtualized-lists && npm publish --tag $(publishTag))
90+
(cd ./packages/react-native && npm publish --tag $(publishTag))
91+
fi
7592
fi
7693
displayName: Publish packages
7794
condition: and(succeeded(), eq(variables['publish_react_native_macos'], '1'))
7895
7996
- script: |
80-
yarn config unset npmAuthToken
81-
yarn config unset npmPublishRegistry
82-
displayName: Unset npm configuration
97+
if [ "$(USE_YARN_FOR_PUBLISH)" = "true" ]; then
98+
echo "Cleaning up yarn npm configuration"
99+
yarn config unset npmAuthToken || true
100+
yarn config unset npmPublishRegistry || true
101+
else
102+
echo "Cleaning up npm configuration"
103+
rm -f ~/.npmrc
104+
fi
105+
displayName: Remove NPM auth configuration
83106
condition: always()

.ado/scripts/prepublish-check.mjs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,10 @@ function verifyNpmAuth(registry = NPM_DEFEAULT_REGISTRY) {
160160
const whoamiArgs = ["npm", "whoami", "--publish"];
161161
const whoami = spawnSync("yarn", whoamiArgs, spawnOptions);
162162
if (whoami.status !== 0) {
163-
const stderr = whoami.stderr.toString().trim();
164-
const stdout = whoami.stdout.toString().trim();
165-
const errorOutput = stderr || stdout || 'No error message available';
166-
167-
// Yarn uses different error format
168-
if (errorOutput.includes("Invalid authentication") || errorOutput.includes("Failed with errors")) {
169-
throw new Error(`Invalid or missing auth token for registry: ${registry}`);
170-
}
163+
const errorOutput =
164+
whoami.stderr.toString().trim() ||
165+
whoami.stdout.toString().trim() ||
166+
'No error message available';
171167

172168
// Provide more context about the yarn authentication failure
173169
throw new Error(`Yarn authentication failed (exit code ${whoami.status}): ${errorOutput}`);

0 commit comments

Comments
 (0)