Commit 0beccdc2 authored by Ugo Mignon's avatar Ugo Mignon Committed by Pierre Smeyers
Browse files

feat: add Corepack support with auto-detection

parent fff942b0
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
@@ -53,6 +53,21 @@ The Node.js template uses some global configuration used throughout all jobs.
| `project-dir` / `NODE_PROJECT_DIR`                           | Node project root directory                                                                                                                                                                                                                                      | `.`                                                                                                                                                                                             |
| `source-dir` / `NODE_SOURCE_DIR`                             | Sources directory                                                                                                                                                                                                                                                | `src`                                                                                                                                                                                           |
| `install-extra-opts` / `NODE_INSTALL_EXTRA_OPTS`             | Extra options to install project dependencies (either [`npm ci`](https://docs.npmjs.com/cli/ci.html/), [`yarn install`](https://yarnpkg.com/cli/install), [`pnpm install`](https://pnpm.io/cli/install) or [`bun install`](https://bun.com/docs/pm/cli/install)) | _none_                                                                                                                                                                                          |
| `corepack-policy` / `NODE_COREPACK_POLICY`                 | [Corepack](https://nodejs.org/api/corepack.html) support policy (one of `auto`, `enabled` or `disabled`) | `auto`                                           |

### Using Corepack

[Corepack](https://nodejs.org/api/corepack.html) ships with recent Node.js images and manages shims for Yarn and pnpm.

By default, the template will automatically enable Corepack if the `packageManager` field is detected in your `package.json`.
This automatic behavior can be overridden by setting explicitly the `corepack-policy` input / `NODE_COREPACK_POLICY` variable to `enabled` or `disabled`.

:warning: If the `corepack` command is not found in the image while trying to enable Corepack, the flag is ignored with a warning.

Examples:

- Component inputs: `corepack-policy: enabled`
- Legacy variables: `NODE_COREPACK_POLICY: "enabled"`

### Using scoped registries

@@ -214,7 +229,7 @@ Mocha may be either configured with CLI options of using separate Mocha and `nyc

Here is the required configuration with CLI options directly in the `package.json` file:

```json
```jsonc
  "scripts": {
    "test": "npm run mocha",
    "mocha": "nyc --report-dir=reports --reporter=text --reporter=lcovonly --reporter=cobertura mocha --reporter mocha-junit-reporter --reporter-option mochaFile=reports/node-test.xunit.xml test/*.js",
@@ -227,7 +242,7 @@ Here is the equivalent using separate config files:

- `package.json`:

  ```json
  ```jsonc
    "scripts": {
      "test": "npm run mocha",
      "mocha": "nyc mocha test/*.js",
@@ -441,7 +456,7 @@ Examples:

- for an unscoped package:

  ```json
  ```jsonc
  {
    "name": "my-package",
    // ...
@@ -454,7 +469,7 @@ Examples:

- for a scoped package:

  ```json
  ```jsonc
  {
    "name": "@acme/my-package",
    // ...
+9 −1
Original line number Diff line number Diff line
@@ -71,6 +71,14 @@
      "name": "NODE_INSTALL_EXTRA_OPTS",
      "description": "Extra options to install project dependencies (either [`npm ci`](https://docs.npmjs.com/cli/ci.html/), [`yarn install`](https://yarnpkg.com/cli/install), [`pnpm install`](https://pnpm.io/cli/install) or [`bun install`](https://bun.com/docs/pm/cli/install))",
      "advanced": true
    },
    {
      "name": "NODE_COREPACK_POLICY",
      "description": "[Corepack](https://nodejs.org/api/corepack.html) support policy",
      "type": "enum",
      "default": "auto",
      "values": ["auto", "enabled", "disabled"],
      "advanced": true
    }
  ],
  "features": [
+24 −0
Original line number Diff line number Diff line
@@ -121,6 +121,13 @@ spec:
    publish-args:
      description: npm [publish](https://docs.npmjs.com/cli/v8/commands/npm-publish) extra arguments - yarn [publish](https://classic.yarnpkg.com/lang/en/docs/cli/publish/) extra arguments - pnpm [publish](https://pnpm.io/cli/publish) extra arguments
      default: ''
    corepack-policy:
      description: "[Corepack](https://nodejs.org/api/corepack.html) support policy"
      default: auto
      options:
      - auto
      - enabled
      - disabled
---
# default workflow rules: Merge Request pipelines
.tbc-workflow-rules:
@@ -236,6 +243,8 @@ variables:
  # Publish
  NODE_PUBLISH_ENABLED: $[[ inputs.publish-enabled ]]
  NODE_PUBLISH_ARGS: $[[ inputs.publish-args ]]
  # Corepack control
  NODE_COREPACK_POLICY: $[[ inputs.corepack-policy ]]

  # default production ref name (pattern)
  PROD_REF: /^(master|main)$/
@@ -692,6 +701,21 @@ stages:
    - !reference [.node-scripts]
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - cd ${NODE_PROJECT_DIR}
    - |
      corepack_enabled="$NODE_COREPACK_POLICY"
      if [[ "$corepack_enabled" == "auto" ]]
      then
        # check whether 'packageManager' is set in package.json
        corepack_enabled=$(node -pe "require('./package.json').packageManager && 'enabled'")
      fi
      if [[ "$corepack_enabled" == "enabled" ]]; then
        if command -v corepack >/dev/null 2>&1; then
          log_info "Enabling Corepack..."
          corepack enable || log_warn "Failed enabling Corepack"
        else
          log_warn "Corepack command not found in image; ignored"
        fi
      fi
    - guess_node_manager_system
    # pnpm & bun should be installed before using it
    - if [ "$NODE_MANAGER" = "pnpm" ] || [ "$NODE_MANAGER" = "bun" ]; then check_manager_installation; fi