kind: pipeline
name: build

trigger:
  branch:
    include:
      - master
  event:
    include:
      - push
      - pull_request

services:
  - name: api
    image: vikunja/api
    environment:
      VIKUNJA_SERVICE_TESTINGTOKEN: averyLongSecretToSe33dtheDB
      VIKUNJA_LOG_LEVEL: DEBUG

steps:
  - name: restore-cache
    image: meltwater/drone-cache:dev
    pull: true
    environment:
      AWS_ACCESS_KEY_ID:
        from_secret: cache_aws_access_key_id
      AWS_SECRET_ACCESS_KEY:
        from_secret: cache_aws_secret_access_key
    settings:
      restore: true
      bucket: kolaente.dev-drone-dependency-cache
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      cache_key: '{{ .Repo.Name }}_{{ checksum "yarn.lock" }}_{{ arch }}_{{ os }}'
      mount:
        - '.cache'

  - name: dependencies
    image: node:12
    pull: true
    environment:
      YARN_CACHE_FOLDER: .cache/yarn/
      CYPRESS_CACHE_FOLDER: .cache/cypress/
    commands:
      - yarn --frozen-lockfile --network-timeout 100000
    depends_on:
      - restore-cache

  - name: rebuild-cache
    image: meltwater/drone-cache:dev
    pull: true
    environment:
      AWS_ACCESS_KEY_ID:
        from_secret: cache_aws_access_key_id
      AWS_SECRET_ACCESS_KEY:
        from_secret: cache_aws_secret_access_key
    settings:
      rebuild: true
      bucket: kolaente.dev-drone-dependency-cache
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      cache_key: '{{ .Repo.Name }}_{{ checksum "yarn.lock" }}_{{ arch }}_{{ os }}'
      mount:
        - '.cache'
    depends_on:
      - dependencies

  - name: build
    image: node:12
    pull: true
    environment:
      YARN_CACHE_FOLDER: .cache/yarn/
      CYPRESS_CACHE_FOLDER: .cache/cypress/
    commands:
      - yarn run lint
      - yarn run build
    depends_on:
      - dependencies

  - name: test-unit
    image: node:12
    pull: true
    commands:
      - yarn test:unit
    depends_on:
      - dependencies

  - name: test-frontend
    image: cypress/browsers:node12.18.3-chrome87-ff82
    pull: true
    environment:
      CYPRESS_API_URL: http://api:3456/api/v1
      CYPRESS_TEST_SECRET: averyLongSecretToSe33dtheDB
      YARN_CACHE_FOLDER: .cache/yarn/
      CYPRESS_CACHE_FOLDER: .cache/cypress/
    commands:
      - sed -i 's/localhost/api/g' public/index.html
      - yarn serve & npx wait-on http://localhost:8080
      - yarn test:frontend --browser chrome
    depends_on:
      - dependencies

  - name: upload-test-results
    image: plugins/s3:1
    pull: true
    settings:
      bucket: drone-test-results
      access_key:
        from_secret: test_results_aws_access_key_id
      secret_key:
        from_secret: test_results_aws_secret_access_key
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      source: cypress/screenshots/**/**/*
      strip_prefix: cypress/screenshots/
      target: /${DRONE_REPO}/${DRONE_PULL_REQUEST}_${DRONE_BRANCH}/${DRONE_BUILD_NUMBER}/
    depends_on: 
      - test-frontend
    when:
      status:
        - failure
        - success

---
kind: pipeline
name: release-latest

depends_on:
  - build

trigger:
  branch:
    - master
  event:
    - push

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags

  - name: restore-cache
    image: meltwater/drone-cache:dev
    pull: true
    environment:
      AWS_ACCESS_KEY_ID:
        from_secret: cache_aws_access_key_id
      AWS_SECRET_ACCESS_KEY:
        from_secret: cache_aws_secret_access_key
    settings:
      restore: true
      bucket: kolaente.dev-drone-dependency-cache
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      cache_key: '{{ .Repo.Name }}_{{ checksum "yarn.lock" }}_{{ arch }}_{{ os }}'
      mount:
        - '.cache'

  - name: build
    image: node:12
    pull: true
    group: build-static
    environment:
      YARN_CACHE_FOLDER: .cache/yarn/
    commands:
      - yarn --frozen-lockfile --network-timeout 100000
      - yarn run lint
      - "echo '{\"VERSION\": \"'$(git describe --tags --always --abbrev=10 | sed 's/-/+/' | sed 's/^v//' | sed 's/-g/-/')'\"}' > src/version.json"
      - yarn run build
      - sed -i 's/http\:\\/\\/localhost\\:3456\\/api\\/v1/\\/api\\/v1/g' dist/index.html # Override the default api url used for developing

  - name: static
    image: kolaente/zip
    pull: true
    commands:
      - cd dist
      - zip -r ../vikunja-frontend-master.zip *
      - cd ..
    depends_on: [ build ]

  - name: release
    image: plugins/s3:1
    pull: true
    settings:
      bucket: vikunja-releases
      access_key:
        from_secret: aws_access_key_id
      secret_key:
        from_secret: aws_secret_access_key
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      source: vikunja-frontend-master.zip
      target: /frontend/
    depends_on: [ static ]

---
kind: pipeline
name: release-version

depends_on:
  - build

trigger:
  event:
    - tag

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags

  - name: restore-cache
    image: meltwater/drone-cache:dev
    pull: true
    environment:
      AWS_ACCESS_KEY_ID:
        from_secret: cache_aws_access_key_id
      AWS_SECRET_ACCESS_KEY:
        from_secret: cache_aws_secret_access_key
    settings:
      restore: true
      bucket: kolaente.dev-drone-dependency-cache
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      cache_key: '{{ .Repo.Name }}_{{ checksum "yarn.lock" }}_{{ arch }}_{{ os }}'
      mount:
        - '.cache'

  - name: build
    image: node:12
    pull: true
    group: build-static
    environment:
      YARN_CACHE_FOLDER: .cache/yarn/
    commands:
      - yarn --frozen-lockfile --network-timeout 100000
      - yarn run lint
      - "echo '{\"VERSION\": \"'$(git describe --tags --always --abbrev=10 | sed 's/-/+/' | sed 's/^v//' | sed 's/-g/-/')'\"}' > src/version.json"
      - yarn run build
      - sed -i 's/http\:\\/\\/localhost\\:3456\\/api\\/v1/\\/api\\/v1/g' dist/index.html # Override the default api url used for developing

  - name: static
    image: kolaente/zip
    pull: true
    commands:
      - cd dist
      - zip -r ../vikunja-frontend-${DRONE_TAG##v}.zip *
      - cd ..
    depends_on: [ build ]

  - name: release
    image: plugins/s3:1
    pull: true
    settings:
      bucket: vikunja-releases
      access_key:
        from_secret: aws_access_key_id
      secret_key:
        from_secret: aws_secret_access_key
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      source: vikunja-frontend-${DRONE_TAG##v}.zip
      target: /frontend/
    depends_on: [ static ]

---
kind: pipeline
name: trigger-desktop-update

trigger:
  branch:
    - master
  event:
    - push

depends_on:
  - release-latest

steps:
  - name: trigger
    image: plugins/downstream
    settings:
      server: https://drone.kolaente.de
      token:
        from_secret: drone_token
      repositories:
        - vikunja/desktop@master

---
kind: pipeline
type: docker
name: docker-arm-release

depends_on:
  - release-latest
  - release-version

platform:
  os: linux
  arch: arm

trigger:
  ref:
    - refs/heads/master
    - "refs/tags/**"

steps:
  - name: docker-latest
    image: plugins/docker:linux-arm
    pull: true
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/frontend
      auto_tag: true
      auto_tag_suffix: linux-arm
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=master
    when:
      ref:
        - refs/heads/master

  - name: docker-version
    image: plugins/docker:linux-arm
    pull: true
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/frontend
      auto_tag: true
      auto_tag_suffix: linux-arm
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=${DRONE_TAG##v}
    when:
      ref:
        - "refs/tags/**"

---
kind: pipeline
type: docker
name: docker-amd64-release

platform:
  os: linux
  arch: amd64

depends_on:
  - release-latest
  - release-version

trigger:
  ref:
    - refs/heads/master
    - "refs/tags/**"

steps:
  - name: docker-latest
    image: plugins/docker:linux-amd64
    pull: true
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/frontend
      auto_tag: true
      auto_tag_suffix: linux-amd64
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=master
    when:
      ref:
        - refs/heads/master

  - name: docker-version
    image: plugins/docker:linux-amd64
    pull: true
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/frontend
      auto_tag: true
      auto_tag_suffix: linux-amd64
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=${DRONE_TAG##v}
    when:
      ref:
        - "refs/tags/**"

---
kind: pipeline
type: docker
name: docker-manifest

trigger:
  ref:
    - refs/heads/master
    - "refs/tags/**"

depends_on:
  - docker-amd64-release
  - docker-arm-release

steps:
  - name: manifest
    pull: always
    image: plugins/manifest
    settings:
      auto_tag: true
      ignore_missing: true
      spec: docker-manifest.tmpl
      password:
        from_secret: docker_password
      username:
        from_secret: docker_username

---
kind: pipeline
type: docker
name: notify

trigger:
  ref:
    - refs/heads/master
    - "refs/tags/**"

depends_on:
  - release-version
  - release-latest
  - docker-manifest

steps:
  - name: telegram
    image: appleboy/drone-telegram:1-linux-amd64
    settings:
      token:
        from_secret: TELEGRAM_TOKEN
      to:
        from_secret: TELEGRAM_TO
      message: >
        {{repo.owner}}/{{repo.name}}: \[{{build.status}}] Build {{build.number}}
        {{commit.author}} pushed to {{commit.branch}} {{commit.sha}}: `{{commit.message}}`
        Build started at {{datetime build.started "2006-Jan-02T15:04:05Z" "GMT+2"}} finished at {{datetime build.finished "2006-Jan-02T15:04:05Z" "GMT+2"}}.
    when:
      status:
        - success
        - failure