Commit aeaf7510 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'refactor-trigger-build-script' into 'master'

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

See merge request gitlab-org/gitlab-ce!19837
parents 4791f771 dd72134e
......@@ -264,10 +264,10 @@ package-and-qa:
<<: *single-script-job
variables:
<<: *single-script-job-variables
SCRIPT_NAME: trigger-build-omnibus
SCRIPT_NAME: trigger-build
retry: 0
script:
- ./$SCRIPT_NAME
- ./$SCRIPT_NAME omnibus
when: manual
only:
- //@gitlab-org/gitlab-ce
......
......@@ -487,7 +487,7 @@ Style/EmptyLiteral:
- 'lib/gitlab/fogbugz_import/importer.rb'
- 'lib/gitlab/git/diff_collection.rb'
- 'lib/gitlab/gitaly_client.rb'
- 'scripts/trigger-build-omnibus'
- 'scripts/trigger-build'
- 'spec/features/merge_requests/versions_spec.rb'
- 'spec/helpers/merge_requests_helper_spec.rb'
- 'spec/lib/gitlab/request_context_spec.rb'
......
......@@ -4,44 +4,44 @@ require 'net/http'
require 'json'
require 'cgi'
module Omnibus
PROJECT_PATH = 'gitlab-org/omnibus-gitlab'.freeze
class Trigger
module Trigger
OMNIBUS_PROJECT_PATH = 'gitlab-org/omnibus-gitlab'.freeze
CNG_PROJECT_PATH = 'gitlab-org/build/CNG'.freeze
TOKEN = ENV['BUILD_TRIGGER_TOKEN']
TRIGGERER = ENV['CI_PROJECT_NAME']
def self.ee?
ENV['CI_PROJECT_NAME'] == 'gitlab-ee' || File.exist?('CHANGELOG-EE.md')
end
class Omnibus
def initialize
@uri = URI("https://gitlab.com/api/v4/projects/#{CGI.escape(Omnibus::PROJECT_PATH)}/trigger/pipeline")
@params = env_params.merge(file_params).merge(token: TOKEN)
@uri = URI("https://gitlab.com/api/v4/projects/#{CGI.escape(Trigger::OMNIBUS_PROJECT_PATH)}/trigger/pipeline")
@params = env_params.merge(file_params).merge(token: Trigger::TOKEN)
end
def invoke!
res = Net::HTTP.post_form(@uri, @params)
id = JSON.parse(res.body)['id']
project = Trigger::OMNIBUS_PROJECT_PATH
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"
else
raise "Trigger failed! The response from the trigger is: #{res.body}"
end
Omnibus::Pipeline.new(id)
Trigger::Pipeline.new(project, id)
end
private
def ee?
TRIGGERER == 'gitlab-ee' || File.exist?('CHANGELOG-EE.md')
end
def env_params
{
"ref" => ENV["OMNIBUS_BRANCH"] || "master",
"variables[GITLAB_VERSION]" => ENV["CI_COMMIT_SHA"],
"variables[ALTERNATIVE_SOURCES]" => true,
"variables[ee]" => ee? ? 'true' : 'false',
"variables[ee]" => Trigger.ee? ? 'true' : 'false',
"variables[TRIGGERED_USER]" => ENV["GITLAB_USER_NAME"],
"variables[TRIGGER_SOURCE]" => "https://gitlab.com/gitlab-org/#{ENV['CI_PROJECT_NAME']}/-/jobs/#{ENV['CI_JOB_ID']}"
}
......@@ -56,13 +56,77 @@ module Omnibus
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
INTERVAL = 60 # seconds
MAX_DURATION = 3600 * 3 # 3 hours
def initialize(id)
def initialize(project, id)
@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
def wait!
......@@ -74,10 +138,10 @@ module Omnibus
print "."
sleep INTERVAL
when :success
puts "Omnibus pipeline succeeded in #{duration} minutes!"
puts "Pipeline succeeded in #{duration} minutes!"
break
else
raise "Omnibus pipeline did not succeed!"
raise "Pipeline did not succeed!"
end
STDOUT.flush
......@@ -105,4 +169,13 @@ module Omnibus
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