diff --git a/README.md b/README.md index cb2e984..65d3623 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ jobs: | `org-id` | No | — | Organization ID for multi-org accounts | | `workspace-id` | No | — | Workspace ID for multi-workspace accounts | | `verify-connection` | No | `false` | Run `devhelm auth me` after setup to verify credentials | -| `node-version` | No | `20` | Node.js version. Set to `''` to skip if you manage Node yourself | +| `node-version` | No | `20` | Node.js version to install **only if** none is detected on PATH (or if detected version is `<18`). Existing Node `>=18` is preserved as-is. Set to `''` to skip auto-install entirely | ## Outputs @@ -88,12 +88,16 @@ jobs: ## How it works -1. **Node.js** — ensures Node >= 18 is available (auto-installs via `actions/setup-node` unless you set `node-version: ''`) +1. **Node.js** — detects an existing `node` on `PATH`; if it's `>=18`, the action keeps it and skips `actions/setup-node` entirely. Otherwise it installs `node-version` (default `20`) via `actions/setup-node`. 2. **Cache** — restores `~/.npm` cache keyed on OS + CLI version 3. **Install** — runs `npm install -g devhelm@` 4. **Environment** — exports `DEVHELM_API_TOKEN`, `DEVHELM_API_URL`, `DEVHELM_ORG_ID`, `DEVHELM_WORKSPACE_ID` for all subsequent steps 5. **Verify** — optionally runs `devhelm auth me` to fail fast on bad credentials +### Compatibility with caller-installed Node + +If your workflow already runs `actions/setup-node@v4` (or otherwise pins a Node version) before this action, **your Node version is preserved**. The action only installs Node when none is on `PATH` or the detected major is `<18`. You no longer need to set `node-version: ''` to avoid being silently downgraded. + ## Security - **Never hardcode tokens** in workflow files. Use [GitHub Secrets](https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions). @@ -102,7 +106,7 @@ jobs: ## Requirements -- **Node.js >= 18** — the action auto-installs Node 20 by default. Set `node-version: ''` if your workflow already provides Node. +- **Node.js >= 18** — if not present (or the detected version is `<18`), the action installs Node 20 by default. Any pre-existing Node `>=18` set up by an earlier step is left untouched. Set `node-version: ''` to skip the install fallback entirely (the action will then error if Node is missing). - **npm** — ships with Node.js. ## License diff --git a/action.yml b/action.yml index 7858dee..4971cfc 100644 --- a/action.yml +++ b/action.yml @@ -28,7 +28,7 @@ inputs: required: false default: 'false' node-version: - description: 'Node.js version to use if actions/setup-node was not called before this action. Set to empty string to skip auto-setup.' + description: 'Node.js version to install IF none is detected on PATH (or if detected version is <18). Existing Node >=18 is preserved. Set to empty string to skip auto-setup entirely. Default: 20.' required: false default: '20' @@ -43,8 +43,26 @@ outputs: runs: using: composite steps: + - name: Detect existing Node.js + id: node-detect + shell: bash + run: | + if command -v node >/dev/null 2>&1; then + MAJOR=$(node -v | sed 's/^v\([0-9]*\).*/\1/') + if [ "$MAJOR" -ge 18 ]; then + echo "found=true" >> "$GITHUB_OUTPUT" + echo "version=$(node -v)" >> "$GITHUB_OUTPUT" + echo "Node $(node -v) detected (>=18); skipping setup-node." + exit 0 + fi + echo "Node $(node -v) detected but <18; will install Node ${{ inputs.node-version }}." + else + echo "No Node.js detected on PATH; will install Node ${{ inputs.node-version }}." + fi + echo "found=false" >> "$GITHUB_OUTPUT" + - name: Ensure Node.js is available - if: inputs.node-version != '' + if: steps.node-detect.outputs.found != 'true' && inputs.node-version != '' uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} @@ -52,12 +70,12 @@ runs: - name: Validate Node.js shell: bash run: | - if ! command -v node &>/dev/null; then - echo "::error::Node.js is required. Either set input 'node-version' or run actions/setup-node before this action." + if ! command -v node >/dev/null 2>&1; then + echo "::error::Node.js is required. Either set input 'node-version' (default: 20) or run actions/setup-node before this action." exit 1 fi - NODE_MAJOR=$(node -v | sed 's/v\([0-9]*\).*/\1/') - if (( NODE_MAJOR < 18 )); then + NODE_MAJOR=$(node -v | sed 's/^v\([0-9]*\).*/\1/') + if [ "$NODE_MAJOR" -lt 18 ]; then echo "::error::DevHelm CLI requires Node.js >= 18 (found $(node -v))" exit 1 fi