Commit 3dc7230e authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '23671-add-variables-to-ci-services' into 'master'

Resolve "support all options from `docker-compose.yml` in gitlab runner services"

See merge request gitlab-org/gitlab!72025
parents 10bd9a66 898efb30
...@@ -260,6 +260,7 @@ test: ...@@ -260,6 +260,7 @@ test:
| `entrypoint` | no | 9.4 |Command or script to execute as the container's entrypoint. It's translated to Docker's `--entrypoint` option while creating the container. The syntax is similar to [`Dockerfile`'s `ENTRYPOINT`](https://docs.docker.com/engine/reference/builder/#entrypoint) directive, where each shell token is a separate string in the array. | | `entrypoint` | no | 9.4 |Command or script to execute as the container's entrypoint. It's translated to Docker's `--entrypoint` option while creating the container. The syntax is similar to [`Dockerfile`'s `ENTRYPOINT`](https://docs.docker.com/engine/reference/builder/#entrypoint) directive, where each shell token is a separate string in the array. |
| `command` | no | 9.4 |Command or script that should be used as the container's command. It's translated to arguments passed to Docker after the image's name. The syntax is similar to [`Dockerfile`'s `CMD`](https://docs.docker.com/engine/reference/builder/#cmd) directive, where each shell token is a separate string in the array. | | `command` | no | 9.4 |Command or script that should be used as the container's command. It's translated to arguments passed to Docker after the image's name. The syntax is similar to [`Dockerfile`'s `CMD`](https://docs.docker.com/engine/reference/builder/#cmd) directive, where each shell token is a separate string in the array. |
| `alias` (1) | no | 9.4 |Additional alias that can be used to access the service from the job's container. Read [Accessing the services](#accessing-the-services) for more information. | | `alias` (1) | no | 9.4 |Additional alias that can be used to access the service from the job's container. Read [Accessing the services](#accessing-the-services) for more information. |
| `variables` | no | 14.5 | Additional environment variables that are passed exclusively to the service. The syntax is the same as [Job Variables](../variables/index.md). |
(1) Alias support for the Kubernetes executor was [introduced](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2229) in GitLab Runner 12.8, and is only available for Kubernetes version 1.7 or later. (1) Alias support for the Kubernetes executor was [introduced](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2229) in GitLab Runner 12.8, and is only available for Kubernetes version 1.7 or later.
......
...@@ -6,6 +6,7 @@ module API ...@@ -6,6 +6,7 @@ module API
module JobRequest module JobRequest
class Service < Entities::Ci::JobRequest::Image class Service < Entities::Ci::JobRequest::Image
expose :alias, :command expose :alias, :command
expose :variables
end end
end end
end end
......
...@@ -4,7 +4,7 @@ module Gitlab ...@@ -4,7 +4,7 @@ module Gitlab
module Ci module Ci
module Build module Build
class Image class Image
attr_reader :alias, :command, :entrypoint, :name, :ports attr_reader :alias, :command, :entrypoint, :name, :ports, :variables
class << self class << self
def from_image(job) def from_image(job)
...@@ -33,6 +33,7 @@ module Gitlab ...@@ -33,6 +33,7 @@ module Gitlab
@entrypoint = image[:entrypoint] @entrypoint = image[:entrypoint]
@name = image[:name] @name = image[:name]
@ports = build_ports(image).select(&:valid?) @ports = build_ports(image).select(&:valid?)
@variables = build_variables(image)
end end
end end
...@@ -45,6 +46,12 @@ module Gitlab ...@@ -45,6 +46,12 @@ module Gitlab
def build_ports(image) def build_ports(image)
image[:ports].to_a.map { |port| ::Gitlab::Ci::Build::Port.new(port) } image[:ports].to_a.map { |port| ::Gitlab::Ci::Build::Port.new(port) }
end end
def build_variables(image)
image[:variables].to_a.map do |key, value|
{ key: key, value: value.to_s }
end
end
end end
end end
end end
......
...@@ -15,7 +15,7 @@ module Gitlab ...@@ -15,7 +15,7 @@ module Gitlab
include ::Gitlab::Config::Entry::Attributable include ::Gitlab::Config::Entry::Attributable
include ::Gitlab::Config::Entry::Configurable include ::Gitlab::Config::Entry::Configurable
ALLOWED_KEYS = %i[name entrypoint command alias ports].freeze ALLOWED_KEYS = %i[name entrypoint command alias ports variables].freeze
validations do validations do
validates :config, hash_or_string: true validates :config, hash_or_string: true
...@@ -32,6 +32,10 @@ module Gitlab ...@@ -32,6 +32,10 @@ module Gitlab
entry :ports, Entry::Ports, entry :ports, Entry::Ports,
description: 'Ports used to expose the service' description: 'Ports used to expose the service'
entry :variables, ::Gitlab::Ci::Config::Entry::Variables,
description: 'Environment variables available for this service.',
inherit: false
attributes :ports attributes :ports
def alias def alias
......
...@@ -443,7 +443,7 @@ FactoryBot.define do ...@@ -443,7 +443,7 @@ FactoryBot.define do
options do options do
{ {
image: { name: 'ruby:2.7', entrypoint: '/bin/sh' }, image: { name: 'ruby:2.7', entrypoint: '/bin/sh' },
services: ['postgres', { name: 'docker:stable-dind', entrypoint: '/bin/sh', command: 'sleep 30', alias: 'docker' }], services: ['postgres', { name: 'docker:stable-dind', entrypoint: '/bin/sh', command: 'sleep 30', alias: 'docker' }, { name: 'mysql:latest', variables: { MYSQL_ROOT_PASSWORD: 'root123.' } }],
script: %w(echo), script: %w(echo),
after_script: %w(ls date), after_script: %w(ls date),
artifacts: { artifacts: {
......
...@@ -218,9 +218,11 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -218,9 +218,11 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
expect(json_response['git_info']).to eq(expected_git_info) expect(json_response['git_info']).to eq(expected_git_info)
expect(json_response['image']).to eq({ 'name' => 'ruby:2.7', 'entrypoint' => '/bin/sh', 'ports' => [] }) expect(json_response['image']).to eq({ 'name' => 'ruby:2.7', 'entrypoint' => '/bin/sh', 'ports' => [] })
expect(json_response['services']).to eq([{ 'name' => 'postgres', 'entrypoint' => nil, expect(json_response['services']).to eq([{ 'name' => 'postgres', 'entrypoint' => nil,
'alias' => nil, 'command' => nil, 'ports' => [] }, 'alias' => nil, 'command' => nil, 'ports' => [], 'variables' => nil },
{ 'name' => 'docker:stable-dind', 'entrypoint' => '/bin/sh', { 'name' => 'docker:stable-dind', 'entrypoint' => '/bin/sh',
'alias' => 'docker', 'command' => 'sleep 30', 'ports' => [] }]) 'alias' => 'docker', 'command' => 'sleep 30', 'ports' => [], 'variables' => [] },
{ 'name' => 'mysql:latest', 'entrypoint' => nil,
'alias' => nil, 'command' => nil, 'ports' => [], 'variables' => [{ 'key' => 'MYSQL_ROOT_PASSWORD', 'value' => 'root123.' }] }])
expect(json_response['steps']).to eq(expected_steps) expect(json_response['steps']).to eq(expected_steps)
expect(json_response['artifacts']).to eq(expected_artifacts) expect(json_response['artifacts']).to eq(expected_artifacts)
expect(json_response['cache']).to eq(expected_cache) expect(json_response['cache']).to eq(expected_cache)
......
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