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

Simplify the triggerring of 'gitlab-docs' Review App pipelines

There's no need to create a branch in the downstream project.
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 8009db27
...@@ -11,16 +11,15 @@ ...@@ -11,16 +11,15 @@
# See https://gitlab.com/gitlab-org/gitlab/issues/191273 # See https://gitlab.com/gitlab-org/gitlab/issues/191273
GIT_DEPTH: 1 GIT_DEPTH: 1
environment: environment:
name: review-docs/$DOCS_GITLAB_REPO_SUFFIX-$CI_MERGE_REQUEST_IID name: review-docs/mr-${CI_MERGE_REQUEST_IID}
# DOCS_REVIEW_APPS_DOMAIN and DOCS_GITLAB_REPO_SUFFIX are CI variables # DOCS_REVIEW_APPS_DOMAIN and DOCS_GITLAB_REPO_SUFFIX are CI variables
# Discussion: https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/14236/diffs#note_40140693 # Discussion: https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/14236/diffs#note_40140693
auto_stop_in: 2 weeks auto_stop_in: 2 weeks
url: http://docs-preview-$DOCS_GITLAB_REPO_SUFFIX-$CI_MERGE_REQUEST_IID.$DOCS_REVIEW_APPS_DOMAIN/$DOCS_GITLAB_REPO_SUFFIX url: http://${DOCS_BRANCH}-${DOCS_GITLAB_REPO_SUFFIX}-${CI_MERGE_REQUEST_IID}.${DOCS_REVIEW_APPS_DOMAIN}/${DOCS_GITLAB_REPO_SUFFIX}
on_stop: review-docs-cleanup on_stop: review-docs-cleanup
before_script: before_script:
- apk add --update openssl - source ./scripts/utils.sh
- gem install httparty --no-document --version 0.17.3 - install_gitlab_gem
- gem install gitlab --no-document --version 4.13.0
# Always trigger a docs build in gitlab-docs only on docs-only branches. # Always trigger a docs build in gitlab-docs only on docs-only branches.
# Useful to preview the docs changes live. # Useful to preview the docs changes live.
...@@ -33,7 +32,7 @@ review-docs-deploy: ...@@ -33,7 +32,7 @@ review-docs-deploy:
review-docs-cleanup: review-docs-cleanup:
extends: .review-docs extends: .review-docs
environment: environment:
name: review-docs/$DOCS_GITLAB_REPO_SUFFIX-$CI_MERGE_REQUEST_IID name: review-docs/mr-${CI_MERGE_REQUEST_IID}
action: stop action: stop
script: script:
- ./scripts/trigger-build docs cleanup - ./scripts/trigger-build docs cleanup
......
...@@ -214,15 +214,7 @@ module Trigger ...@@ -214,15 +214,7 @@ module Trigger
=> If something doesn't work, drop a line in the #docs chat channel. => If something doesn't work, drop a line in the #docs chat channel.
MSG MSG
# 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 deploy! def deploy!
create_remote_branch!
cancel_latest_pipeline!
invoke!.wait! invoke!.wait!
display_success_message display_success_message
end end
...@@ -231,31 +223,47 @@ module Trigger ...@@ -231,31 +223,47 @@ module Trigger
# Remove a remote branch in gitlab-docs. # Remove a remote branch in gitlab-docs.
# #
def cleanup! def cleanup!
gitlab_client(:downstream).delete_branch(downstream_project_path, ref) environment = gitlab_client(:downstream).environments(downstream_project_path, name: downstream_environment).first
puts "=> Remote branch '#{downstream_project_path}' deleted" return unless environment
environment = gitlab_client(:downstream).stop_environment(downstream_project_path, environment.id)
if environment.state == 'stopped'
puts "=> Downstream environment '#{downstream_environment}' stopped"
else
puts "=> Downstream environment '#{downstream_environment}' failed to stop."
end
end end
private private
def downstream_environment
"review/#{ref}#{review_slug}"
end
# We prepend the `-` here because we cannot use variable substitution in `environment.name`/`environment.url`
# Some projects (e.g. `omnibus-gitlab`) use this script for branch pipelines, so we fallback to using `CI_COMMIT_REF_SLUG` for those cases.
def review_slug
identifier = ENV['CI_MERGE_REQUEST_IID'] || ENV['CI_COMMIT_REF_SLUG']
"-#{project_slug}-#{identifier}"
end
def downstream_project_path def downstream_project_path
ENV['DOCS_PROJECT_PATH'] || 'gitlab-org/gitlab-docs' ENV['DOCS_PROJECT_PATH'] || 'gitlab-org/gitlab-docs'
end end
def ref def ref
if ENV['CI_MERGE_REQUEST_IID'].nil? ENV['DOCS_BRANCH'] || 'master'
"docs-preview-#{slug}-#{ENV['CI_COMMIT_REF_SLUG']}"
else
"docs-preview-#{slug}-#{ENV['CI_MERGE_REQUEST_IID']}"
end
end end
def extra_variables def extra_variables
{ {
"BRANCH_#{slug.upcase}" => ENV['CI_COMMIT_REF_NAME'] "BRANCH_#{project_slug.upcase}" => ENV['CI_COMMIT_REF_NAME'],
"REVIEW_SLUG" => review_slug
} }
end end
def slug def project_slug
case ENV['CI_PROJECT_PATH'] case ENV['CI_PROJECT_PATH']
when 'gitlab-org/gitlab-foss' when 'gitlab-org/gitlab-foss'
'ce' 'ce'
...@@ -270,37 +278,14 @@ module Trigger ...@@ -270,37 +278,14 @@ module Trigger
end end
end end
# app_url is the URL of the `gitlab-docs` Review App URL defined in
# https://gitlab.com/gitlab-org/gitlab-docs/-/blob/b38038132cf82a24271bbb294dead7c2f529e275/.gitlab-ci.yml#L383
def app_url def app_url
"http://#{ref}.#{ENV['DOCS_REVIEW_APPS_DOMAIN']}/#{slug}" "http://#{ref}#{review_slug}.#{ENV['DOCS_REVIEW_APPS_DOMAIN']}/#{project_slug}"
end
def create_remote_branch!
gitlab_client(:downstream).create_branch(downstream_project_path, ref, 'master')
puts "=> Remote branch '#{ref}' created"
rescue Gitlab::Error::BadRequest
puts "=> Remote branch '#{ref}' already exists!"
end
def cancel_latest_pipeline!
pipelines = nil
# Wait until the pipeline is started
loop do
sleep 1
puts "=> Waiting for pipeline to start..."
pipelines = gitlab_client(:downstream).pipelines(downstream_project_path, { ref: ref })
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_client(:downstream).cancel_pipeline(downstream_project_path, pipeline_id)
end end
def display_success_message def display_success_message
format(SUCCESS_MESSAGE, app_url: app_url) puts format(SUCCESS_MESSAGE, app_url: app_url)
end end
end end
......
...@@ -32,7 +32,7 @@ function install_api_client_dependencies_with_apt() { ...@@ -32,7 +32,7 @@ function install_api_client_dependencies_with_apt() {
function install_gitlab_gem() { function install_gitlab_gem() {
gem install httparty --no-document --version 0.18.1 gem install httparty --no-document --version 0.18.1
gem install gitlab --no-document --version 4.14.1 gem install gitlab --no-document --version 4.17.0
} }
function install_tff_gem() { function install_tff_gem() {
......
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