diff --git a/npm/npm-test/action.yaml b/npm/npm-test/action.yaml index 7eba525..47edd30 100644 --- a/npm/npm-test/action.yaml +++ b/npm/npm-test/action.yaml @@ -9,6 +9,14 @@ inputs: description: "Node.js version to use." required: false default: "20" + registries: + description: "Newline-separated list of registry URLs that require authentication." + required: false + default: "${{ github.server_url }}/api/packages/${{ github.repository_owner }}/npm/index.json" + apiKeys: + description: "Newline-separated list of API keys corresponding to registries." + required: false + default: "${{ github.token }}" testCommand: description: "Command to run tests. Default: npm test" required: false @@ -50,6 +58,12 @@ runs: node-version: ${{ inputs.nodeVersion }} cache: npm cache-dependency-path: ${{ inputs.workingDirectory }}/package-lock.json + - name: "Configure npm authentication" + if: ${{ inputs.registries != '' && inputs.apiKeys != '' }} + run: | + bash "${{ github.action_path }}/configure-auth.sh" add "${{ inputs.registries }}" "${{ inputs.apiKeys }}" + working-directory: ${{ inputs.workingDirectory }} + shell: bash - name: "Install dependencies" run: npm ci working-directory: ${{ inputs.workingDirectory }} @@ -74,3 +88,9 @@ runs: path: ${{ inputs.workingDirectory }}/${{ inputs.coverageDirectory }} if-no-files-found: warn retention-days: ${{ inputs.retention-days }} + - name: "Clean up npm authentication" + if: always() + run: | + bash "${{ github.action_path }}/configure-auth.sh" remove + working-directory: ${{ inputs.workingDirectory }} + shell: bash diff --git a/npm/npm-test/configure-auth.sh b/npm/npm-test/configure-auth.sh new file mode 100644 index 0000000..6926642 --- /dev/null +++ b/npm/npm-test/configure-auth.sh @@ -0,0 +1,42 @@ +#!/bin/bash +set -e + +ACTION="$1" +AUTH_URLS="$2" +API_KEYS="$3" + +if [ "$ACTION" == "add" ]; then + # Parse newline-separated URLs and tokens into arrays + IFS=$'\n' read -rd '' -a URLS <<< "$AUTH_URLS" || true + IFS=$'\n' read -rd '' -a KEYS <<< "$API_KEYS" || true + + # Verify arrays have matching lengths + if [ ${#URLS[@]} -ne ${#KEYS[@]} ]; then + echo "Error: Number of authUrls (${#URLS[@]}) does not match number of apiKeys (${#KEYS[@]})" + exit 1 + fi + + # Configure authentication for each registry + for i in "${!URLS[@]}"; do + URL="${URLS[$i]}" + KEY="${KEYS[$i]}" + + # Skip empty entries + if [ -z "$URL" ] || [ -z "$KEY" ]; then + continue + fi + + # Remove protocol from URL for npm config + REGISTRY_PATH=$(echo "$URL" | sed -E 's|https?://||') + + echo "Configuring authentication for: $URL" + npm config set --location=project "//${REGISTRY_PATH}:_authToken" "$KEY" + done +elif [ "$ACTION" == "remove" ]; then + # Clean up project .npmrc + rm -f .npmrc + echo "Removed npm authentication configuration" +else + echo "Error: Invalid action '$ACTION'. Use 'add' or 'remove'" + exit 1 +fi