image: ruby:3.3

stages:
  - build
  - test
  - review
  - accessibility
  - deploy

.artifacts_extension:
  artifacts:
    paths:
      - public
    expire_in: 1d

.caching: &caching
  cache:
    - key:
        files:
          - Gemfile.lock
        prefix: "$CI_JOB_IMAGE"
      paths:
        - vendor/bundle
    - key: jumbotron-cache-$CI_COMMIT_REF_SLUG
      paths:
        - assets/img/jumbotrons/desktop
        - assets/img/jumbotrons/display_2k
        - assets/img/jumbotrons/phone
        - assets/img/jumbotrons/tablet

.common_production: &production_before_script
  before_script:
    - "[[ -x \"$(command -v apt-get)\" ]] && \
          apt-get update && \
          apt-get install -y ffmpeg"
    - ./fetch_external_content.sh
    - gem install bundler --no-document
    - bundle config set without 'test'
    - bundle install --jobs $(nproc)
  <<: *caching

.common_latest: &latest_before_script
  before_script:
    - "[[ -x \"$(command -v apt-get)\" ]] && \
          apt-get update && \
          apt-get install -y ffmpeg"
    - ./fetch_external_content.sh
    - "[[ -f \"Gemfile.lock\" ]] && rm Gemfile.lock"
    - bundle config set without 'test'
    - bundle install --jobs $(nproc) --without test "${FLAGS[@]}"
    - bundle update --jobs $(nproc) "${FLAGS[@]}"
  <<: *caching

.common_variables: &common_variables
  GIT_SUBMODULE_STRATEGY: recursive
  SUBPATH: "/"
  BUNDLE_PATH: vendor/bundle
  NOKOGIRI_USE_SYSTEM_LIBRARIES: "1"

.builds: &builds
  stage: build
  variables:
    <<: *common_variables
  script:
    - "bash scripts/create_jumbotrons.sh assets/img/jumbotrons/"
    - "bundle exec jekyll build --future \
        --config _config.yml \
        -d public$SUBPATH \
        --baseurl $SUBPATH"
    - "cp _favicons/* public/$SUBPATH"
  # https://docs.gitlab.com/ce/ci/yaml/#interruptible
  interruptible: true

.builds_reviewapps: &builds_reviewapps
  stage: build
  variables:
    <<: *common_variables
  script:
    - "bash scripts/create_jumbotrons.sh assets/img/jumbotrons/"
    - "bundle exec jekyll build --future \
        --config _config.yml,_config_reviewapps.yml \
        -d public$SUBPATH \
        --baseurl $SUBPATH"
    - "cp _favicons/* public/$SUBPATH"
  # https://docs.gitlab.com/ce/ci/yaml/#interruptible
  interruptible: true

.tests: &tests
  stage: test
  variables:
    <<: *common_variables
  before_script:
    # Installing, generating and exporting locale because htmlproofer needs it.
    # Locale is not set in "standard" docker containers.
    - "[[ -x \"$(command -v apt-get)\" ]] && \
      apt-get update && \
      apt-get install -y locales && \
      echo \"en_US UTF-8\" > /etc/locale.gen && \
      locale-gen en_US.UTF-8 && \
      export LANG=en_US.UTF-8 && \
      export LANGUAGE=en_US:en && \
      export LC_ALL=en_US.UTF-8"
    - gem install bundler --no-document
    - bundle install --jobs $(nproc)
  script:
    - "bundle exec htmlproofer public \
        --disable-external=true \
        --enforce-https=false"
    # Use yaml-lint tool to check yaml files.
    - "bundle exec yaml-lint _data/ _config.yml"
  <<: *caching
  # https://docs.gitlab.com/ce/ci/yaml/#interruptible
  interruptible: true

# Job runs jekyll build with latest dependencies.
build:latest:
  <<: *builds
  <<: *latest_before_script
  image: ruby:latest
  allow_failure: true

# Job runs jekyll build with production dependencies,
# puts everything in a sub-directory as required for review apps.
build:production:review-apps:
  <<: *builds_reviewapps
  <<: *production_before_script
  variables:
    <<: *common_variables
    SUBPATH: /review-apps/$CI_PROJECT_PATH_SLUG/$CI_COMMIT_REF_SLUG
  extends:
    - .artifacts_extension

# Job runs jekyll build with current production dependencies
build:production:
  <<: *builds
  <<: *production_before_script
  extends:
    - .artifacts_extension

# Job runs htmlproofer basing on production build.
test:htmlproofer:
  <<: *tests
  dependencies:
    - build:production
  needs: ["build:production"]

# Job runs htmlproofer basing on production build with subpath.
test:htmlproofer:subpath:
  <<: *tests
  dependencies:
    - build:production:review-apps
  needs: ["build:production:review-apps"]

# Job tests external links in addition to test:htmlproofer job.
test:htmlproofer:external_links:
  <<: *tests
  dependencies:
    - build:production
  needs: ["build:production"]
  script:
    - >
        bundle exec htmlproofer public
        --disable-external=false
        --enforce-https=false
        --ignore-urls='/www.openstoragenetwork.org/,/hzdr.de/,/indico4.twgrid.org/,/webfts.fedcloud-tf.fedcloud.eu/,/nubes.helmholtz-berlin.de\/f/'
        --ignore-status-codes=429
        --ignore-status-codes=401
        --ignore-status-codes=403
        --check-external-hash=false
        --typhoeus='{"headers":{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0"}}'
        --only-4xx
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
  allow_failure: true

review:
  stage: review
  variables:
    GIT_STRATEGY: none
    REVIEW_APPS_PATH: review-apps/$CI_PROJECT_PATH_SLUG/$CI_COMMIT_REF_SLUG
  dependencies:
    - build:production:review-apps
  needs: ["build:production:review-apps"]
  script:
    - mkdir -p /var/www/html/gl-review-apps/$REVIEW_APPS_PATH
    - rsync -a --delete ./public/$REVIEW_APPS_PATH/. /var/www/html/gl-review-apps/$REVIEW_APPS_PATH/.
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://$APPS_DOMAIN/review-apps/$CI_PROJECT_PATH_SLUG/$CI_COMMIT_REF_SLUG
    on_stop: stop_review
  tags:
    - review-apps
  allow_failure: true

stop_review:
  stage: review
  variables:
    GIT_STRATEGY: none
    REVIEW_APPS_PATH: review-apps/$CI_PROJECT_PATH_SLUG/$CI_COMMIT_REF_SLUG
  script:
    - rm -rf public /var/www/html/gl-review-apps/$REVIEW_APPS_PATH
    - rmdir --ignore-fail-on-non-empty /var/www/html/gl-review-apps/review-apps/$CI_PROJECT_PATH_SLUG/
  dependencies: []
  when: manual
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop
  tags:
    - review-apps

include:
  - template: 'Workflows/MergeRequest-Pipelines.gitlab-ci.yml'