Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
a0990ff3
Commit
a0990ff3
authored
Jun 01, 2017
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify CreateDeploymentService so that it uses
methods directly from job, avoid duplicating the works.
parent
66edbc5e
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
111 deletions
+73
-111
app/models/ci/build.rb
app/models/ci/build.rb
+5
-5
app/services/create_deployment_service.rb
app/services/create_deployment_service.rb
+34
-44
app/workers/build_success_worker.rb
app/workers/build_success_worker.rb
+1
-10
spec/services/create_deployment_service_spec.rb
spec/services/create_deployment_service_spec.rb
+33
-52
No files found.
app/models/ci/build.rb
View file @
a0990ff3
...
@@ -138,6 +138,11 @@ module Ci
...
@@ -138,6 +138,11 @@ module Ci
ExpandVariables
.
expand
(
environment
,
simple_variables
)
if
environment
ExpandVariables
.
expand
(
environment
,
simple_variables
)
if
environment
end
end
def
expanded_environment_url
ExpandVariables
.
expand
(
environment_url
,
simple_variables
)
if
environment_url
end
def
ci_environment_url
def
ci_environment_url
expanded_environment_url
||
persisted_environment
&
.
external_url
expanded_environment_url
||
persisted_environment
&
.
external_url
end
end
...
@@ -526,11 +531,6 @@ module Ci
...
@@ -526,11 +531,6 @@ module Ci
variables
variables
end
end
def
expanded_environment_url
ExpandVariables
.
expand
(
environment_url
,
simple_variables
)
if
environment_url
end
def
environment_url
def
environment_url
options
.
dig
(
:environment
,
:url
)
options
.
dig
(
:environment
,
:url
)
end
end
...
...
app/services/create_deployment_service.rb
View file @
a0990ff3
class
CreateDeploymentService
<
BaseService
class
CreateDeploymentService
def
execute
(
deployable
=
nil
)
attr_reader
:job
delegate
:expanded_environment_name
,
:expanded_environment_url
,
:project
,
to: :job
def
initialize
(
job
)
@job
=
job
end
def
execute
return
unless
executable?
return
unless
executable?
ActiveRecord
::
Base
.
transaction
do
ActiveRecord
::
Base
.
transaction
do
@deployable
=
deployable
environment
.
external_url
=
expanded_environment_url
if
expanded_environment_url
environment
.
fire_state_event
(
action
)
@environment
=
environment
return
unless
environment
.
save
@environment
.
external_url
=
expanded_url
if
expanded_url
return
if
environment
.
stopped?
@environment
.
fire_state_event
(
action
)
return
unless
@environment
.
save
deploy
.
tap
(
&
:update_merge_request_metrics!
)
return
if
@environment
.
stopped?
deploy
.
tap
do
|
deployment
|
deployment
.
update_merge_request_metrics!
end
end
end
end
end
private
private
def
executable?
def
executable?
project
&&
name
.
present?
project
&&
job
.
environment
.
present?
end
end
def
deploy
def
deploy
project
.
deployments
.
create
(
project
.
deployments
.
create
(
environment:
@
environment
,
environment:
environment
,
ref:
params
[
:ref
]
,
ref:
job
.
ref
,
tag:
params
[
:tag
]
,
tag:
job
.
tag
,
sha:
params
[
:sha
]
,
sha:
job
.
sha
,
user:
current_
user
,
user:
job
.
user
,
deployable:
@deployable
,
deployable:
job
,
on_stop:
o
ptions
[
:on_stop
]
)
on_stop:
o
n_stop
)
end
end
def
environment
def
environment
@environment
||=
project
.
environments
.
find_or_create_by
(
name:
expanded_name
)
@environment
||=
end
project
.
environments
.
find_or_create_by
(
name:
expanded_environment_name
)
def
expanded_name
ExpandVariables
.
expand
(
name
,
variables
)
end
def
expanded_url
return
unless
url
@expanded_url
||=
ExpandVariables
.
expand
(
url
,
variables
)
end
def
name
params
[
:environment
]
end
def
url
options
[
:url
]
end
end
def
options
def
environment_
options
params
[
:options
]
||
{}
@environment_options
||=
job
.
options
[
:environment
]
||
{}
end
end
def
variables
def
on_stop
params
[
:variables
]
||
[
]
environment_options
[
:on_stop
]
end
end
def
action
def
action
options
[
:action
]
||
'start'
environment_
options
[
:action
]
||
'start'
end
end
end
end
app/workers/build_success_worker.rb
View file @
a0990ff3
...
@@ -11,15 +11,6 @@ class BuildSuccessWorker
...
@@ -11,15 +11,6 @@ class BuildSuccessWorker
private
private
def
create_deployment
(
build
)
def
create_deployment
(
build
)
service
=
CreateDeploymentService
.
new
(
CreateDeploymentService
.
new
(
build
).
execute
build
.
project
,
build
.
user
,
environment:
build
.
environment
,
sha:
build
.
sha
,
ref:
build
.
ref
,
tag:
build
.
tag
,
options:
build
.
options
.
to_h
[
:environment
],
variables:
build
.
variables
)
service
.
execute
(
build
)
end
end
end
end
spec/services/create_deployment_service_spec.rb
View file @
a0990ff3
require
'spec_helper'
require
'spec_helper'
describe
CreateDeploymentService
,
services:
true
do
describe
CreateDeploymentService
,
services:
true
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:service
)
{
described_class
.
new
(
project
,
user
,
params
)
}
describe
'#execute'
do
let
(
:options
)
{
nil
}
let
(
:options
)
{
nil
}
let
(
:params
)
do
{
let
(
:job
)
do
environment:
'production'
,
create
(
:ci_build
,
ref:
'master'
,
ref:
'master'
,
tag:
false
,
tag:
false
,
sha:
'97de212e80737a608d939f648d959671fb0a0142'
,
environment:
'production'
,
options:
options
options:
{
environment:
options
})
}
end
end
let
(
:project
)
{
job
.
project
}
let
(
:service
)
{
described_class
.
new
(
job
)
}
describe
'#execute'
do
subject
{
service
.
execute
}
subject
{
service
.
execute
}
context
'when no environments exist'
do
context
'when no environments exist'
do
...
@@ -83,13 +83,8 @@ describe CreateDeploymentService, services: true do
...
@@ -83,13 +83,8 @@ describe CreateDeploymentService, services: true do
end
end
context
'for environment with invalid name'
do
context
'for environment with invalid name'
do
let
(
:params
)
do
before
do
{
job
.
update
(
environment:
'name,with,commas'
)
environment:
'name,with,commas'
,
ref:
'master'
,
tag:
false
,
sha:
'97de212e80737a608d939f648d959671fb0a0142'
}
end
end
it
'does not create a new environment'
do
it
'does not create a new environment'
do
...
@@ -102,27 +97,20 @@ describe CreateDeploymentService, services: true do
...
@@ -102,27 +97,20 @@ describe CreateDeploymentService, services: true do
end
end
context
'when variables are used'
do
context
'when variables are used'
do
let
(
:params
)
do
let
(
:options
)
do
{
{
name:
'review-apps/$CI_COMMIT_REF_NAME'
,
environment:
'review-apps/$CI_COMMIT_REF_NAME'
,
url:
'http://$CI_COMMIT_REF_NAME.review-apps.gitlab.com'
}
ref:
'master'
,
end
tag:
false
,
sha:
'97de212e80737a608d939f648d959671fb0a0142'
,
before
do
options:
{
job
.
update
(
environment:
'review-apps/$CI_COMMIT_REF_NAME'
)
name:
'review-apps/$CI_COMMIT_REF_NAME'
,
url:
'http://$CI_COMMIT_REF_NAME.review-apps.gitlab.com'
},
variables:
[
{
key:
'CI_COMMIT_REF_NAME'
,
value:
'feature-review-apps'
}
]
}
end
end
it
'does create a new environment'
do
it
'does create a new environment'
do
expect
{
subject
}.
to
change
{
Environment
.
count
}.
by
(
1
)
expect
{
subject
}.
to
change
{
Environment
.
count
}.
by
(
1
)
expect
(
subject
.
environment
.
name
).
to
eq
(
'review-apps/
feature-review-apps
'
)
expect
(
subject
.
environment
.
name
).
to
eq
(
'review-apps/
master
'
)
expect
(
subject
.
environment
.
external_url
).
to
eq
(
'http://
feature-review-apps
.review-apps.gitlab.com'
)
expect
(
subject
.
environment
.
external_url
).
to
eq
(
'http://
master
.review-apps.gitlab.com'
)
end
end
it
'does create a new deployment'
do
it
'does create a new deployment'
do
...
@@ -130,7 +118,7 @@ describe CreateDeploymentService, services: true do
...
@@ -130,7 +118,7 @@ describe CreateDeploymentService, services: true do
end
end
context
'and environment exist'
do
context
'and environment exist'
do
let!
(
:environment
)
{
create
(
:environment
,
project:
project
,
name:
'review-apps/
feature-review-apps
'
)
}
let!
(
:environment
)
{
create
(
:environment
,
project:
project
,
name:
'review-apps/
master
'
)
}
it
'does not create a new environment'
do
it
'does not create a new environment'
do
expect
{
subject
}.
not_to
change
{
Environment
.
count
}
expect
{
subject
}.
not_to
change
{
Environment
.
count
}
...
@@ -139,8 +127,8 @@ describe CreateDeploymentService, services: true do
...
@@ -139,8 +127,8 @@ describe CreateDeploymentService, services: true do
it
'updates external url'
do
it
'updates external url'
do
subject
subject
expect
(
subject
.
environment
.
name
).
to
eq
(
'review-apps/
feature-review-apps
'
)
expect
(
subject
.
environment
.
name
).
to
eq
(
'review-apps/
master
'
)
expect
(
subject
.
environment
.
external_url
).
to
eq
(
'http://
feature-review-apps
.review-apps.gitlab.com'
)
expect
(
subject
.
environment
.
external_url
).
to
eq
(
'http://
master
.review-apps.gitlab.com'
)
end
end
it
'does create a new deployment'
do
it
'does create a new deployment'
do
...
@@ -150,7 +138,9 @@ describe CreateDeploymentService, services: true do
...
@@ -150,7 +138,9 @@ describe CreateDeploymentService, services: true do
end
end
context
'when project was removed'
do
context
'when project was removed'
do
let
(
:project
)
{
nil
}
before
do
job
.
update
(
project:
nil
)
end
it
'does not create deployment or environment'
do
it
'does not create deployment or environment'
do
expect
{
subject
}.
not_to
raise_error
expect
{
subject
}.
not_to
raise_error
...
@@ -250,15 +240,6 @@ describe CreateDeploymentService, services: true do
...
@@ -250,15 +240,6 @@ describe CreateDeploymentService, services: true do
end
end
describe
"merge request metrics"
do
describe
"merge request metrics"
do
let
(
:params
)
do
{
environment:
'production'
,
ref:
'master'
,
tag:
false
,
sha:
'97de212e80737a608d939f648d959671fb0a0142b'
}
end
let
(
:merge_request
)
{
create
(
:merge_request
,
target_branch:
'master'
,
source_branch:
'feature'
,
source_project:
project
)
}
let
(
:merge_request
)
{
create
(
:merge_request
,
target_branch:
'master'
,
source_branch:
'feature'
,
source_project:
project
)
}
context
"while updating the 'first_deployed_to_production_at' time"
do
context
"while updating the 'first_deployed_to_production_at' time"
do
...
@@ -273,8 +254,8 @@ describe CreateDeploymentService, services: true do
...
@@ -273,8 +254,8 @@ describe CreateDeploymentService, services: true do
end
end
it
"doesn't set the time if the deploy's environment is not 'production'"
do
it
"doesn't set the time if the deploy's environment is not 'production'"
do
staging_params
=
params
.
merg
e
(
environment:
'staging'
)
job
.
updat
e
(
environment:
'staging'
)
service
=
described_class
.
new
(
project
,
user
,
staging_params
)
service
=
described_class
.
new
(
job
)
service
.
execute
service
.
execute
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_nil
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_nil
...
@@ -298,7 +279,7 @@ describe CreateDeploymentService, services: true do
...
@@ -298,7 +279,7 @@ describe CreateDeploymentService, services: true do
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_like_time
(
time
)
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_like_time
(
time
)
# Current deploy
# Current deploy
service
=
described_class
.
new
(
project
,
user
,
params
)
service
=
described_class
.
new
(
job
)
Timecop
.
freeze
(
time
+
12
.
hours
)
{
service
.
execute
}
Timecop
.
freeze
(
time
+
12
.
hours
)
{
service
.
execute
}
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_like_time
(
time
)
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_like_time
(
time
)
...
@@ -318,7 +299,7 @@ describe CreateDeploymentService, services: true do
...
@@ -318,7 +299,7 @@ describe CreateDeploymentService, services: true do
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_nil
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_nil
# Current deploy
# Current deploy
service
=
described_class
.
new
(
project
,
user
,
params
)
service
=
described_class
.
new
(
job
)
Timecop
.
freeze
(
time
+
12
.
hours
)
{
service
.
execute
}
Timecop
.
freeze
(
time
+
12
.
hours
)
{
service
.
execute
}
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_nil
expect
(
merge_request
.
reload
.
metrics
.
first_deployed_to_production_at
).
to
be_nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment