image: "registry.gitlab.com/luciocarreras/sayonara-player:focal"
stages:
  - build
  - test
  - packaging
  - deploy

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "web"
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_REF_NAME == 'master'
    - if: $CI_COMMIT_TAG
    - when: never


build:docker:
  stage: build
  tags:
    - luke-1
  image:
    name: gcr.io/kaniko-project/executor:v1.14.0-debug
    entrypoint: [""]
  script:
    - /kaniko/executor
      --context "${CI_PROJECT_DIR}"
      --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
      --destination "${CI_REGISTRY_IMAGE}:test-focal"
  rules:
    - if: $CI_PIPELINE_SOURCE == "web"
      when: manual
      allow_failure: true
    - when: never


build:sayonara:
  stage: build
  tags:
    - luke-1
  script:
    - cmake --preset=ci-cotire -DGIT_REVISION="${CI_COMMIT_SHORT_SHA}"
    - cd build
    - make -j`nproc`
    - make install DESTDIR=AppDir
  artifacts:
    untracked: true
    expire_in: 1 day
  rules:
    - when: always


test:sayonara:
  stage: test
  dependencies:
    - build:sayonara
  variables:
    QT_QPA_PLATFORM: "offscreen"
  script:
    - cd build
    - make test
  artifacts:
    when: on_failure
    untracked: true
    expire_in: 1 day
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - when: never


package:snap:
  image: registry.gitlab.com/luciocarreras/sayonara-player:core20-5-99-qt-5-15-7
  stage: packaging
  tags:
    - luke-1
  script:
    - export LC_ALL=C.UTF-8
    - export LANG=C.UTF-8
    - export SNAP_ARCH=amd64
    - export SNAPCRAFT_BUILD_INFO=1
    - export SNAPCRAFT_SETUP_CORE=1
    - apt update -qq && apt install -y -qq git
    - echo $(snapcraft --version)
    - snapcraft --destructive-mode --enable-experimental-extensions
  artifacts:
    when: always
    paths:
      - ./*.snap
    expire_in: 1 week
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_REF_NAME == 'master'
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: manual
      allow_failure: true
    - when: never


package:fedora:
  image: "fedora:latest"
  stage: packaging
  tags:
    - luke-1
  variables:
    RPM_BUILD_NCPUS: 8
  script:
    - dnf --refresh --assumeyes makecache
    - dnf --assumeyes install git make cmake gcc-c++ desktop-file-utils libappstream-glib libnotify-devel qt5-qtbase-devel qt5-qttools-devel qt5-qtsvg-devel gstreamer1-plugins-base-devel taglib-devel zlib-devel hicolor-icon-theme auto-buildrequires
    - rm -rf build && mkdir build && cd build
    - cmake ..
    - cd ..
    - dist/rpm/fedora-rpm.sh ${CI_COMMIT_SHA} ./build/dist/sayonara.spec
  artifacts:
    paths:
      - /root/rpmbuild/RPMS
    expire_in: 1 day
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_REF_NAME == 'master'
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: manual
      allow_failure: true
    - when: never


package:appimage:
  stage: packaging
  dependencies:
    - build:sayonara
  script:
    - dist/AppImage/create-appimage.sh ${CI_COMMIT_SHORT_SHA}
  artifacts:
    paths:
      - build/*.AppImage
    expire_in: 1 day
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_REF_NAME == 'master'
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: manual
      allow_failure: true
    - when: never


deploy:ppa:
  stage: deploy
  script:
    - curl -X POST
      --fail
      -F token=${PPA_TOKEN}
      -F ref=${PPA_REF_NAME}
      -F variables[UPSTREAM_COMMIT_ID]=${CI_COMMIT_SHORT_SHA}
      "https://gitlab.com/api/v4/projects/${PPA_PROJECT}/trigger/pipeline"
  rules:
    - if: $CI_COMMIT_REF_NAME == 'master'
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: manual
      allow_failure: true
    - when: never


deploy:snap:
  image: registry.gitlab.com/luciocarreras/sayonara-player:core20-5-99-qt-5-15-7
  stage: deploy
  variables:
    SNAP_DEPLOY_CHANNEL: "edge"
  tags:
    - luke-1
  script:
    - echo $SNAPCRAFT_LOGIN | base64 --decode --ignore-garbage > snapcraft.login
    - snapcraft login --with snapcraft.login
    - snapcraft push *.snap --release $SNAP_DEPLOY_CHANNEL
  needs:
    - job: package:snap
      artifacts: true
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_REF_NAME == 'master'
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: manual
      allow_failure: true
    - when: never


deploy:language:
  stage: deploy
  script:
    - mkdir -p tx
    - /opt/qt/bin/lupdate 
      -no-obsolete 
      -no-sort 
      -locations relative 
      -source-language en 
      -target-language en 
      ./src 
      -ts tx/sayonara_lang_en.ts
    - cd tx
    - export TX_TOKEN="$TX_API_KEY"
    - export PATH=/tmp/tx:$PATH
    - tx init
    - tx add 
      --organization sayonara 
      --project sayonara-player 
      --resource sayonara_lang_en 
      --file-filter "sayonara_lang_<lang>.ts" 
      --type QT sayonara_lang_en.ts
    - tx push
  artifacts:
    paths:
      - tx/*.ts
    expire_in: 2 days
  rules:
    - if: $CI_COMMIT_REF_NAME == 'master'
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: manual
      allow_failure: true
    - when: never

