2018-03-17 18:26:18 +05:30
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'gitlab'
|
|
|
|
|
|
|
|
#
|
|
|
|
# Configure credentials to be used with gitlab gem
|
|
|
|
#
|
|
|
|
Gitlab.configure do |config|
|
2018-12-05 23:21:45 +05:30
|
|
|
config.endpoint = 'https://gitlab.com/api/v4'
|
|
|
|
config.private_token = ENV["DOCS_API_TOKEN"] # GitLab Docs bot access token with Developer access to gitlab-docs
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The remote docs project
|
|
|
|
#
|
2019-09-30 21:07:59 +05:30
|
|
|
GITLAB_DOCS_REPO = 'gitlab-org/gitlab-docs'.freeze
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
#
|
2019-12-21 20:55:43 +05:30
|
|
|
# This is the branch that will be created in the gitlab-docs project.
|
|
|
|
# Name it after the product we're previewing and the ID of the MR that
|
|
|
|
# kicked the review app.
|
2018-03-17 18:26:18 +05:30
|
|
|
#
|
|
|
|
def docs_branch
|
2019-12-21 20:55:43 +05:30
|
|
|
"docs-preview-#{slug}-#{ENV["CI_MERGE_REQUEST_IID"]}"
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2018-03-27 19:54:05 +05:30
|
|
|
# Create a remote branch in gitlab-docs and immediately cancel the pipeline
|
|
|
|
# to avoid race conditions, since a triggered pipeline will also run right
|
|
|
|
# after the branch creation. This only happens the very first time a branch
|
|
|
|
# is created and will be skipped in subsequent runs. Read more in
|
2019-09-30 21:07:59 +05:30
|
|
|
# https://gitlab.com/gitlab-org/gitlab-docs/issues/154.
|
2018-03-17 18:26:18 +05:30
|
|
|
#
|
|
|
|
def create_remote_branch
|
|
|
|
Gitlab.create_branch(GITLAB_DOCS_REPO, docs_branch, 'master')
|
2018-03-27 19:54:05 +05:30
|
|
|
puts "=> Remote branch '#{docs_branch}' created"
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
pipelines = nil
|
|
|
|
|
|
|
|
# Wait until the pipeline is started
|
|
|
|
loop do
|
|
|
|
sleep 1
|
|
|
|
puts "=> Waiting for pipeline to start..."
|
|
|
|
pipelines = Gitlab.pipelines(GITLAB_DOCS_REPO, { ref: docs_branch })
|
|
|
|
break if pipelines.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the first pipeline ID which should be the only one for the branch
|
|
|
|
pipeline_id = pipelines.first.id
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
# Cancel the pipeline
|
|
|
|
Gitlab.cancel_pipeline(GITLAB_DOCS_REPO, pipeline_id)
|
2018-03-17 18:26:18 +05:30
|
|
|
rescue Gitlab::Error::BadRequest
|
2018-03-27 19:54:05 +05:30
|
|
|
puts "=> Remote branch '#{docs_branch}' already exists"
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove a remote branch in gitlab-docs
|
|
|
|
#
|
|
|
|
def remove_remote_branch
|
|
|
|
Gitlab.delete_branch(GITLAB_DOCS_REPO, docs_branch)
|
2018-03-27 19:54:05 +05:30
|
|
|
puts "=> Remote branch '#{docs_branch}' deleted"
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Define suffix in review app URL based on project
|
|
|
|
#
|
|
|
|
def slug
|
|
|
|
case ENV["CI_PROJECT_NAME"]
|
2019-12-04 20:38:33 +05:30
|
|
|
when 'gitlab-foss'
|
2018-03-17 18:26:18 +05:30
|
|
|
'ce'
|
2019-12-04 20:38:33 +05:30
|
|
|
when 'gitlab'
|
2018-03-17 18:26:18 +05:30
|
|
|
'ee'
|
|
|
|
when 'gitlab-runner'
|
|
|
|
'runner'
|
|
|
|
when 'omnibus-gitlab'
|
|
|
|
'omnibus'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2019-09-30 21:07:59 +05:30
|
|
|
# Overriding vars in https://gitlab.com/gitlab-org/gitlab-docs/blob/master/.gitlab-ci.yml
|
2018-03-17 18:26:18 +05:30
|
|
|
#
|
|
|
|
def param_name
|
|
|
|
"BRANCH_#{slug.upcase}"
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Trigger a pipeline in gitlab-docs
|
|
|
|
#
|
|
|
|
def trigger_pipeline
|
|
|
|
# The review app URL
|
|
|
|
app_url = "http://#{docs_branch}.#{ENV["DOCS_REVIEW_APPS_DOMAIN"]}/#{slug}"
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
# Create the cross project pipeline using CI_JOB_TOKEN
|
2018-03-17 18:26:18 +05:30
|
|
|
pipeline = Gitlab.run_trigger(GITLAB_DOCS_REPO, ENV["CI_JOB_TOKEN"], docs_branch, { param_name => ENV["CI_COMMIT_REF_NAME"] })
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
puts "=> Follow the status of the triggered pipeline:"
|
2018-03-17 18:26:18 +05:30
|
|
|
puts ""
|
2018-12-05 23:21:45 +05:30
|
|
|
puts pipeline.web_url
|
2018-03-17 18:26:18 +05:30
|
|
|
puts ""
|
2018-03-27 19:54:05 +05:30
|
|
|
puts "=> In a few minutes, you will be able to preview your changes under the following URL:"
|
2018-03-17 18:26:18 +05:30
|
|
|
puts ""
|
|
|
|
puts app_url
|
|
|
|
puts ""
|
2019-09-30 21:07:59 +05:30
|
|
|
puts "=> For more information, see the documentation"
|
|
|
|
puts "=> https://docs.gitlab.com/ee/development/documentation/index.html#previewing-the-changes-live"
|
2018-03-27 19:54:05 +05:30
|
|
|
puts ""
|
|
|
|
puts "=> If something doesn't work, drop a line in the #docs chat channel."
|
|
|
|
puts ""
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# When the first argument is deploy then create the branch and trigger pipeline
|
|
|
|
# When it is 'stop', it deleted the remote branch. That way, we ensure there
|
|
|
|
# are no stale remote branches and the Review server doesn't fill.
|
|
|
|
#
|
|
|
|
case ARGV[0]
|
|
|
|
when 'deploy'
|
|
|
|
create_remote_branch
|
|
|
|
trigger_pipeline
|
|
|
|
when 'cleanup'
|
|
|
|
remove_remote_branch
|
|
|
|
else
|
|
|
|
puts "Please provide a valid option:
|
|
|
|
deploy - Creates the remote branch and triggers a pipeline
|
|
|
|
cleanup - Deletes the remote branch and stops the Review App"
|
|
|
|
end
|