Commit 937d048d authored by Nathan Friend's avatar Nathan Friend Committed by Shinya Maeda

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

parent d81d4d41
publish:
image: node:latest
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_REF_NAME =~ /^v\d+\.\d+\.\d+.*$/
changes:
- package.json
script:
# If no .npmrc if included in the repo, generate a temporary one that is configured to publish to GitLab's NPM registry
- |
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
fi
- echo "Created the following .npmrc:"; cat .npmrc
# Extract a few values from package.json
- NPM_PACKAGE_NAME=$(node -p "require('./package.json').name")
- NPM_PACKAGE_VERSION=$(node -p "require('./package.json').version")
# 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
- |
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
# Compare the version in package.json to all published versions.
# If the package.json version has not yet been published, run `npm publish`.
- |
if [[ $(npm view "${NPM_PACKAGE_NAME}" versions) != *"'${NPM_PACKAGE_VERSION}'"* ]]; then
npm publish
echo "Successfully published version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} to GitLab's NPM registry: ${CI_PROJECT_URL}/-/packages"
else
echo "Version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} has already been published, so no new version has been published."
fi
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'npm.latest.gitlab-ci.yml' do
subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('npm.latest') }
describe 'the created pipeline' do
let_it_be(:user) { create(:admin) }
let(:repo_files) { { 'package.json' => '{}', 'README.md' => '' } }
let(:modified_files) { %w[package.json] }
let(:project) { create(:project, :custom_repo, files: repo_files) }
let(:pipeline_branch) { project.default_branch }
let(:pipeline_tag) { 'v1.2.1' }
let(:pipeline_ref) { pipeline_branch }
let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref ) }
let(:pipeline) { service.execute!(:push) }
let(:build_names) { pipeline.builds.pluck(:name) }
def create_branch(name:)
::Branches::CreateService.new(project, user).execute(name, project.default_branch)
end
def create_tag(name:)
::Tags::CreateService.new(project, user).execute(name, project.default_branch, nil)
end
before do
stub_ci_pipeline_yaml_file(template.content)
create_branch(name: pipeline_branch)
create_tag(name: pipeline_tag)
allow_any_instance_of(Ci::Pipeline).to receive(:modified_paths).and_return(modified_files)
end
shared_examples 'publish job created' do
it 'creates a pipeline with a single job: publish' do
expect(build_names).to eq(%w[publish])
end
end
shared_examples 'no pipeline created' do
it 'does not create a pipeline because the only job (publish) is not created' do
expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError, 'No stages / jobs for this pipeline.')
end
end
context 'on default branch' do
context 'when package.json has been changed' do
it_behaves_like 'publish job created'
end
context 'when package.json does not exist or has not been changed' do
let(:modified_files) { %w[README.md] }
it_behaves_like 'no pipeline created'
end
end
%w[v1.0.0 v2.1.0-alpha].each do |valid_version|
context "when the branch name is #{valid_version}" do
let(:pipeline_branch) { valid_version }
it_behaves_like 'publish job created'
end
context "when the tag name is #{valid_version}" do
let(:pipeline_tag) { valid_version }
let(:pipeline_ref) { pipeline_tag }
it_behaves_like 'publish job created'
end
end
%w[patch-1 my-feature-branch v1 v1.0 2.1.0].each do |invalid_version|
context "when the branch name is #{invalid_version}" do
let(:pipeline_branch) { invalid_version }
it_behaves_like 'no pipeline created'
end
context "when the tag name is #{invalid_version}" do
let(:pipeline_tag) { invalid_version }
let(:pipeline_ref) { pipeline_tag }
it_behaves_like 'no pipeline created'
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment