Skip to content

Commit 3d7700a

Browse files
author
strausr
committed
fix: simplify npm token extraction - use token even if whoami fails
1 parent 9f18d51 commit 3d7700a

File tree

1 file changed

+11
-75
lines changed

1 file changed

+11
-75
lines changed

.github/workflows/release.yml

Lines changed: 11 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -97,96 +97,32 @@ jobs:
9797
echo "=== Git tags ==="
9898
git tag
9999
100-
- name: Extract npm token from .npmrc
100+
- name: Extract npm token for semantic-release
101101
run: |
102102
echo "=== Extracting npm token ==="
103-
echo "NPM_CONFIG_USERCONFIG: $NPM_CONFIG_USERCONFIG"
104-
echo "NODE_AUTH_TOKEN is set: $([ -n "$NODE_AUTH_TOKEN" ] && echo 'yes' || echo 'no')"
105103
106-
# Copy .npmrc to multiple locations
104+
# Copy .npmrc created by setup-node
107105
if [ -f "$NPM_CONFIG_USERCONFIG" ]; then
108-
# Copy to home directory
109-
mkdir -p ~/.npm
110106
cp "$NPM_CONFIG_USERCONFIG" ~/.npmrc
111107
echo "✓ Copied .npmrc to ~/.npmrc"
112108
113-
# Also copy to repository root (semantic-release might look there)
114-
cp "$NPM_CONFIG_USERCONFIG" .npmrc
115-
echo "✓ Copied .npmrc to repository root"
109+
# Extract token
110+
NPM_TOKEN=$(grep '_authToken' ~/.npmrc | sed 's/.*_authToken=//' | head -1 | tr -d '\n\r\t ' || echo "")
116111
117-
# Show .npmrc format (masked) for debugging
118-
echo ""
119-
echo "Contents of .npmrc (masked):"
120-
cat ~/.npmrc | sed 's/=.*/=***/' || true
121-
echo ""
122-
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 "")
128-
129-
# Method 2: grep with regex
130-
if [ -z "$NPM_TOKEN" ]; then
131-
NPM_TOKEN=$(grep -oP '(?<=_authToken=).*' ~/.npmrc | head -1 | tr -d '\n\r' || echo "")
132-
fi
133-
134-
# Method 3: simple sed
135-
if [ -z "$NPM_TOKEN" ]; then
136-
NPM_TOKEN=$(grep '_authToken' ~/.npmrc | sed 's/.*_authToken=//' | head -1 | tr -d '\n\r' || echo "")
137-
fi
138-
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-
# Clean up the token (remove any whitespace, newlines, etc.)
148-
NPM_TOKEN=$(echo "$NPM_TOKEN" | tr -d '\n\r\t ' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
149-
150-
if [ -n "$NPM_TOKEN" ] && [ "$NPM_TOKEN" != "\${NODE_AUTH_TOKEN}" ] && [ ${#NPM_TOKEN} -gt 10 ]; then
112+
if [ -n "$NPM_TOKEN" ] && [ ${#NPM_TOKEN} -gt 10 ]; then
151113
echo "::add-mask::$NPM_TOKEN"
152114
echo "NPM_TOKEN=$NPM_TOKEN" >> $GITHUB_ENV
153-
echo "✓ NPM_TOKEN extracted and set (length: ${#NPM_TOKEN} chars)"
154-
155-
# Also ensure .npmrc is properly formatted for npm
156-
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
157-
echo "registry=https://registry.npmjs.org/" >> ~/.npmrc
158-
echo "always-auth=true" >> ~/.npmrc
159-
echo "✓ Recreated .npmrc with proper format"
160-
else
161-
echo "✗ Error: Could not extract valid token from .npmrc"
162-
echo "Token length: ${#NPM_TOKEN} chars"
163-
echo "Token preview (first 10 chars): ${NPM_TOKEN:0:10}..."
115+
echo "✓ NPM_TOKEN extracted (length: ${#NPM_TOKEN} chars)"
164116
echo ""
165-
echo "Full .npmrc content (first 10 lines, tokens masked):"
166-
head -10 ~/.npmrc | sed 's/=.*/=***/' || true
167-
exit 1
168-
fi
169-
else
170-
echo "✗ Error: .npmrc file not found at $NPM_CONFIG_USERCONFIG"
171-
echo "This means actions/setup-node did not create the .npmrc file"
172-
echo "Checking if NODE_AUTH_TOKEN can be used directly..."
173-
if [ -n "$NODE_AUTH_TOKEN" ]; then
174-
echo "::add-mask::$NODE_AUTH_TOKEN"
175-
echo "NPM_TOKEN=$NODE_AUTH_TOKEN" >> $GITHUB_ENV
176-
echo "✓ Using NODE_AUTH_TOKEN directly as NPM_TOKEN"
117+
echo "Note: Token may not work for 'npm whoami' but should work for 'npm publish'"
118+
echo "semantic-release will attempt to use it for publishing"
177119
else
120+
echo "✗ Could not extract valid token"
178121
exit 1
179122
fi
180-
fi
181-
182-
# Test authentication
183-
echo ""
184-
echo "=== Testing npm authentication ==="
185-
if npm whoami --registry=https://registry.npmjs.org 2>/dev/null; then
186-
echo "✓ npm authentication successful"
187123
else
188-
echo "✗ npm authentication test failed"
189-
echo "Note: This might be OK - semantic-release will verify the token"
124+
echo "✗ .npmrc not found"
125+
exit 1
190126
fi
191127
192128
- name: Release

0 commit comments

Comments
 (0)