diff options
Diffstat (limited to 'scripts/trigger-build')
-rwxr-xr-x | scripts/trigger-build | 103 |
1 files changed, 75 insertions, 28 deletions
diff --git a/scripts/trigger-build b/scripts/trigger-build index ab6dcc63e11..29d53609026 100755 --- a/scripts/trigger-build +++ b/scripts/trigger-build @@ -3,13 +3,6 @@ require 'gitlab' -# -# Configure credentials to be used with gitlab gem -# -Gitlab.configure do |config| - config.endpoint = 'https://gitlab.com/api/v4' -end - module Trigger def self.ee? # Support former project name for `dev` @@ -34,18 +27,13 @@ module Trigger ENV['GITLAB_BOT_MULTI_PROJECT_PIPELINE_POLLING_TOKEN'] end - def initialize - # gitlab-bot's token "GitLab multi-project pipeline polling" - Gitlab.private_token = self.class.access_token - end - def invoke!(post_comment: false, downstream_job_name: nil) pipeline_variables = variables puts "Triggering downstream pipeline on #{downstream_project_path}" puts "with variables #{pipeline_variables}" - pipeline = Gitlab.run_trigger( + pipeline = gitlab_client(:downstream).run_trigger( downstream_project_path, trigger_token, ref, @@ -54,23 +42,34 @@ module Trigger puts "Triggered downstream pipeline: #{pipeline.web_url}\n" puts "Waiting for downstream pipeline status" - Trigger::CommitComment.post!(pipeline) if post_comment + Trigger::CommitComment.post!(pipeline, gitlab_client(:upstream)) if post_comment downstream_job = if downstream_job_name - Gitlab.pipeline_jobs(downstream_project_path, pipeline.id).auto_paginate.find do |potential_job| + gitlab_client(:downstream).pipeline_jobs(downstream_project_path, pipeline.id).auto_paginate.find do |potential_job| potential_job.name == downstream_job_name end end if downstream_job - Trigger::Job.new(downstream_project_path, downstream_job.id) + Trigger::Job.new(downstream_project_path, downstream_job.id, gitlab_client(:downstream)) else - Trigger::Pipeline.new(downstream_project_path, pipeline.id) + Trigger::Pipeline.new(downstream_project_path, pipeline.id, gitlab_client(:downstream)) end end private + # Override to trigger and work with pipeline on different GitLab instance + # type: :downstream -> downstream build and pipeline status + # type: :upstream -> this project, e.g. for posting comments + def gitlab_client(type) + # By default, always use the same client + @gitlab_client ||= Gitlab.client( + endpoint: 'https://gitlab.com/api/v4', + private_token: self.class.access_token + ) + end + # Must be overridden def downstream_project_path raise NotImplementedError @@ -201,7 +200,7 @@ module Trigger class Docs < Base def self.access_token - ENV['DOCS_API_TOKEN'] + ENV['DOCS_PROJECT_API_TOKEN'] end SUCCESS_MESSAGE = <<~MSG @@ -232,7 +231,7 @@ module Trigger # Remove a remote branch in gitlab-docs. # def cleanup! - Gitlab.delete_branch(downstream_project_path, ref) + gitlab_client(:downstream).delete_branch(downstream_project_path, ref) puts "=> Remote branch '#{downstream_project_path}' deleted" end @@ -276,7 +275,7 @@ module Trigger end def create_remote_branch! - Gitlab.create_branch(downstream_project_path, ref, 'master') + gitlab_client(:downstream).create_branch(downstream_project_path, ref, 'master') puts "=> Remote branch '#{ref}' created" rescue Gitlab::Error::BadRequest puts "=> Remote branch '#{ref}' already exists!" @@ -289,7 +288,7 @@ module Trigger loop do sleep 1 puts "=> Waiting for pipeline to start..." - pipelines = Gitlab.pipelines(downstream_project_path, { ref: ref }) + pipelines = gitlab_client(:downstream).pipelines(downstream_project_path, { ref: ref }) break if pipelines.any? end @@ -297,7 +296,7 @@ module Trigger pipeline_id = pipelines.first.id # Cancel the pipeline - Gitlab.cancel_pipeline(downstream_project_path, pipeline_id) + gitlab_client(:downstream).cancel_pipeline(downstream_project_path, pipeline_id) end def display_success_message @@ -305,9 +304,53 @@ module Trigger end end + class DatabaseTesting < Base + def self.access_token + ENV['GITLABCOM_DATABASE_TESTING_ACCESS_TOKEN'] + end + + private + + def gitlab_client(type) + @gitlab_clients ||= { + downstream: Gitlab.client( + endpoint: 'https://ops.gitlab.net/api/v4', + private_token: self.class.access_token + ), + upstream: Gitlab.client( + endpoint: 'https://gitlab.com/api/v4', + private_token: Base.access_token + ) + } + + @gitlab_clients[type] + end + + def trigger_token + ENV['GITLABCOM_DATABASE_TESTING_TRIGGER_TOKEN'] + end + + def downstream_project_path + ENV['GITLABCOM_DATABASE_TESTING_PROJECT_PATH'] || 'gitlab-com/database-team/gitlab-com-database-testing' + end + + def extra_variables + { + # Use CI_MERGE_REQUEST_SOURCE_BRANCH_SHA for omnibus checkouts due to pipeline for merged results + # and fallback to CI_COMMIT_SHA for the `detached` pipelines. + 'GITLAB_COMMIT_SHA' => Trigger.non_empty_variable_value('CI_MERGE_REQUEST_SOURCE_BRANCH_SHA') || ENV['CI_COMMIT_SHA'], + 'TRIGGERED_USER_LOGIN' => ENV['GITLAB_USER_LOGIN'] + } + end + + def ref + ENV['GITLABCOM_DATABASE_TESTING_TRIGGER_REF'] || 'master' + end + end + class CommitComment - def self.post!(downstream_pipeline) - Gitlab.create_commit_comment( + def self.post!(downstream_pipeline, gitlab_client) + gitlab_client.create_commit_comment( ENV['CI_PROJECT_PATH'], Trigger.non_empty_variable_value('CI_MERGE_REQUEST_SOURCE_BRANCH_SHA') || ENV['CI_COMMIT_SHA'], "The [`#{ENV['CI_JOB_NAME']}`](#{ENV['CI_JOB_URL']}) job from pipeline #{ENV['CI_PIPELINE_URL']} triggered #{downstream_pipeline.web_url} downstream.") @@ -329,9 +372,10 @@ module Trigger unscoped_class_name.downcase end - def initialize(project, id) + def initialize(project, id, gitlab_client) @project = project @id = id + @gitlab_client = gitlab_client @start_time = Time.now.to_i end @@ -359,7 +403,7 @@ module Trigger end def status - Gitlab.public_send(self.class.gitlab_api_method_name, project, id).status.to_sym # rubocop:disable GitlabSecurity/PublicSend + gitlab_client.public_send(self.class.gitlab_api_method_name, project, id).status.to_sym # rubocop:disable GitlabSecurity/PublicSend rescue Gitlab::Error::Error => error puts "Ignoring the following error: #{error}" # Ignore GitLab API hiccups. If GitLab is really down, we'll hit the job @@ -369,7 +413,7 @@ module Trigger private - attr_reader :project, :id, :start_time + attr_reader :project, :id, :gitlab_client, :start_time end Job = Class.new(Pipeline) @@ -380,6 +424,8 @@ when 'omnibus' Trigger::Omnibus.new.invoke!(post_comment: true, downstream_job_name: 'Trigger:qa-test').wait! when 'cng' Trigger::CNG.new.invoke!.wait! +when 'gitlab-com-database-testing' + Trigger::DatabaseTesting.new.invoke! when 'docs' docs_trigger = Trigger::Docs.new @@ -395,5 +441,6 @@ when 'docs' else puts "Please provide a valid option: omnibus - Triggers a pipeline that builds the omnibus-gitlab package - cng - Triggers a pipeline that builds images used by the GitLab helm chart" + cng - Triggers a pipeline that builds images used by the GitLab helm chart + gitlab-com-database-testing - Triggers a pipeline that tests database changes on GitLab.com data" end |