kind: pipeline
name: default

workspace:
  base: /go
  path: src/github.com/mickael-kerjean.me/filestash

clone:
  depth: 1


steps:

- name: build_prepare
  image: alpine:latest
  depends_on: [ clone ]
  commands:
  - mkdir -p ./dist/data/state/
  - cp -R config dist/data/state/config

- name: build_frontend
  image: node:12-alpine
  depends_on: [ build_prepare ]
  commands:
  - apk add make git > /dev/null
  - npm install --silent
  - make build_frontend

- name: build_backend
  image: golang:1.12-stretch
  depends_on: [ build_prepare ]
  environment:
    CGO_LDFLAGS_ALLOW: '-fopenmp'
  commands:
  - apt-get update > /dev/null && apt-get install -y libglib2.0-dev curl make > /dev/null
  - make build_init
  - find server/plugin/plg_* -type f -name '*.a' -exec mv {} /usr/local/lib/ \;
  - go get -t ./server/...
  - make build_backend
  - timeout 1 ./dist/filestash || true

- name: test_prepare
  image: alpine:latest
  depends_on: [ clone ]
  environment:
    PASSWORD:
      from_secret: GIT_PASSWORD
  commands:
  - apk add git > /dev/null
  - chmod -R o+r+w+x ./dist/data
  - cd ../ && git clone "https://readonly:$PASSWORD@git.kerjean.me/filestash/test.git"
  - chmod -R 777 ./test/

- name: test_frontend
  image: node:8-alpine
  depends_on: [ test_prepare, build_frontend ]
  commands:
  - cd ../test/unit_js
  - npm install --silent
  - npm test

- name: test_backend
  image: golang:1.12-alpine
  depends_on: [ test_prepare, build_backend ]
  commands:
  - apk add git gcc libc-dev poppler-utils > /dev/null
  - cp ../test/assets/* /tmp/
  - go get -t ../test/unit_go/...
  - go test -v --tags "fts5" ../test/unit_go/...

- name: test_e2e
  image: machines/puppeteer
  depends_on: [ build_frontend, build_backend ]
  environment:
    ADMIN_PASSWORD: $$2a$$10$$9OFbPZV4lYpYjU5eUi91o.kgeMyCuW11j878YBRri3gBwccq2lSFy
    APP_URL: http://127.0.0.1:8334
    CI: true
  commands:
  - ./dist/filestash > /dev/null &
  - cd ../test/e2e
  - npm install > /dev/null
  - node servers/webdav.js > /dev/null &
  - npm test

- name: release_prepare
  image: debian:stable-slim
  depends_on: [ test_e2e ]
  environment:
    GPG_PRIVATE:
      from_secret: GPG_PRIVATE
    GPG_PASSPHRASE:
      from_secret: GPG_PASSPHRASE
  commands:
  - cp -R config/* ./dist/data/state/config/
  - chmod -R o-r-w-x- ./dist/
  - apt-get update > /dev/null && apt-get install -y gnupg1 curl > /dev/null
  - mv dist filestash
  - tar -cf filestash_linux-amd64.tar ./filestash
  - curl -s "https://downloads.filestash.app/gpg?private=$GPG_PASSPHRASE" > /tmp/private.key
  - echo $GPG_PASSPHRASE | gpg1 --import /tmp/private.key
  - echo $GPG_PASSPHRASE | gpg1 --sign --passphrase-fd 0 --default-key mickael@kerjean.me --no-tty filestash_linux-amd64.tar

- name: release_publish
  image: appleboy/drone-scp
  depends_on: [ release_prepare ]
  when:
    branch: master
  settings:
    command_timeout: 30m
    host: hal.kerjean.me
    user:
      from_secret: SSH_USERNAME
    password:
      from_secret: SSH_PASSWORD
    source: filestash_linux-amd64.tar.gpg
    target: /app/pages/data/projects/filestash/downloads/latest/

- name: release_docker
  image: plugins/docker
  depends_on: [ release_publish ]
  when:
    branch: master
  settings:
    dockerfile: ./docker/Dockerfile
    repo: machines/filestash
    username:
      from_secret: DOCKER_USERNAME
    password:
      from_secret: DOCKER_PASSWORD
    tags: latest

- name: release_deploy
  image: appleboy/drone-ssh
  depends_on: [ release_docker ]
  when:
    branch: master
  settings:
    command_timeout: 10m
    host: hal.kerjean.me
    user:
      from_secret: SSH_USERNAME
    password:
      from_secret: SSH_PASSWORD
    port: 22
    script:
    - cd /app/filestash/
    - docker-compose pull
    - docker-compose up -d --force-recreate --build
    - docker image prune -f

- name: release_report
  image: plugins/irc
  depends_on: [ release_deploy ]
  when:
    branch: master
  settings:
    prefix: build
    nick: blipblip
    channel: filestash
    host: chat.freenode.net
    port: 6667
    template: >
      {{#success build.status}}
        Build success: https://github.com/mickael-kerjean/filestash/commit/{{build.commit}}
      {{else}}
        Build failure: er}} https://github.com/mickael-kerjean/filestash/commit/{{build.commit}}
      {{/success}}