kind: pipeline
name: build

trigger:
  branch:
    include:
      - main
  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:16
    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:16
    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:16
    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/
      CYPRESS_DEFAULT_COMMAND_TIMEOUT: 20000
    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:
    - main
  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:16
    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
    depends_on:
      - restore-cache

  - name: static
    image: kolaente/zip
    pull: true
    commands:
      - cd dist
      - zip -r ../vikunja-frontend-unstable.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-unstable.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:16
    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
    depends_on:
      - restore-cache

  - 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:
    - main
  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@main

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

depends_on:
  - release-latest
  - release-version

platform:
  os: linux
  arch: arm64

trigger:
  ref:
    - refs/heads/main
    - "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
      tags: latest-linux-arm
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=unstable
    when:
      ref:
        - refs/heads/main
    depends_on:
      - clone

  - 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/**"
    depends_on:
      - clone

  - name: docker-latest-arm64
    image: plugins/docker:linux-arm64
    pull: true
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/frontend
      tags: latest-linux-arm64
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=unstable
    when:
      ref:
        - refs/heads/main
    depends_on:
      - clone

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

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

platform:
  os: linux
  arch: amd64

depends_on:
  - release-latest
  - release-version

trigger:
  ref:
    - refs/heads/main
    - "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
      tags: latest-linux-amd64
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=unstable
    when:
      ref:
        - refs/heads/main

  - 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/main
    - "refs/tags/**"

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

steps:
  - name: manifest-latest
    pull: always
    image: plugins/manifest
    settings:
      tags: latest
      spec: docker-manifest-latest.tmpl
      password:
        from_secret: docker_password
      username:
        from_secret: docker_username
    when:
      ref:
        - refs/heads/main

  - name: manifest-release
    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
    when:
      ref:
        - "refs/tags/**"

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

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

depends_on:
  - build
  - release-version
  - release-latest
  - trigger-desktop-update
  - docker-arm-release
  - docker-amd64-release
  - docker-manifest

steps:
  - name: notify
    image: plugins/matrix
    settings:
      homeserver: https://matrix.org
      roomid: WqBDCxzghKcNflkErL:matrix.org
      username:
        from_secret: matrix_username
      password:
        from_secret: matrix_password
    when:
      status:
        - success
        - failure
---
kind: pipeline
name: ping-weblate

depends_on:
  - build

trigger:
  branch:
    - main
  event:
    - push

steps:
  - name: update-translation-base
    image: appleboy/drone-git-push
    failure: ignore
    settings:
      branch: translations
      remote: ssh://git@kolaente.dev:9022/vikunja/frontend.git
      ssh_key:
        from_secret: translations_branch_update_ssh_key
  - name: notify-weblate
    image: curlimages/curl
    depends_on:
      - update-translation-base
    environment:
      WEBLATE_TOKEN:
        from_secret: weblate_token
    commands:
      - ./ping-weblate.sh
