Skip to content

Update NPM .gitlab-ci.yml template to validate package scope

Nathan Friend requested to merge nfriend-validate-package-name into master

What does this MR do?

Adds a validation step to our NPM .gitlab-ci.yml template to warn the user if their package name is not properly scoped to the root namespace of the project.

In addition, this MR makes some other small improvements:

  • Validate that package.json exists, and throw a helpful error if not
  • Switch to using CI_PROJECT_ROOT_NAMESPACE (added in !34733 (merged))
  • Add inline documentation to explain the purpose of each step
  • Use default: instead of top-level image: as recommended by our documentation
  • Small updates for consistency throughout the script
    • Update all if [ ... ] to if [[ ... ]]
    • Update all variable interpolations from $VAR_NAME to ${VAR_NAME}
    • Update generation of .npmrc to use { cmd1; cmd2; } >> file syntax as recommended by ShellCheck
  • Add link to /~/packages page in job output

Example scenarios and output

Below are some examples of output from this .gitlab-ci.yml configuration in a test project:

When the repository does not have a package.json:

All jobs will fail with this message:

No package.json found! A package.json file is required to publish a package to GitLab's NPM registry.
For more information, see https://docs.gitlab.com/ee/user/packages/npm_registry/#creating-a-project

Full job output: https://gitlab.com/nfriend/npm-test-project/-/jobs/600026096

When the package's name is not properly scoped to the project's root namespace:

The validate_package_scope job will fail with this message:

Invalid package scope! Packages must be scoped in the root namespace of the project, e.g. "@nfriend/npm-test-project"
For more information, see https://docs.gitlab.com/ee/user/packages/npm_registry/#package-naming-convention

Full job output: https://gitlab.com/nfriend/npm-test-project/-/jobs/600029522

When the version specified in package.json has already been published:

The publish_package job will succeed with this message:

No new version of @nfriend/npm-test-project published. This is most likely because version 1.0.4 already exists in GitLab's NPM registry.

Full job output: https://gitlab.com/nfriend/npm-test-project/-/jobs/600035320

(This is behavior is unchanged by this MR.)

When the version specified in package.json has not yet been published:

The publish_package job will succeed with this message:

Successfully published version 1.0.7 of @nfriend/npm-test-project to GitLab's NPM registry: https://gitlab.com/nfriend/npm-test-project/-/packages

Full job output: https://gitlab.com/nfriend/npm-test-project/-/jobs/600063947

Edited by Nathan Friend

Merge request reports

Loading