2021-06-28 06:07:45 +00:00
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/npm.gitlab-ci.yml
2021-06-02 21:10:00 +00:00
publish :
2020-06-22 18:08:47 +00:00
image : node:latest
2021-06-02 21:10:00 +00:00
stage : deploy
rules :
- if : $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_REF_NAME =~ /^v\d+\.\d+\.\d+.*$/
changes :
- package.json
script :
2021-10-05 12:12:58 +00:00
# If no .npmrc is included in the repo, generate a temporary one that is configured to publish to GitLab's NPM registry
2020-06-22 18:08:47 +00:00
- |
2021-06-02 21:10:00 +00:00
if [[ ! -f .npmrc ]]; then
echo 'No .npmrc found! Creating one now. Please review the following link for more information : https://docs.gitlab.com/ee/user/packages/npm_registry/index.html#project-level-npm-endpoint-1'
{
echo "@${CI_PROJECT_ROOT_NAMESPACE}:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
echo "${CI_API_V4_URL#http*:}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=\${CI_JOB_TOKEN}"
} >> .npmrc
2020-06-22 18:08:47 +00:00
fi
2021-06-02 21:10:00 +00:00
- echo "Created the following .npmrc:"; cat .npmrc
# Extract a few values from package.json
2020-06-22 18:08:47 +00:00
- NPM_PACKAGE_NAME=$(node -p "require('./package.json').name")
- NPM_PACKAGE_VERSION=$(node -p "require('./package.json').version")
2021-06-02 21:10:00 +00:00
# Validate that the package name is properly scoped to the project's root namespace.
# For more information, see https://docs.gitlab.com/ee/user/packages/npm_registry/#package-naming-convention
2020-06-22 18:08:47 +00:00
- |
if [[ ! $NPM_PACKAGE_NAME =~ ^@$CI_PROJECT_ROOT_NAMESPACE/ ]]; then
echo "Invalid package scope! Packages must be scoped in the root namespace of the project, e.g. \"@${CI_PROJECT_ROOT_NAMESPACE}/${CI_PROJECT_NAME}\""
echo 'For more information, see https://docs.gitlab.com/ee/user/packages/npm_registry/#package-naming-convention'
exit 1
fi
2021-06-02 21:10:00 +00:00
# Compare the version in package.json to all published versions.
# If the package.json version has not yet been published, run `npm publish`.
2020-06-17 21:08:24 +00:00
- |
2022-09-13 09:13:39 +00:00
if [[ "$(npm view ${NPM_PACKAGE_NAME} versions)" != *"'${NPM_PACKAGE_VERSION}'"* ]]; then
2021-06-02 21:10:00 +00:00
npm publish
2020-06-22 18:08:47 +00:00
echo "Successfully published version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} to GitLab's NPM registry: ${CI_PROJECT_URL}/-/packages"
2021-06-02 21:10:00 +00:00
else
echo "Version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} has already been published, so no new version has been published."
fi