.review-docs:
  stage: docs
  extends:
  - .rules:docs:review
  - .no_cache_and_dependencies
  image: ruby:3.3-alpine
  needs: []
  before_script:
  - gem install gitlab --no-doc
  # We need to download the script rather than clone the repo since the
  # review-docs-cleanup and review-docs-hugo-cleanup jobs will not be able to run when the branch gets
  # deleted (when merging the MR).
  - apk add --update openssl
  - wget https://gitlab.com/gitlab-org/gitlab/-/raw/master/scripts/trigger-build.rb
  - chmod 755 trigger-build.rb
  variables:
    GIT_STRATEGY: none
    DOCS_REVIEW_APPS_DOMAIN: docs.gitlab-review.app
    # By default, deploy the Review App using the `main` branch of the `gitlab-org/gitlab-docs` project
    DOCS_BRANCH: main
  allow_failure: true

# Trigger a docs build in gitlab-docs
# Useful to preview the docs changes live
# https://docs.gitlab.com/ee/development/documentation/review_apps.html
review-docs-deploy:
  extends:
  - .review-docs
  environment:
    name: review-docs/mr-${CI_MERGE_REQUEST_IID}
    # 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
    auto_stop_in: 2 weeks
    url: https://${DOCS_BRANCH}-${DOCS_GITLAB_REPO_SUFFIX}-${CI_MERGE_REQUEST_IID}.${DOCS_REVIEW_APPS_DOMAIN}/${DOCS_GITLAB_REPO_SUFFIX}
    on_stop: review-docs-cleanup
  script:
  - ./trigger-build.rb docs deploy

# Cleanup remote environment of gitlab-docs
review-docs-cleanup:
  extends:
  - .review-docs
  environment:
    name: review-docs/mr-${CI_MERGE_REQUEST_IID}
    action: stop
  script:
  - ./trigger-build.rb docs cleanup

# Trigger a docs build in gitlab-docs-hugo
# Useful to preview the docs changes live
# https://docs.gitlab.com/ee/development/documentation/review_apps.html
review-docs-hugo-deploy:
  extends:
  - .review-docs
  environment:
    name: review-docs/mr-${CI_MERGE_REQUEST_IID}-hugo
    auto_stop_in: 2 weeks
    url: https://new.docs.gitlab.com/upstream-review-mr-${DOCS_GITLAB_REPO_SUFFIX}-${CI_MERGE_REQUEST_IID}/${DOCS_GITLAB_REPO_SUFFIX}
    on_stop: review-docs-hugo-cleanup
  script:
  - ./trigger-build.rb docs-hugo deploy

# Cleanup remote environment of gitlab-docs-hugo
review-docs-hugo-cleanup:
  extends:
  - .review-docs
  environment:
    name: review-docs/mr-${CI_MERGE_REQUEST_IID}-hugo
    action: stop
  script:
  - ./trigger-build.rb docs-hugo cleanup
