image: "registry.gitlab.com/luciocarreras/sayonara-player:focal"

default:

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

.run-when-merge-to-master-or-tag:
  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

build:docker:
  stage: build
  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 -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: always
    paths:
      - ${CI_PROJECT_DIR}/build/test/*.xml
    reports:
      junit: ${CI_PROJECT_DIR}/build/test/*.xml
    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
  tags: ["luke-1"]
  stage: packaging
  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
  extends: .run-when-merge-to-master-or-tag

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
    - cp -r /root/rpmbuild/RPMS ./build/dist/
  artifacts:
    paths:
      - ./build/dist
    expire_in: 1 day
  extends: .run-when-merge-to-master-or-tag

package:appimage:
  stage: packaging
  tags: ["luke-1"]
  dependencies:
    - build:sayonara
  script:
    - dist/AppImage/create-appimage.sh ${CI_COMMIT_SHORT_SHA}
  artifacts:
    paths:
      - build/*.AppImage
    expire_in: 1 day
  extends: .run-when-merge-to-master-or-tag

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_TAG
    - 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"
  script:
    - export SNAPCRAFT_STORE_CREDENTIALS=$SNAPCRAFT9_LOGIN
    - snapcraft push *.snap --release $SNAP_DEPLOY_CHANNEL
  needs:
    - job: package:snap
      artifacts: true
  extends: .run-when-merge-to-master-or-tag

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
  extends: .run-when-merge-to-master-or-tag
