Commit 58486918 authored by Nick Thomas's avatar Nick Thomas

Create environments when the build referencing them is created

parent 93a03cd9
...@@ -10,18 +10,29 @@ module Ci ...@@ -10,18 +10,29 @@ module Ci
end end
end end
def project
pipeline.project
end
private private
def create_build(build_attributes) def create_build(build_attributes)
build_attributes = build_attributes.merge( build_attributes = build_attributes.merge(
pipeline: pipeline, pipeline: pipeline,
project: pipeline.project, project: project,
ref: pipeline.ref, ref: pipeline.ref,
tag: pipeline.tag, tag: pipeline.tag,
user: current_user, user: current_user,
trigger_request: trigger_request trigger_request: trigger_request
) )
pipeline.builds.create(build_attributes) build = pipeline.builds.create(build_attributes)
# Create the environment before the build starts. This sets its slug and
# makes it available as an environment variable
project.environments.find_or_create_by(name: build.expanded_environment_name) if
build.has_environment?
build
end end
def new_builds def new_builds
......
...@@ -223,7 +223,7 @@ describe Environment, models: true do ...@@ -223,7 +223,7 @@ describe Environment, models: true do
"foo-" => "foo" + SUFFIX, "foo-" => "foo" + SUFFIX,
}.each do |name, matcher| }.each do |name, matcher|
it "returns a slug matching #{matcher}, given #{name}" do it "returns a slug matching #{matcher}, given #{name}" do
slug = described_class.new(name: name).generate_clean_name slug = described_class.new(name: name).generate_slug
expect(slug).to match(/\A#{matcher}\z/) expect(slug).to match(/\A#{matcher}\z/)
end end
......
...@@ -210,5 +210,22 @@ describe Ci::CreatePipelineService, services: true do ...@@ -210,5 +210,22 @@ describe Ci::CreatePipelineService, services: true do
expect(result.manual_actions).not_to be_empty expect(result.manual_actions).not_to be_empty
end end
end end
context 'with environment' do
before do
config = YAML.dump(deploy: { environment: { name: "review/$CI_BUILD_REF_NAME" }, script: 'ls' })
stub_ci_pipeline_yaml_file(config)
end
it 'creates the environment' do
result = execute(ref: 'refs/heads/master',
before: '00000000',
after: project.commit.id,
commits: [{ message: 'some msg' }])
expect(result).to be_persisted
expect(Environment.find_by(name: "review/master")).not_to be_nil
end
end
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