Skip to content

Add npm.latest.gitlab-ci.yml to only run `publish` if necessary

Nathan Friend requested to merge nfriend-only-run-publish-if into master

What does this MR do?

Adds a npm.latest.gitlab-ci.yml CI template - an updated version of npm.gitlab-ci.yml that only runs npm publish if the version in package.json has not already been published. It does this by running npm view "${NPM_PACKAGE_NAME}" version, which returns the latest published version of the NPM package, and compares this to the version specified in package.json. npm publish is only run if they differ.

In addition, if no changes have been made to package.json, the entire publish job is skipped.

This MR also makes some other improvements:

  • Simplifies the pipeline down to a single publish job
  • Fixes #227508 (closed) by making use of the CI_API_V4_URL environment variable
  • Remove the step that checks for the existence of a package.json, since the job now includes changes: [package.json]
  • Remove instance-level API authentication from .npmrc since only the project-level authentication is necessary now (see #220985 (closed))

Example job logs

Below are some different scenarios demonstrated in a test project:

When the version in package.json is bumped

Successfully published version 2.0.18 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/875609396

When package.json is changed, but the version is not changed

2.0.18 is already the latest version, so no new version has been published.

https://gitlab.com/nfriend/npm-test-project/-/jobs/875614049

When package.json is not changed

The publish job is not run.

Example commit: nfriend/npm-test-project@7deee368

Notice that no pipeline was created for this commit (since publish is the only job in the pipeline, and it wasn't run).

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

The publish 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/875616256

When an error occurs while publishing

The pipeline will now fail if an error is thrown while publishing. In the previous version of the template, these kinds of errors would be swallowed and the pipeline would succeed.

npm ERR! code E401
npm ERR! 401 Unauthorized - GET https://gitlab.com/api/v4/projects/18806182/packages/npm/@nfriend%2fnpm-test-project
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-11-26T18_39_40_874Z-debug.log
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1

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

Closes #288346 (closed)

Closes #227508 (closed)

Edited by Nathan Friend

Merge request reports