34
loading...
This website collects cookies to deliver better user experience
Packages & Registries
. As it blew my mind that such an option exists I decided to research it a little and use it for this javascript package if possible. package.json
you should scope your project cause Gitlab requires packages to be scoped.{
"name": "@scope/example-package-name",
"version": "1.0.0"
}
.npmrc
file or npm config set registry
we can tell npm where we want it to publish our package. Looks something like this://gitlab.example.com/api/v4/projects/${PROJECT_ID}/packages/npm/:_authToken=${GITLAB_DEPLOY_TOKEN}
npm publish
you should be able to see your package in the registry of your repository..gitlab-ci.yml
configuration that looks like this:stages:
- build
- test
- publish
build:
stage: build
only:
- tags
cache:
key: build-cache
paths:
- node_modules/
- lib/
- .npmrc
policy: push
script:
- echo "//gitlab.example.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">.npmrc
- docker run -v $(pwd):/app -v /home:/home -w="/app" -u="$(id -u):$(id -g)" -e HOME node:14 npm install
- docker run -v $(pwd):/app -v /home:/home -w="/app" -u="$(id -u):$(id -g)" -e HOME node:14 npm run build
test:
stage: test
only:
- tags
cache:
key: build-cache
paths:
- node_modules/
- lib/
- .npmrc
policy: pull
script:
- docker run -v $(pwd):/app -v /home:/home -w="/app" -u="$(id -u):$(id -g)" -e HOME node:14 npm run test
lint:
stage: test
only:
- tags
cache:
key: build-cache
paths:
- node_modules/
- lib/
- .npmrc
policy: pull
script:
- docker run -v $(pwd):/app -v /home:/home -w="/app" -u="$(id -u):$(id -g)" -e HOME node:14 npm run lint
publish:
stage: publish
only:
- tags
cache:
key: build-cache
paths:
- node_modules/
- lib/
- .npmrc
policy: pull
script:
- docker run -v $(pwd):/app -v /home:/home -w="/app" -u="$(id -u):$(id -g)" -e HOME node:14 npm version --no-git-tag-version ${CI_COMMIT_TAG}
- docker run -v $(pwd):/app -v /home:/home -w="/app" -u="$(id -u):$(id -g)" -e HOME node:14 npm publish
.npmrc
file that contains the path of the registry made by using the CI environment variablesnpm version --no-git-tag-version ${CI_COMMIT_TAG}
command. npm version
is a noisy command that tags and commits code if it detects a directory being a git repository so that's why we use --no-git-tag-version
here. As the stage was triggered by us tagging the code, we have the ${CI_COMMIT_TAG}
environment variable available to use for package versioning. After that we just publish the package.docker run
commands like shown. So... not the most elegant way of doing it.