2017-09-07 11:43:20 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'gitlab'
|
|
|
|
|
|
|
|
#
|
|
|
|
# Configure credentials to be used with gitlab gem
|
|
|
|
#
|
|
|
|
Gitlab.configure do |config|
|
2018-09-04 16:56:20 -04:00
|
|
|
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
|
2017-09-07 11:43:20 -04:00
|
|
|
end
|
|
|
|
|
2017-09-19 07:30:44 -04:00
|
|
|
#
|
|
|
|
# The remote docs project
|
|
|
|
#
|
2019-07-11 07:58:49 -04:00
|
|
|
GITLAB_DOCS_REPO = 'gitlab-org/gitlab-docs'.freeze
|
2017-09-19 07:30:44 -04:00
|
|
|
|
|
|
|
#
|
2018-07-04 06:26:52 -04:00
|
|
|
# Truncate the remote docs branch name otherwise we hit the filesystem
|
|
|
|
# limit and the directory name where NGINX serves the site won't match
|
|
|
|
# the branch name.
|
2017-09-19 07:30:44 -04:00
|
|
|
#
|
|
|
|
def docs_branch
|
|
|
|
# The maximum string length a file can have on a filesystem (ext4)
|
2018-07-04 06:26:52 -04:00
|
|
|
# is 63 characters. CI_ENVIRONMENT_SLUG is limited to 24 characters.
|
|
|
|
ENV["CI_ENVIRONMENT_SLUG"]
|
2017-09-19 07:30:44 -04:00
|
|
|
end
|
|
|
|
|
2017-09-07 11:43:20 -04:00
|
|
|
#
|
2018-03-06 04:02:21 -05:00
|
|
|
# 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-07-11 07:58:49 -04:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab-docs/issues/154.
|
2017-09-07 11:43:20 -04:00
|
|
|
#
|
|
|
|
def create_remote_branch
|
2017-09-19 07:30:44 -04:00
|
|
|
Gitlab.create_branch(GITLAB_DOCS_REPO, docs_branch, 'master')
|
2018-03-06 04:02:21 -05:00
|
|
|
puts "=> Remote branch '#{docs_branch}' created"
|
|
|
|
|
2018-03-08 06:04:34 -05:00
|
|
|
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-06 04:02:21 -05:00
|
|
|
|
|
|
|
# Cancel the pipeline
|
|
|
|
Gitlab.cancel_pipeline(GITLAB_DOCS_REPO, pipeline_id)
|
2017-09-07 13:21:47 -04:00
|
|
|
rescue Gitlab::Error::BadRequest
|
2018-03-06 04:02:21 -05:00
|
|
|
puts "=> Remote branch '#{docs_branch}' already exists"
|
2017-09-07 11:43:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove a remote branch in gitlab-docs
|
|
|
|
#
|
|
|
|
def remove_remote_branch
|
2017-09-19 07:30:44 -04:00
|
|
|
Gitlab.delete_branch(GITLAB_DOCS_REPO, docs_branch)
|
2018-03-06 04:02:21 -05:00
|
|
|
puts "=> Remote branch '#{docs_branch}' deleted"
|
2017-09-07 11:43:20 -04:00
|
|
|
end
|
|
|
|
|
2017-11-15 07:05:53 -05:00
|
|
|
#
|
|
|
|
# Define suffix in review app URL based on project
|
|
|
|
#
|
|
|
|
def slug
|
|
|
|
case ENV["CI_PROJECT_NAME"]
|
|
|
|
when 'gitlab-ce'
|
|
|
|
'ce'
|
|
|
|
when 'gitlab-ee'
|
|
|
|
'ee'
|
|
|
|
when 'gitlab-runner'
|
|
|
|
'runner'
|
|
|
|
when 'omnibus-gitlab'
|
|
|
|
'omnibus'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2019-07-11 07:58:49 -04:00
|
|
|
# Overriding vars in https://gitlab.com/gitlab-org/gitlab-docs/blob/master/.gitlab-ci.yml
|
2017-11-15 07:05:53 -05:00
|
|
|
#
|
|
|
|
def param_name
|
|
|
|
"BRANCH_#{slug.upcase}"
|
|
|
|
end
|
|
|
|
|
2017-09-07 11:43:20 -04:00
|
|
|
#
|
|
|
|
# Trigger a pipeline in gitlab-docs
|
|
|
|
#
|
|
|
|
def trigger_pipeline
|
|
|
|
# The review app URL
|
2017-11-15 07:05:53 -05:00
|
|
|
app_url = "http://#{docs_branch}.#{ENV["DOCS_REVIEW_APPS_DOMAIN"]}/#{slug}"
|
2017-09-07 11:43:20 -04:00
|
|
|
|
2018-03-06 04:02:21 -05:00
|
|
|
# Create the cross project pipeline using CI_JOB_TOKEN
|
2017-09-19 07:30:44 -04:00
|
|
|
pipeline = Gitlab.run_trigger(GITLAB_DOCS_REPO, ENV["CI_JOB_TOKEN"], docs_branch, { param_name => ENV["CI_COMMIT_REF_NAME"] })
|
2017-09-07 11:43:20 -04:00
|
|
|
|
2018-03-06 04:02:21 -05:00
|
|
|
puts "=> Follow the status of the triggered pipeline:"
|
2017-09-07 11:43:20 -04:00
|
|
|
puts ""
|
2018-09-04 16:56:20 -04:00
|
|
|
puts pipeline.web_url
|
2017-09-07 11:43:20 -04:00
|
|
|
puts ""
|
2018-03-06 04:02:21 -05:00
|
|
|
puts "=> In a few minutes, you will be able to preview your changes under the following URL:"
|
2017-09-07 11:43:20 -04:00
|
|
|
puts ""
|
|
|
|
puts app_url
|
|
|
|
puts ""
|
2019-07-08 11:44:50 -04:00
|
|
|
puts "=> For more information, see the documentation"
|
|
|
|
puts "=> https://docs.gitlab.com/ee/development/documentation/index.html#previewing-the-changes-live"
|
2018-03-06 04:02:21 -05:00
|
|
|
puts ""
|
|
|
|
puts "=> If something doesn't work, drop a line in the #docs chat channel."
|
|
|
|
puts ""
|
2017-09-07 11:43:20 -04:00
|
|
|
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
|
2017-09-19 07:30:44 -04:00
|
|
|
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"
|
2017-09-07 11:43:20 -04:00
|
|
|
end
|