#
# GitLab CI configuration for QOwnNotes
# https://ci.gitlab.com/lint
#
# you need a runner for OS X
# https://gitlab.com/gitlab-org/gitlab-ci-multi-runner
#
# execute: gitlab-ci-multi-runner run
#
# set the GITHUB_ACCESS_TOKEN with your GitHub securitry token for release deployment
#

before_script:
  - PATH="/Applications/Qt/5.4/clang_64/bin:$PATH"
  - QTDIR="/Applications/Qt/5.4/clang_64"
  - cd src
  # http://doc.gitlab.com/ci/variables/README.html
  - echo "#define BUILD ""$CI_BUILD_ID" > build_number.h
  - qmake -project
  - qmake QOwnNotes.pro -r -spec macx-clang CONFIG+=x86_64
  - make

build_job:
  type: build
  script:
    - echo do some testing...
  only:
    - main
    - release

deployment_job:
  type: deploy
  script:
    - ../gitlab-ci/create-dmg.sh
    - RELEASE_JSON_FILE="../gitlab-ci/github-release.json"
    - cat $RELEASE_JSON_FILE
    - sed -i -e 's/CI_BUILD_ID/'"$CI_BUILD_ID"'/g' $RELEASE_JSON_FILE
    - API_JSON=$(cat $RELEASE_JSON_FILE)
    # creating the GitHub release and parsing returned release id
    # $GITHUB_ACCESS_TOKEN is set as a secret variable
    # https://ci.gitlab.com/projects/5070/variables
    # https://github.com/settings/tokens
    - RELEASE_ID=$(curl --data "$API_JSON" https://api.github.com/repos/pbek/QOwnNotes/releases?access_token=$GITHUB_ACCESS_TOKEN | python -c 'import sys, json; print json.load(sys.stdin)["id"]')
    - echo $RELEASE_ID
    # check if $RELEASE_ID is greater than 0, fail build if not
    - if [[ ! $RELEASE_ID -gt 0 ]]; then exit 1; fi
    # deploy QOwnNotes.dmg to GitHub Releases
    - ../gitlab-ci/deploy-file.sh $RELEASE_ID $GITHUB_ACCESS_TOKEN

  only:
    - release
