summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2016-12-08 15:37:41 +0000
committerNick Thomas <nick@gitlab.com>2016-12-15 13:57:04 +0000
commit58486918fc12bbcc5139b6ca32461ad5e037497b (patch)
tree45f046807b9c15612acd66f0790662178b1e8fed
parent93a03cd92f6418fbeaf126c30c161ab40d377e94 (diff)
downloadgitlab-ce-58486918fc12bbcc5139b6ca32461ad5e037497b.tar.gz
Create environments when the build referencing them is created
-rw-r--r--app/services/ci/create_pipeline_builds_service.rb15
-rw-r--r--spec/models/environment_spec.rb2
-rw-r--r--spec/services/ci/create_pipeline_service_spec.rb17
3 files changed, 31 insertions, 3 deletions
diff --git a/app/services/ci/create_pipeline_builds_service.rb b/app/services/ci/create_pipeline_builds_service.rb
index 005014fa1de..b7da3f8e7eb 100644
--- a/app/services/ci/create_pipeline_builds_service.rb
+++ b/app/services/ci/create_pipeline_builds_service.rb
@@ -10,18 +10,29 @@ module Ci
end
end
+ def project
+ pipeline.project
+ end
+
private
def create_build(build_attributes)
build_attributes = build_attributes.merge(
pipeline: pipeline,
- project: pipeline.project,
+ project: project,
ref: pipeline.ref,
tag: pipeline.tag,
user: current_user,
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
def new_builds
diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb
index 706f1a5cd1c..97cbb093ed2 100644
--- a/spec/models/environment_spec.rb
+++ b/spec/models/environment_spec.rb
@@ -223,7 +223,7 @@ describe Environment, models: true do
"foo-" => "foo" + SUFFIX,
}.each do |name, matcher|
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/)
end
diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb
index 4aadd009f3e..ceaca96e25b 100644
--- a/spec/services/ci/create_pipeline_service_spec.rb
+++ b/spec/services/ci/create_pipeline_service_spec.rb
@@ -210,5 +210,22 @@ describe Ci::CreatePipelineService, services: true do
expect(result.manual_actions).not_to be_empty
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