Commit b9d5afc5 authored by Kerri Miller's avatar Kerri Miller

Merge branch 'zj-no-duplicate-deployments' into 'master'

Don't create duplicate deployments

See merge request gitlab-org/gitlab!47610
parents 6a570d83 13bd5ac8
......@@ -350,6 +350,13 @@ class Deployment < ApplicationRecord
File.join(environment.ref_path, 'deployments', iid.to_s)
end
def equal_to?(params)
ref == params[:ref] &&
tag == params[:tag] &&
sha == params[:sha] &&
status == params[:status]
end
private
def legacy_finished_at
......
......@@ -11,6 +11,8 @@ module Deployments
end
def execute
return last_deployment if last_deployment&.equal_to?(params)
environment.deployments.build(deployment_attributes).tap do |deployment|
# Deployment#change_status already saves the model, so we only need to
# call #save ourselves if no status is provided.
......@@ -36,5 +38,11 @@ module Deployments
on_stop: params[:on_stop]
}
end
private
def last_deployment
@environment.last_deployment
end
end
end
---
title: Deployments::CreateService executions are idempotent for duplicate params
merge_request: 47610
author:
type: added
......@@ -41,6 +41,27 @@ RSpec.describe Deployments::CreateService do
expect(service.execute).to be_persisted
end
context 'when the last deployment has the same parameters' do
let(:params) do
{
sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0',
ref: 'master',
tag: false,
status: 'success'
}
end
it 'does not create a new deployment' do
described_class.new(environment, user, params).execute
expect(Deployments::UpdateEnvironmentWorker).not_to receive(:perform_async)
expect(Deployments::LinkMergeRequestWorker).not_to receive(:perform_async)
expect(Deployments::ExecuteHooksWorker).not_to receive(:perform_async)
described_class.new(environment.reload, user, params).execute
end
end
end
describe '#deployment_attributes' do
......
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