Skip to content

Commit 5c9ec06

Browse files
author
strausr
committed
fix: improve npm token extraction with better debugging and fallbacks
1 parent b2bcdfd commit 5c9ec06

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

.github/workflows/release.yml

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,66 @@ jobs:
9999
100100
- name: Configure npm authentication for semantic-release
101101
run: |
102-
# actions/setup-node creates .npmrc in a temp location via NPM_CONFIG_USERCONFIG
103-
# We need to ensure semantic-release can find it
102+
echo "=== NPM Authentication Setup ==="
103+
echo "NPM_CONFIG_USERCONFIG: $NPM_CONFIG_USERCONFIG"
104+
echo "NODE_AUTH_TOKEN is set: $([ -n "$NODE_AUTH_TOKEN" ] && echo 'yes' || echo 'no')"
105+
106+
# Check if .npmrc exists in temp location
104107
if [ -f "$NPM_CONFIG_USERCONFIG" ]; then
105-
echo "Found npmrc at $NPM_CONFIG_USERCONFIG"
106-
# Copy to home directory for semantic-release to read
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
107113
mkdir -p ~/.npm
108114
cp "$NPM_CONFIG_USERCONFIG" ~/.npmrc
109-
echo "Copied .npmrc to ~/.npmrc"
115+
echo "Copied .npmrc to ~/.npmrc"
110116
111-
# Also extract token for NPM_TOKEN env var (semantic-release npm plugin requires it)
112-
# We mask the token value in logs for security
117+
# Extract token - try multiple patterns
118+
TOKEN=""
119+
# Pattern 1: //registry.npmjs.org/:_authToken=TOKEN
113120
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 "")
124+
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"
129+
fi
130+
114131
if [ -n "$TOKEN" ]; then
115-
echo "NPM_TOKEN=***" >> $GITHUB_ENV
132+
# Mask token in logs
116133
echo "::add-mask::$TOKEN"
117134
echo "NPM_TOKEN=$TOKEN" >> $GITHUB_ENV
118-
echo "NPM_TOKEN configured (masked in logs)"
135+
echo "NPM_TOKEN configured (masked in logs)"
119136
else
120-
echo "Warning: Could not extract token from .npmrc"
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
121140
fi
122141
else
123-
echo "Warning: NPM_CONFIG_USERCONFIG not found at $NPM_CONFIG_USERCONFIG"
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
124151
fi
125152
126-
# Test npm auth (without exposing token)
127-
echo "Testing npm authentication..."
128-
npm whoami --registry=https://registry.npmjs.org >/dev/null 2>&1 && echo "✓ npm authentication successful" || echo "✗ npm authentication failed"
153+
# Test npm auth
154+
echo ""
155+
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
129162
130163
- name: Release
131164
env:

0 commit comments

Comments
 (0)