---
stages:
  - prepare
  - build
  - collect
  - deploy


# Anchors

.release_rules: &release_rules
  rules:
    - if: "$CI_COMMIT_TAG =~ /^release-/ && $CI_COMMIT_BRANCH == 'master'"
      when: always
    - when: never



# Stage: prepare
#
# Builds unpacked extensions from the source files.

.prepare:
  stage: prepare
  image: python:3.7-stretch
  script:
    - python3 scripts/update-version.py -k "$SSH_TAGGING_KEY" -o version -v
    - bash scripts/build.sh --version "$(cat version)"
  artifacts:
    paths:
      - version
      - build/**

prepare:beta:
  extends: .prepare
  only:
    - beta

prepare:release:
  extends: .prepare
  <<: *release_rules


# Stage: build
#
# Builds packages from the prepared unpacked extensions.

.build:firefox:
  stage: build
  image: node:lts-buster
  cache:
    paths:
      - $CI_PROJECT_DIR/.cache/npm
  before_script:
    - mkdir -p "$CI_PROJECT_DIR/.cache/npm"
  script:
    - npm install --global web-ext --cache "$CI_PROJECT_DIR/.cache/npm" --prefer-offline --no-audit
    - cd build/firefox
    - web-ext -a . sign --channel=$CHANNEL
    - mv atp_safe_links_cleaner* ../../safelinks-cleaner-firefox-$VARIANT.xpi
  artifacts:
    paths:
      - safelinks-cleaner-firefox.xpi

build:firefox:beta:
  extends: .build:firefox
  variables:
    CHANNEL: unlisted
    VARIANT: beta
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "beta"'
      changes:
        - shared/**
        - firefox/**
      when: always
    - if: '$CI_COMMIT_MESSAGE =~ /#force-build:(all|firefox)/ && $CI_COMMIT_BRANCH == "beta"'
      when: always
    - when: never

build:firefox:release:
  extends: .build:firefox
  variables:
    CHANNEL: listed
    VARIANT: release
  <<: *release_rules


.build:thunderbird:
  stage: build
  image: python:3.7-stretch
  script:
    - apt-get -y update
    - apt-get -y install zip
    - cd build/thunderbird
    - zip -r ../../safelinks-cleaner-thunderbird-$VARIANT.xpi *
  artifacts:
    paths:
      - safelinks-cleaner-thunderbird-$VARIANT.xpi

.build:thunderbird:beta:
  extends: .build:thunderbird
  variables:
    VARIANT: beta
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "beta"'
      changes:
        - shared/**
        - thunderbird/**
      when: always
    - if: '$CI_COMMIT_MESSAGE =~ /#force-build:(all|thunderbird)/ && $CI_COMMIT_BRANCH == "beta"'
      when: always
    - when: never

.build:thunderbird:release:
  extends: .build:thunderbird
  variables:
    VARIANT: release
  <<: *release_rules


# Stage: collect/deploy
#
# Wait for build artifacts then deploy new beta version to pages.

collect:
  stage: collect
  image: busybox
  script:
    - echo "I have seen the CONSING!"
  only:
    - beta

pages:
  stage: deploy
  script:
    - cp -r site .public
    - sed -i -e "s|%BUILDDATE%|$(date +'%Y-%m-%d %H:%M')|g" .public/index.html
    - sed -i -e "s|%VERSION%|$(cat version)|g" .public/index.html
    - cp safelinks-cleaner-*.xpi .public
    - mv .public public
    - ls -lr public
  needs:
    - job: prepare
    - job: collect
    - project: $CI_PROJECT_PATH
      job: build:thunderbird:beta
      ref: $CI_COMMIT_REF_NAME
      artifacts: true
    - project: $CI_PROJECT_PATH
      job: build:firefox:beta
      ref: $CI_COMMIT_REF_NAME
      artifacts: true
  only:
    - beta
  artifacts:
    paths:
      - public/