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


version:
  image: python:3.7-stretch
  stage: version
  script:
    - python3 scripts/update-version.py -k "$SSH_TAGGING_KEY" -o version -v
  artifacts:
    paths:
      - version
  only:
    - branches


prepare:
  stage: prepare
  image: python:3.7-stretch
  script:
    - bash scripts/build.sh --version "$(cat version)"
  artifacts:
    paths:
      - build/**
  only:
    - master
    - beta


.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.xpi
  artifacts:
    paths:
      - safelinks-cleaner-firefox.xpi


build:firefox:beta:
  extends: .build:firefox
  variables:
    CHANNEL: unlisted
  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:master:
  extends: .build:firefox
  variables:
    CHANNEL: listed
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
      changes:
        - shared/**
        - firefox/**
      when: always
    - if: '$CI_COMMIT_MESSAGE =~ /#force-build:(all|firefox)/ && $CI_COMMIT_BRANCH == "master"'
      when: always
    - when: never


build:thunderbird:beta:
  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.xpi *
  artifacts:
    paths:
      - safelinks-cleaner-thunderbird.xpi
  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


collect:
  stage: collect
  script:
    - echo "done"
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "beta"'
      changes:
        - shared/**
        - thunderbird/**
        - firefox/**
      when: always
    - if: '$CI_COMMIT_MESSAGE =~ /#force-build:(all|firefox|thunderbird)/ && $CI_COMMIT_BRANCH == "beta"'
      when: always
    - when: never


pages:
  stage: deploy
  script:
    - cp -r site .public
    - sed -i -e "s/%BUILDDATE%/$(date +'%Y-%m-%d %H:%M')/g" .public/index.html
    - cp safelinks-cleaner-thunderbird.xpi .public
    - cp safelinks-cleaner-firefox.xpi .public
    - mv .public public
    - ls -lr public
  needs:
    - job: prepare
    - 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
  artifacts:
    paths:
      - public/
  only:
    - beta
  when: always
