Update NPM .gitlab-ci.yml template to validate package scope
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-levelimage:
as recommended by our documentation - Small updates for consistency throughout the script
- Update all
if [ ... ]
toif [[ ... ]]
- Update all variable interpolations from
$VAR_NAME
to${VAR_NAME}
- Update generation of
.npmrc
to use{ cmd1; cmd2; } >> file
syntax as recommended by ShellCheck
- Update all
- 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:
package.json
:
When the repository does not have a 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
is not properly scoped to the project's root namespace:
When the package's nameThe 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
package.json
has already been published:
When the version specified in 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.)
package.json
has not yet been published:
When the version specified in 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