Commit dd72134e authored by Marin Jankovski's avatar Marin Jankovski

Move the trigger-build script into one location to serve both omnibus and cloud-native triggers.

parent 434efdac
...@@ -264,10 +264,10 @@ package-and-qa: ...@@ -264,10 +264,10 @@ package-and-qa:
<<: *single-script-job <<: *single-script-job
variables: variables:
<<: *single-script-job-variables <<: *single-script-job-variables
SCRIPT_NAME: trigger-build-omnibus SCRIPT_NAME: trigger-build
retry: 0 retry: 0
script: script:
- ./$SCRIPT_NAME - ./$SCRIPT_NAME omnibus
when: manual when: manual
only: only:
- //@gitlab-org/gitlab-ce - //@gitlab-org/gitlab-ce
......
...@@ -487,7 +487,7 @@ Style/EmptyLiteral: ...@@ -487,7 +487,7 @@ Style/EmptyLiteral:
- 'lib/gitlab/fogbugz_import/importer.rb' - 'lib/gitlab/fogbugz_import/importer.rb'
- 'lib/gitlab/git/diff_collection.rb' - 'lib/gitlab/git/diff_collection.rb'
- 'lib/gitlab/gitaly_client.rb' - 'lib/gitlab/gitaly_client.rb'
- 'scripts/trigger-build-omnibus' - 'scripts/trigger-build'
- 'spec/features/merge_requests/versions_spec.rb' - 'spec/features/merge_requests/versions_spec.rb'
- 'spec/helpers/merge_requests_helper_spec.rb' - 'spec/helpers/merge_requests_helper_spec.rb'
- 'spec/lib/gitlab/request_context_spec.rb' - 'spec/lib/gitlab/request_context_spec.rb'
......
...@@ -4,44 +4,44 @@ require 'net/http' ...@@ -4,44 +4,44 @@ require 'net/http'
require 'json' require 'json'
require 'cgi' require 'cgi'
module Omnibus module Trigger
PROJECT_PATH = 'gitlab-org/omnibus-gitlab'.freeze OMNIBUS_PROJECT_PATH = 'gitlab-org/omnibus-gitlab'.freeze
CNG_PROJECT_PATH = 'gitlab-org/build/CNG'.freeze
TOKEN = ENV['BUILD_TRIGGER_TOKEN']
class Trigger def self.ee?
TOKEN = ENV['BUILD_TRIGGER_TOKEN'] ENV['CI_PROJECT_NAME'] == 'gitlab-ee' || File.exist?('CHANGELOG-EE.md')
TRIGGERER = ENV['CI_PROJECT_NAME'] end
class Omnibus
def initialize def initialize
@uri = URI("https://gitlab.com/api/v4/projects/#{CGI.escape(Omnibus::PROJECT_PATH)}/trigger/pipeline") @uri = URI("https://gitlab.com/api/v4/projects/#{CGI.escape(Trigger::OMNIBUS_PROJECT_PATH)}/trigger/pipeline")
@params = env_params.merge(file_params).merge(token: TOKEN) @params = env_params.merge(file_params).merge(token: Trigger::TOKEN)
end end
def invoke! def invoke!
res = Net::HTTP.post_form(@uri, @params) res = Net::HTTP.post_form(@uri, @params)
id = JSON.parse(res.body)['id'] id = JSON.parse(res.body)['id']
project = Trigger::OMNIBUS_PROJECT_PATH
if id if id
puts "Triggered https://gitlab.com/#{Omnibus::PROJECT_PATH}/pipelines/#{id}" puts "Triggered https://gitlab.com/#{project}/pipelines/#{id}"
puts "Waiting for downstream pipeline status" puts "Waiting for downstream pipeline status"
else else
raise "Trigger failed! The response from the trigger is: #{res.body}" raise "Trigger failed! The response from the trigger is: #{res.body}"
end end
Omnibus::Pipeline.new(id) Trigger::Pipeline.new(project, id)
end end
private private
def ee?
TRIGGERER == 'gitlab-ee' || File.exist?('CHANGELOG-EE.md')
end
def env_params def env_params
{ {
"ref" => ENV["OMNIBUS_BRANCH"] || "master", "ref" => ENV["OMNIBUS_BRANCH"] || "master",
"variables[GITLAB_VERSION]" => ENV["CI_COMMIT_SHA"], "variables[GITLAB_VERSION]" => ENV["CI_COMMIT_SHA"],
"variables[ALTERNATIVE_SOURCES]" => true, "variables[ALTERNATIVE_SOURCES]" => true,
"variables[ee]" => ee? ? 'true' : 'false', "variables[ee]" => Trigger.ee? ? 'true' : 'false',
"variables[TRIGGERED_USER]" => ENV["GITLAB_USER_NAME"], "variables[TRIGGERED_USER]" => ENV["GITLAB_USER_NAME"],
"variables[TRIGGER_SOURCE]" => "https://gitlab.com/gitlab-org/#{ENV['CI_PROJECT_NAME']}/-/jobs/#{ENV['CI_JOB_ID']}" "variables[TRIGGER_SOURCE]" => "https://gitlab.com/gitlab-org/#{ENV['CI_PROJECT_NAME']}/-/jobs/#{ENV['CI_JOB_ID']}"
} }
...@@ -56,13 +56,77 @@ module Omnibus ...@@ -56,13 +56,77 @@ module Omnibus
end end
end end
class CNG
def initialize
@uri = URI("https://gitlab.com/api/v4/projects/#{CGI.escape(Trigger::CNG_PROJECT_PATH)}/trigger/pipeline")
@ref_name = ENV['CI_COMMIT_REF_NAME']
@username = ENV['GITLAB_USER_NAME']
@project_name = ENV['CI_PROJECT_NAME']
@job_id = ENV['CI_JOB_ID']
@params = env_params.merge(file_params).merge(token: Trigger::TOKEN)
end
#
# Trigger a pipeline
#
def invoke!
res = Net::HTTP.post_form(@uri, @params)
id = JSON.parse(res.body)['id']
project = Trigger::CNG_PROJECT_PATH
if id
puts "Triggered https://gitlab.com/#{project}/pipelines/#{id}"
puts "Waiting for downstream pipeline status"
else
raise "Trigger failed! The response from the trigger is: #{res.body}"
end
Trigger::Pipeline.new(project, id)
end
private
def env_params
params = {
"ref" => ENV["CNG_BRANCH"] || "master",
"variables[TRIGGERED_USER]" => @username,
"variables[TRIGGER_SOURCE]" => "https://gitlab.com/gitlab-org/#{@project_name}/-/jobs/#{@job_id}"
}
if Trigger.ee?
params["variables[GITLAB_EE_VERSION]"] = @ref_name
params["variables[EE_PIPELINE]"] = 'true'
else
params["variables[GITLAB_CE_VERSION]"] = @ref_name
params["variables[CE_PIPELINE]"] = 'true'
end
params
end
# Read version files from all components
def file_params
Dir.glob("*_VERSION").each_with_object({}) do |version_file, params|
raw_version = File.read(version_file).strip
# if the version matches semver format, treat it as a tag and prepend `v`
version = if raw_version =~ Regexp.compile(/^\d+\.\d+\.\d+(-rc\d+)?(-ee)?$/)
"v#{raw_version}"
else
raw_version
end
params["variables[#{version_file}]"] = version
end
end
end
class Pipeline class Pipeline
INTERVAL = 60 # seconds INTERVAL = 60 # seconds
MAX_DURATION = 3600 * 3 # 3 hours MAX_DURATION = 3600 * 3 # 3 hours
def initialize(id) def initialize(project, id)
@start = Time.now.to_i @start = Time.now.to_i
@uri = URI("https://gitlab.com/api/v4/projects/#{CGI.escape(Omnibus::PROJECT_PATH)}/pipelines/#{id}") @uri = URI("https://gitlab.com/api/v4/projects/#{CGI.escape(project)}/pipelines/#{id}")
end end
def wait! def wait!
...@@ -74,10 +138,10 @@ module Omnibus ...@@ -74,10 +138,10 @@ module Omnibus
print "." print "."
sleep INTERVAL sleep INTERVAL
when :success when :success
puts "Omnibus pipeline succeeded in #{duration} minutes!" puts "Pipeline succeeded in #{duration} minutes!"
break break
else else
raise "Omnibus pipeline did not succeed!" raise "Pipeline did not succeed!"
end end
STDOUT.flush STDOUT.flush
...@@ -105,4 +169,13 @@ module Omnibus ...@@ -105,4 +169,13 @@ module Omnibus
end end
end end
Omnibus::Trigger.new.invoke!.wait! case ARGV[0]
when 'omnibus'
Trigger::Omnibus.new.invoke!.wait!
when 'cng'
Trigger::CNG.new.invoke!.wait!
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"
end
#!/usr/bin/env ruby
require 'gitlab'
#
# Configure credentials to be used with gitlab gem
#
Gitlab.configure do |config|
config.endpoint = 'https://gitlab.com/api/v4'
end
#
# The remote project
#
GITLAB_CNG_REPO = 'gitlab-org/build/CNG'.freeze
def ee?
ENV['CI_PROJECT_NAME'] == 'gitlab-ee' || File.exist?('CHANGELOG-EE.md')
end
def read_file_version(filename)
raw_version = File.read(filename).strip
# if the version matches semver format, treat it as a tag and prepend `v`
if raw_version =~ Regexp.compile(/^\d+\.\d+\.\d+(-rc\d+)?(-ee)?$/)
"v#{raw_version}"
else
raw_version
end
end
def params
params = {
'GITLAB_SHELL_VERSION' => read_file_version('GITLAB_SHELL_VERSION'),
'GITALY_VERSION' => read_file_version('GITALY_SERVER_VERSION'),
'TRIGGERED_USER' => ENV['GITLAB_USER_NAME'],
'TRIGGER_SOURCE' => "https://gitlab.com/gitlab-org/#{ENV['CI_PROJECT_NAME']}/-/jobs/#{ENV['CI_JOB_ID']}"
}
if ee?
params['EE_PIPELINE'] = 'true'
params['GITLAB_EE_VERSION'] = ENV['CI_COMMIT_REF_NAME']
else
params['CE_PIPELINE'] = 'true'
params['GITLAB_CE_VERSION'] = ENV['CI_COMMIT_REF_NAME']
end
params
end
#
# Trigger a pipeline
#
def trigger_pipeline
# Create the cross project pipeline using CI_JOB_TOKEN
pipeline = Gitlab.run_trigger(GITLAB_CNG_REPO, ENV['CI_JOB_TOKEN'], 'master', params)
puts "Triggered https://gitlab.com/#{GITLAB_CNG_REPO}/pipelines/#{pipeline.id}"
end
trigger_pipeline
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