variables:
  NODE_TAG: 20

stages:
  - build
  - release

chrome_extension:
  stage: build
  image: node:${NODE_TAG}
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - npm run prod-chrome
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
        --upload-file dist/chrome/metager_search-${CI_COMMIT_TAG}.zip \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/metager_search/${CI_COMMIT_TAG}/metager_search-${CI_COMMIT_TAG}-chrome.zip"

firefox_extension:
  stage: build
  image: node:${NODE_TAG}
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - npm run prod-ff
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
        --upload-file dist/firefox/metager_search-${CI_COMMIT_TAG}.zip \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/metager_search/${CI_COMMIT_TAG}/metager_search-${CI_COMMIT_TAG}-firefox.zip"

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  needs:
    - chrome_extension
    - firefox_extension
  script:
    - echo "Releasing $CI_COMMIT_TAG"
  release:
    tag_name: $CI_COMMIT_TAG
    name: "$CI_COMMIT_TAG"
    description: "MetaGer Web Extension v$CI_COMMIT_TAG"
    assets:
      links:
        - name: "chrome"
          filepath: /metager_search-chrome.zip
          url: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/metager_search/${CI_COMMIT_TAG}/metager_search-${CI_COMMIT_TAG}-chrome.zip"
        - name: "firefox"
          filepath: /metager_search-firefox.zip
          url: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/metager_search/${CI_COMMIT_TAG}/metager_search-${CI_COMMIT_TAG}-firefox.zip"

amo:
  stage: release
  image: node:${NODE_TAG}
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - npm run prod-ff-release
