From e3fb0740228219433a4623dc0b9325785e23ae16 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Sun, 20 Nov 2016 20:43:50 +0100 Subject: [PATCH] Send credentials array with build data --- app/models/ci/build.rb | 11 +++++ ...nd-registry-address-with-build-payload.yml | 2 +- lib/ci/api/entities.rb | 8 ++-- lib/gitlab/ci/build/credentials.rb | 16 ++++++++ spec/requests/ci/api/builds_spec.rb | 40 ++++++++++++------- 5 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 lib/gitlab/ci/build/credentials.rb diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index bf5f92f8462..ac3fff3fb0c 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -448,8 +448,19 @@ module Ci ] end + def credentials + [build_container_registry_credentials].compact + end + private + def build_container_registry_credentials + return unless Gitlab.config.registry.enabled + + Gitlab::Ci::Build::Credentials.new('docker-registry', Gitlab.config.registry.host_port, + 'gitlab-ci-token', token) + end + def update_artifacts_size self.artifacts_size = if artifacts_file.exists? artifacts_file.size diff --git a/changelogs/unreleased/feature-send-registry-address-with-build-payload.yml b/changelogs/unreleased/feature-send-registry-address-with-build-payload.yml index bb2c1a0a4a7..db9bb2bc31f 100644 --- a/changelogs/unreleased/feature-send-registry-address-with-build-payload.yml +++ b/changelogs/unreleased/feature-send-registry-address-with-build-payload.yml @@ -1,4 +1,4 @@ --- -title: Send registry_url with build data to GitLab Runner +title: Send credentials (currently for registry only) with build data to GitLab Runner merge_request: 7474 author: diff --git a/lib/ci/api/entities.rb b/lib/ci/api/entities.rb index e00d91a6b45..792ff628b09 100644 --- a/lib/ci/api/entities.rb +++ b/lib/ci/api/entities.rb @@ -32,6 +32,10 @@ module Ci expose :artifacts_file, using: ArtifactFile, if: ->(build, _) { build.artifacts? } end + class BuildCredentials < Grape::Entity + expose :type, :url, :username, :password + end + class BuildDetails < Build expose :commands expose :repo_url @@ -51,9 +55,7 @@ module Ci expose :variables expose :depends_on_builds, using: Build - expose :registry_url, if: ->(_, _) { Gitlab.config.registry.enabled } do |_| - Gitlab.config.registry.host_port - end + expose :credentials, using: BuildCredentials end class Runner < Grape::Entity diff --git a/lib/gitlab/ci/build/credentials.rb b/lib/gitlab/ci/build/credentials.rb new file mode 100644 index 00000000000..14f9e8d7244 --- /dev/null +++ b/lib/gitlab/ci/build/credentials.rb @@ -0,0 +1,16 @@ +module Gitlab + module Ci + module Build + class Credentials + attr_accessor :type, :url, :username, :password + + def initialize(type, url, username, password) + @type = type + @url = url + @username = username + @password = password + end + end + end + end +end diff --git a/spec/requests/ci/api/builds_spec.rb b/spec/requests/ci/api/builds_spec.rb index bd4d3d61714..c5e498601e6 100644 --- a/spec/requests/ci/api/builds_spec.rb +++ b/spec/requests/ci/api/builds_spec.rb @@ -58,28 +58,38 @@ describe Ci::API::API do expect { register_builds }.to change { runner.reload.contacted_at } end - context 'when registry is enabled' do - before do - stub_container_registry_config(enabled: true, host_port: 'registry.example.com:5005') + context 'registry credentials' do + let(:registry_credentials) do + { 'type' => 'docker-registry', + 'url' => 'registry.example.com:5005', + 'username' => 'gitlab-ci-token', + 'password' => build.token } end - it 'sends registry_url key' do - register_builds info: { platform: :darwin } + context 'when registry is enabled' do + before do + stub_container_registry_config(enabled: true, host_port: 'registry.example.com:5005') + end - expect(json_response).to have_key('registry_url') - expect(json_response['registry_url']).to eq("registry.example.com:5005") - end - end + it 'sends registry credentials key' do + register_builds info: { platform: :darwin } - context 'when registry is disabled' do - before do - stub_container_registry_config(enabled: false, host_port: 'registry.example.com:5005') + expect(json_response).to have_key('credentials') + expect(json_response['credentials']).to include(registry_credentials) + end end - it 'does not send registry_url key' do - register_builds info: { platform: :darwin } + context 'when registry is disabled' do + before do + stub_container_registry_config(enabled: false, host_port: 'registry.example.com:5005') + end - expect(json_response).not_to have_key('registry_url') + it 'does not send registry credentials' do + register_builds info: { platform: :darwin } + + expect(json_response).to have_key('credentials') + expect(json_response['credentials']).not_to include(registry_credentials) + end end end end