summaryrefslogtreecommitdiff
path: root/scripts/trigger-build-docs
blob: ea717d9ccbfd70c25089f2ab3c50724f4e16d901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/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'
  config.private_token = ENV["DOCS_API_TOKEN"] # GitLab Docs bot access token with Developer access to gitlab-docs
end

#
# The remote docs project
#
GITLAB_DOCS_REPO = 'gitlab-org/gitlab-docs'.freeze

#
# 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.
#
def docs_branch
  "docs-preview-#{slug}-#{ENV["CI_MERGE_REQUEST_IID"]}"
end

#
# 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
# https://gitlab.com/gitlab-org/gitlab-docs/issues/154.
#
def create_remote_branch
  Gitlab.create_branch(GITLAB_DOCS_REPO, docs_branch, 'master')
  puts "=> Remote branch '#{docs_branch}' created"

  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

  # Cancel the pipeline
  Gitlab.cancel_pipeline(GITLAB_DOCS_REPO, pipeline_id)
rescue Gitlab::Error::BadRequest
  puts "=> Remote branch '#{docs_branch}' already exists"
end

#
# Remove a remote branch in gitlab-docs
#
def remove_remote_branch
  Gitlab.delete_branch(GITLAB_DOCS_REPO, docs_branch)
  puts "=> Remote branch '#{docs_branch}' deleted"
end

#
# Define suffix in review app URL based on project
#
def slug
  case ENV["CI_PROJECT_NAME"]
  when 'gitlab-foss'
    'ce'
  when 'gitlab'
    'ee'
  when 'gitlab-runner'
    'runner'
  when 'omnibus-gitlab'
    'omnibus'
  end
end

#
# Overriding vars in https://gitlab.com/gitlab-org/gitlab-docs/blob/master/.gitlab-ci.yml
#
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}"

  # Create the cross project pipeline using CI_JOB_TOKEN
  pipeline = Gitlab.run_trigger(GITLAB_DOCS_REPO, ENV["CI_JOB_TOKEN"], docs_branch, { param_name => ENV["CI_COMMIT_REF_NAME"] })

  puts "=> Follow the status of the triggered pipeline:"
  puts ""
  puts pipeline.web_url
  puts ""
  puts "=> In a few minutes, you will be able to preview your changes under the following URL:"
  puts ""
  puts app_url
  puts ""
  puts "=> For more information, see the documentation"
  puts "=> https://docs.gitlab.com/ee/development/documentation/index.html#previewing-the-changes-live"
  puts ""
  puts "=> If something doesn't work, drop a line in the #docs chat channel."
  puts ""
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