Skip to content

Commit 5df6779

Browse files
author
strausr
committed
fix: improve npm token extraction with multiple locations and fallbacks
1 parent dba9bb2 commit 5df6779

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

.github/workflows/release.yml

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,52 +101,83 @@ jobs:
101101
run: |
102102
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')"
104105
105-
# Copy .npmrc to home directory first
106+
# Copy .npmrc to multiple locations
106107
if [ -f "$NPM_CONFIG_USERCONFIG" ]; then
108+
# Copy to home directory
107109
mkdir -p ~/.npm
108110
cp "$NPM_CONFIG_USERCONFIG" ~/.npmrc
109111
echo "✓ Copied .npmrc to ~/.npmrc"
110112
113+
# Also copy to repository root (semantic-release might look there)
114+
cp "$NPM_CONFIG_USERCONFIG" .npmrc
115+
echo "✓ Copied .npmrc to repository root"
116+
111117
# Show .npmrc format (masked) for debugging
118+
echo ""
112119
echo "Contents of .npmrc (masked):"
113120
cat ~/.npmrc | sed 's/=.*/=***/' || true
114121
echo ""
115122
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 "")
123+
# Extract token - try multiple methods
124+
NPM_TOKEN=""
125+
126+
# Method 1: awk
127+
NPM_TOKEN=$(awk -F'=' '/_authToken/ {print $2}' ~/.npmrc | head -1 | tr -d '\n\r' || echo "")
118128
119-
# If that didn't work, try grep
129+
# Method 2: grep with regex
120130
if [ -z "$NPM_TOKEN" ]; then
121-
NPM_TOKEN=$(grep -oP '(?<=_authToken=).*' ~/.npmrc | head -1 | tr -d '\n' || echo "")
131+
NPM_TOKEN=$(grep -oP '(?<=_authToken=).*' ~/.npmrc | head -1 | tr -d '\n\r' || echo "")
122132
fi
123133
124-
# If still empty, try without regex (simple sed)
134+
# Method 3: simple sed
125135
if [ -z "$NPM_TOKEN" ]; then
126-
NPM_TOKEN=$(grep '_authToken' ~/.npmrc | sed 's/.*_authToken=//' | tr -d '\n' || echo "")
136+
NPM_TOKEN=$(grep '_authToken' ~/.npmrc | sed 's/.*_authToken=//' | head -1 | tr -d '\n\r' || echo "")
127137
fi
128138
129-
if [ -n "$NPM_TOKEN" ]; then
139+
# Method 4: Check if it's a variable reference
140+
if [ -z "$NPM_TOKEN" ] || [[ "$NPM_TOKEN" == \$\{*\} ]]; then
141+
echo "Token appears to be a variable reference, using NODE_AUTH_TOKEN"
142+
if [ -n "$NODE_AUTH_TOKEN" ]; then
143+
NPM_TOKEN="$NODE_AUTH_TOKEN"
144+
fi
145+
fi
146+
147+
if [ -n "$NPM_TOKEN" ] && [ "$NPM_TOKEN" != "\${NODE_AUTH_TOKEN}" ]; then
130148
echo "::add-mask::$NPM_TOKEN"
131149
echo "NPM_TOKEN=$NPM_TOKEN" >> $GITHUB_ENV
132-
echo "✓ NPM_TOKEN extracted and set"
150+
echo "✓ NPM_TOKEN extracted and set (length: ${#NPM_TOKEN} chars)"
133151
else
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
152+
echo "✗ Error: Could not extract valid token from .npmrc"
153+
echo "Token value: '$NPM_TOKEN'"
154+
echo ""
155+
echo "Full .npmrc content (first 10 lines, tokens masked):"
156+
head -10 ~/.npmrc | sed 's/=.*/=***/' || true
137157
exit 1
138158
fi
139159
else
140160
echo "✗ Error: .npmrc file not found at $NPM_CONFIG_USERCONFIG"
141161
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
162+
echo "Checking if NODE_AUTH_TOKEN can be used directly..."
163+
if [ -n "$NODE_AUTH_TOKEN" ]; then
164+
echo "::add-mask::$NODE_AUTH_TOKEN"
165+
echo "NPM_TOKEN=$NODE_AUTH_TOKEN" >> $GITHUB_ENV
166+
echo "✓ Using NODE_AUTH_TOKEN directly as NPM_TOKEN"
167+
else
168+
exit 1
169+
fi
144170
fi
145171
146172
# Test authentication
147173
echo ""
148174
echo "=== Testing npm authentication ==="
149-
npm whoami --registry=https://registry.npmjs.org && echo "✓ npm authentication successful" || echo "✗ npm authentication failed"
175+
if npm whoami --registry=https://registry.npmjs.org 2>/dev/null; then
176+
echo "✓ npm authentication successful"
177+
else
178+
echo "✗ npm authentication test failed"
179+
echo "Note: This might be OK - semantic-release will verify the token"
180+
fi
150181
151182
- name: Release
152183
env:

0 commit comments

Comments
 (0)