variables:
  EXTENSION_PATH: "./deadlock-plugins/deadlock-extension"
  REGISTRY: "registry.takima.io"

stages:
  - build
  - publish

before_script:
  - apk update
  - apk add nodejs npm
  - apk add yarn
  - apk add curl
  - export TAG=${CI_COMMIT_TAG:-latest}
  - ./setup.sh

build:
  stage: build
  services:
    - docker:18.09.6-dind
  variables:
    VERSION: "desktop"
  script:
    - ./build.sh $TAG desktop $CI_REGISTRY_IMAGE
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $REGISTRY
    - docker push $CI_REGISTRY_IMAGE/$VERSION:$TAG
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
    - if: $CI_COMMIT_TAG != null

desktop_extension:package:pre-release:
  stage: build
  cache:
    paths:
      - $EXTENSION_PATH
    policy: push
  script:
    - ./build-desktop.sh staging
  rules:
    - if: $CI_COMMIT_TAG =~ /(?:\d+.){2}(?:\d+)*(1|3|5|7|9)$/ # If tag is like X.Y.Z and Z is odd


desktop_extension:publish:pre-release:
  stage: publish
  cache:
    paths:
      - $EXTENSION_PATH
    policy: pull
  script:
    - cd $EXTENSION_PATH
    - npm run vsce-publish -- --pre-release -p $VSCODE_EXTENSION_MARKET_PLACE_ACCESS_TOKEN
  rules:
    - if: $CI_COMMIT_TAG =~ /(?:\d+.){2}(?:\d+)*(1|3|5|7|9)$/ # If tag is like X.Y.Z and Z is odd


desktop_extension:package:release:
  stage: build
  cache:
    paths:
      - $EXTENSION_PATH
    policy: push
  script:
    - ./build-desktop.sh prod
  rules:
    - if: $CI_COMMIT_TAG =~ /(?:\d+.){2}(?:\d+)*(2|4|6|8|0)$/ # If tag is like X.Y.Z and Z is even

desktop_extension:publish:release:
  stage: publish
  cache:
    paths:
      - $EXTENSION_PATH
    policy: pull
  script:
    - cd $EXTENSION_PATH
    - npm run vsce-publish -- -p $VSCODE_EXTENSION_MARKET_PLACE_ACCESS_TOKEN
  rules:
    - if: $CI_COMMIT_TAG =~ /(?:\d+.){2}(?:\d+)*(2|4|6|8|0)$/ # If tag is like X.Y.Z and Z is even
