Skip to content

Commit dba9bb2

Browse files
author
strausr
committed
fix: simplify npm token extraction with multiple fallback methods
1 parent 5c9ec06 commit dba9bb2

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

.github/workflows/release.yml

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -97,68 +97,56 @@ jobs:
9797
echo "=== Git tags ==="
9898
git tag
9999
100-
- name: Configure npm authentication for semantic-release
100+
- name: Extract npm token from .npmrc
101101
run: |
102-
echo "=== NPM Authentication Setup ==="
102+
echo "=== Extracting npm token ==="
103103
echo "NPM_CONFIG_USERCONFIG: $NPM_CONFIG_USERCONFIG"
104-
echo "NODE_AUTH_TOKEN is set: $([ -n "$NODE_AUTH_TOKEN" ] && echo 'yes' || echo 'no')"
105104
106-
# Check if .npmrc exists in temp location
105+
# Copy .npmrc to home directory first
107106
if [ -f "$NPM_CONFIG_USERCONFIG" ]; then
108-
echo "✓ Found .npmrc at $NPM_CONFIG_USERCONFIG"
109-
# Show first line only (without token) for debugging
110-
head -1 "$NPM_CONFIG_USERCONFIG" | sed 's/=.*/=***/' || true
111-
112-
# Copy to home directory
113107
mkdir -p ~/.npm
114108
cp "$NPM_CONFIG_USERCONFIG" ~/.npmrc
115109
echo "✓ Copied .npmrc to ~/.npmrc"
116110
117-
# Extract token - try multiple patterns
118-
TOKEN=""
119-
# Pattern 1: //registry.npmjs.org/:_authToken=TOKEN
120-
TOKEN=$(grep -oP '(?<=//registry\.npmjs\.org/:_authToken=).*' ~/.npmrc 2>/dev/null || echo "")
121-
# Pattern 2: _authToken=TOKEN (without registry prefix)
122-
if [ -z "$TOKEN" ]; then
123-
TOKEN=$(grep -oP '(?<=_authToken=).*' ~/.npmrc 2>/dev/null | head -1 || echo "")
111+
# Show .npmrc format (masked) for debugging
112+
echo "Contents of .npmrc (masked):"
113+
cat ~/.npmrc | sed 's/=.*/=***/' || true
114+
echo ""
115+
116+
# Extract token - the format should be: //registry.npmjs.org/:_authToken=TOKEN
117+
NPM_TOKEN=$(awk -F'=' '/_authToken/ {print $2}' ~/.npmrc | head -1 | tr -d '\n' || echo "")
118+
119+
# If that didn't work, try grep
120+
if [ -z "$NPM_TOKEN" ]; then
121+
NPM_TOKEN=$(grep -oP '(?<=_authToken=).*' ~/.npmrc | head -1 | tr -d '\n' || echo "")
124122
fi
125-
# Pattern 3: Check if it's using NODE_AUTH_TOKEN variable
126-
if [ -z "$TOKEN" ] && [ -n "$NODE_AUTH_TOKEN" ]; then
127-
TOKEN="$NODE_AUTH_TOKEN"
128-
echo "Using NODE_AUTH_TOKEN directly"
123+
124+
# If still empty, try without regex (simple sed)
125+
if [ -z "$NPM_TOKEN" ]; then
126+
NPM_TOKEN=$(grep '_authToken' ~/.npmrc | sed 's/.*_authToken=//' | tr -d '\n' || echo "")
129127
fi
130128
131-
if [ -n "$TOKEN" ]; then
132-
# Mask token in logs
133-
echo "::add-mask::$TOKEN"
134-
echo "NPM_TOKEN=$TOKEN" >> $GITHUB_ENV
135-
echo "✓ NPM_TOKEN configured (masked in logs)"
129+
if [ -n "$NPM_TOKEN" ]; then
130+
echo "::add-mask::$NPM_TOKEN"
131+
echo "NPM_TOKEN=$NPM_TOKEN" >> $GITHUB_ENV
132+
echo "✓ NPM_TOKEN extracted and set"
136133
else
137-
echo "✗ Warning: Could not extract token from .npmrc"
138-
echo "Contents of ~/.npmrc (first 3 lines, tokens masked):"
139-
head -3 ~/.npmrc | sed 's/=.*/=***/' || true
134+
echo "✗ Error: Could not extract token from .npmrc"
135+
echo "Full .npmrc content (tokens will be masked in actual output):"
136+
cat ~/.npmrc || true
137+
exit 1
140138
fi
141139
else
142-
echo "✗ Warning: NPM_CONFIG_USERCONFIG file not found"
143-
echo "Trying to use NODE_AUTH_TOKEN directly..."
144-
if [ -n "$NODE_AUTH_TOKEN" ]; then
145-
echo "::add-mask::$NODE_AUTH_TOKEN"
146-
echo "NPM_TOKEN=$NODE_AUTH_TOKEN" >> $GITHUB_ENV
147-
echo "✓ NPM_TOKEN set from NODE_AUTH_TOKEN"
148-
else
149-
echo "✗ Error: No npm authentication found"
150-
fi
140+
echo "✗ Error: .npmrc file not found at $NPM_CONFIG_USERCONFIG"
141+
echo "This means actions/setup-node did not create the .npmrc file"
142+
echo "NODE_AUTH_TOKEN is set: $([ -n "$NODE_AUTH_TOKEN" ] && echo 'yes' || echo 'no')"
143+
exit 1
151144
fi
152145
153-
# Test npm auth
146+
# Test authentication
154147
echo ""
155148
echo "=== Testing npm authentication ==="
156-
if npm whoami --registry=https://registry.npmjs.org 2>/dev/null; then
157-
echo "✓ npm authentication successful"
158-
else
159-
echo "✗ npm authentication failed"
160-
echo "This might be OK if using OIDC - semantic-release will verify"
161-
fi
149+
npm whoami --registry=https://registry.npmjs.org && echo "✓ npm authentication successful" || echo "✗ npm authentication failed"
162150
163151
- name: Release
164152
env:

0 commit comments

Comments
 (0)