image: $CI_REGISTRY/openlp/runners/angular

stages:
  - test
  - build
  - deploy

audit:
  stage: test
  script:
    - yarn install
    - yarn audit
  rules:
    - when: always
  allow_failure: true

lint:
  stage: test
  script:
    - yarn install
    - yarn lint
  rules:
    - when: always

test:
  stage: test
  script:
    - yarn install
    - yarn test --no-progress --no-watch --browsers=ChromiumHeadlessCI
  rules:
    - when: always

push-i18n-source:
  stage: build
  script:
    - apk add npm
    - npm install @transifex/api
    - npm run tx push
  rules:
    - changes:
        - src/assets/en.json

build-branch:
  stage: build
  script:
    # Temporarily install npm manually until we get it added to the runner image
    - apk add npm
    - yarn install --production
    - npm install @angular/cli
    - yarn build --no-progress --configuration production --aot
    - export APP_VERSION=`git describe --dirty --tags --long --match '*[0-9]*'`
    - 'echo "window.appVersion = \"$APP_VERSION\";" > dist/web-remote/assets/version.js'
  artifacts:
    paths:
      - dist/
  rules:
    - if: $CI_COMMIT_BRANCH != "master"

build-tag:
  stage: build
  script:
    # Temporarily install npm manually until we get it added to the runner image
    - apk add npm
    - yarn install --production
    - npm install @angular/cli
    - yarn build --no-progress --configuration production --aot
    - 'echo "window.appVersion = \"$CI_COMMIT_TAG\";" > dist/web-remote/assets/version.js'
  artifacts:
    paths:
      - dist/
  rules:
    - if: $CI_COMMIT_TAG

deploy:
  stage: deploy
  variables:
    OPENLP_HOST: "new.openlp.io"
  script:
    - cd dist/web-remote
    - zip -r ../../remote-$CI_COMMIT_TAG.zip *
    - cd ../../
    - export CHECK_SUM=`sha256sum remote-$CI_COMMIT_TAG.zip | cut -d' ' -f1`
    - export FILE_SIZE=`stat -c '%s' remote-$CI_COMMIT_TAG.zip`
    - 'echo -e "{\"latest\": {\"version\": \"$CI_COMMIT_TAG\", \"sha256\": \"$CHECK_SUM\", \"filename\": \"remote-$CI_COMMIT_TAG.zip\", \"size\": $FILE_SIZE}}" > version-$CI_COMMIT_TAG.json'
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - eval $(ssh-agent -s)
    - ssh-add ~/.ssh/id_rsa
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - ssh openlp@$OPENLP_HOST "mkdir -p /home/openlp/sites/get.openlp.org/www/remote/$CI_COMMIT_TAG"
    - scp remote-$CI_COMMIT_TAG.zip openlp@$OPENLP_HOST:/home/openlp/sites/get.openlp.org/www/remote/$CI_COMMIT_TAG/
    - scp version-$CI_COMMIT_TAG.json openlp@$OPENLP_HOST:/home/openlp/sites/get.openlp.org/www/remote/
    - ssh openlp@$OPENLP_HOST "rm /home/openlp/sites/get.openlp.org/www/remote/version.json"
    - scp version-$CI_COMMIT_TAG.json openlp@$OPENLP_HOST:/home/openlp/sites/get.openlp.org/www/remote/version.json
  rules:
    - if: $CI_COMMIT_TAG