diff --git a/app/services/ci/create_pipeline_builds_service.rb b/app/services/ci/create_pipeline_builds_service.rb index 005014fa1ded2e94b350f06aad0ec2bf1613106f..b7da3f8e7eb04dbdf540608ded89e17119157c8d 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 706f1a5cd1c1c12843b73067a164f187fe967489..97cbb093ed264e9cd7b86a5879b72deb82aa3cc8 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 4aadd009f3ecf0316e6ae28b9de734f317a29e69..ceaca96e25b377a1ace0e89bef1de526261ea9d1 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