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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
87637693
Commit
87637693
authored
Sep 22, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce CI/CD variables collection class
parent
26607a16
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
132 additions
and
11 deletions
+132
-11
app/models/project_services/kubernetes_service.rb
app/models/project_services/kubernetes_service.rb
+9
-11
lib/gitlab/ci/variables/collection.rb
lib/gitlab/ci/variables/collection.rb
+60
-0
spec/lib/gitlab/ci/variables/collection_spec.rb
spec/lib/gitlab/ci/variables/collection_spec.rb
+63
-0
No files found.
app/models/project_services/kubernetes_service.rb
View file @
87637693
...
...
@@ -100,19 +100,17 @@ class KubernetesService < DeploymentService
def
predefined_variables
config
=
YAML
.
dump
(
kubeconfig
)
variables
=
[
{
key:
'KUBE_URL'
,
value:
api_url
,
public:
true
},
{
key:
'KUBE_TOKEN'
,
value:
token
,
public:
false
},
{
key:
'KUBE_NAMESPACE'
,
value:
actual_namespace
,
public:
true
},
{
key:
'KUBECONFIG'
,
value:
config
,
public:
false
,
file:
true
}
]
if
ca_pem
.
present?
variables
<<
{
key:
'KUBE_CA_PEM'
,
value:
ca_pem
,
public:
true
}
variables
<<
{
key:
'KUBE_CA_PEM_FILE'
,
value:
ca_pem
,
public:
true
,
file:
true
}
variables
=
Gitlab
::
Ci
::
Variables
::
Collection
.
new
.
tap
do
|
collection
|
collection
.
append
(
key:
'KUBE_URL'
,
value:
api_url
,
public:
true
)
collection
.
append
(
key:
'KUBE_TOKEN'
,
value:
token
,
public:
false
)
collection
.
append
(
key:
'KUBE_NAMESPACE'
,
value:
actual_namespace
,
public:
true
)
collection
.
append
(
key:
'KUBECONFIG'
,
value:
config
,
public:
false
,
file:
true
)
collection
.
append
(
key:
'KUBE_CA_PEM'
,
value:
ca_pem
,
public:
true
)
collection
.
append
(
key:
'KUBE_CA_PEM_FILE'
,
value:
ca_pem
,
public:
true
,
file:
true
)
end
variables
variables
.
to_hash
end
# Constructs a list of terminals from the reactive cache
...
...
lib/gitlab/ci/variables/collection.rb
0 → 100644
View file @
87637693
module
Gitlab
module
Ci
module
Variables
class
Collection
include
Enumerable
Variable
=
Struct
.
new
(
:key
,
:value
,
:public
,
:file
)
def
initialize
(
variables
=
[])
@variables
=
[]
variables
.
each
{
|
variable
|
append
(
variable
)
}
end
def
append
(
resource
)
@variables
.
append
(
fabricate
(
resource
))
end
def
each
@variables
.
each
{
|
variable
|
yield
variable
}
end
def
+
(
other
)
self
.
class
.
new
.
tap
do
|
collection
|
self
.
each
{
|
variable
|
collection
.
append
(
variable
)
}
other
.
each
{
|
variable
|
collection
.
append
(
variable
)
}
end
end
def
to_h
self
.
map
do
|
variable
|
variable
.
to_h
.
reject
do
|
key
,
value
|
key
==
:file
&&
value
==
false
end
end
end
alias_method
:to_hash
,
:to_h
private
def
fabricate
(
resource
)
case
resource
when
Hash
Variable
.
new
(
resource
.
fetch
(
:key
),
resource
.
fetch
(
:value
),
resource
.
fetch
(
:public
,
false
),
resource
.
fetch
(
:file
,
false
))
when
::
Ci
::
Variable
Variable
.
new
(
resource
.
key
,
resource
.
value
,
false
,
false
)
when
Collection
::
Variable
resource
.
dup
else
raise
ArgumentError
,
'Unknown CI/CD variable resource!'
end
end
end
end
end
end
spec/lib/gitlab/ci/variables/collection_spec.rb
0 → 100644
View file @
87637693
require
'spec_helper'
describe
Gitlab
::
Ci
::
Variables
::
Collection
do
describe
'.new'
do
it
'can be initialized with an array'
do
variable
=
{
key:
'SOME_VAR'
,
value:
'Some Value'
}
collection
=
described_class
.
new
([
variable
])
expect
(
collection
.
first
.
to_h
).
to
include
variable
end
it
'can be initialized without an argument'
do
expect
(
subject
).
to
be_none
end
end
describe
'#append'
do
it
'appends a hash'
do
subject
.
append
(
key:
'VARIABLE'
,
value:
'something'
)
expect
(
subject
).
to
be_one
end
it
'appends a Ci::Variable'
do
subject
.
append
(
build
(
:ci_variable
))
expect
(
subject
).
to
be_one
end
it
'appends an internal resource'
do
collection
=
described_class
.
new
([{
key:
'TEST'
,
value:
1
}])
subject
.
append
(
collection
.
first
)
expect
(
subject
).
to
be_one
end
end
describe
'#+'
do
it
'makes it possible to combine with an array'
do
collection
=
described_class
.
new
([{
key:
'TEST'
,
value:
1
}])
variables
=
[{
key:
'TEST'
,
value:
'something'
}]
expect
((
collection
+
variables
).
count
).
to
eq
2
end
it
'makes it possible to combine with another collection'
do
collection
=
described_class
.
new
([{
key:
'TEST'
,
value:
1
}])
other
=
described_class
.
new
([{
key:
'TEST'
,
value:
2
}])
expect
((
collection
+
other
).
count
).
to
eq
2
end
end
describe
'#to_hash'
do
it
'creates a hash / value mapping'
do
collection
=
described_class
.
new
([{
key:
'TEST'
,
value:
1
}])
expect
(
collection
.
to_hash
)
.
to
eq
[{
key:
'TEST'
,
value:
1
,
public:
false
}]
end
end
end
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