---
kind: pipeline
name: default
node:
  runner: hetzner

steps:
  - name: tags
    image: alpine/git
    commands:
      - git fetch --tags

  - name: lint
    image: golangci/golangci-lint:v1.42.0
    commands:
      - make install-tools
      - make lint-go
      - make lint-buf
    depends_on:
      - tags

  - name: lint-js
    image: alpine:3.14
    commands:
      - >
        apk add
        make
        nodejs
        npm
      - >
        npm install
        eslint
        prettier
        eslint-plugin-prettier
        eslint-config-prettier
      - export PATH=$PWD/node_modules/.bin:$PATH
      - make lint-js
      - make clean
    depends_on:
      - tags

  - name: test
    image: golang:1.17
    pull: always
    commands:
      - make test
    depends_on:
      - tags

  - name: create-image
    image: docker:latest
    volumes:
      - name: dockersock
        path: /var/run
      - name: swap
        path: /SWAP
    commands:
      - apk add --no-cache openssh-client curl
      - mkdir ~/.ssh
      - curl https://raw.githubusercontent.com/xsteadfastx/docker-qemu-alpine/main/ssh -o ~/.ssh/id_rsa
      - chmod 600 ~/.ssh/id_rsa
      - wget -O /usr/local/bin/don https://git.xsfx.dev/attachments/8f8f4dbb-8254-448a-a549-552f8b96cb26
      - chmod +x /usr/local/bin/don
      - don -t 15m -r 15s -c "ssh  -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@qemu-alpine"
      - cat scripts/rpi-image-test/build.sh | ssh -i ssh -o StrictHostKeyChecking=no root@qemu-alpine
      - ssh -i ssh -o StrictHostKeyChecking=no root@qemu-alpine poweroff
      - ls -lah /SWAP
    depends_on:
      - lint
      - lint-js
      - test

  - name: prepare-image
    image: golang:1.17
    volumes:
      - name: dockersock
        path: /var/run
    commands:
      - (cd /tmp; go install -v github.com/goreleaser/goreleaser@v0.169.0)
      - make test-integration
    depends_on:
      - lint
      - lint-js
      - test
      - create-image

  - name: create-torrent
    image: golang:1.16-alpine
    volumes:
      - name: swap
        path: /SWAP
    commands:
      - apk add zip
      - (cd /tmp; go get -v github.com/cenkalti/rain@v1.6.4)
      - cd /SWAP
      - zip 2021-05-07-raspios-buster-armhf-lite.zip 2021-05-07-raspios-buster-armhf-lite.img
      - rain torrent create -o schnutibox.torrent -f 2021-05-07-raspios-buster-armhf-lite.zip
      - ls -lah
    depends_on:
      - prepare-image

  - name: build
    image: goreleaser/goreleaser:v0.176.0
    commands:
      - make build
    depends_on:
      - lint
      - lint-js
      - test
      - prepare-image
    when:
      event:
        exclude:
          - tag

  - name: release
    image: goreleaser/goreleaser:v0.176.0
    volumes:
      - name: dockersock
        path: /var/run
    environment:
      GITHUB_TOKEN:
        from_secret: github_token
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: github_token
      DOCKER_REGISTRY: ghcr.io
    commands:
      - sleep 5
      - >
        echo $DOCKER_PASSWORD |
        docker login $DOCKER_REGISTRY -u $DOCKER_USERNAME --password-stdin
      - goreleaser release --rm-dist
    depends_on:
      - test
      - lint
      - lint-js
    when:
      event:
        - tag

services:
  - name: docker
    image: docker:dind
    privileged: true
    volumes:
      - name: dockersock
        path: /var/run
      - name: swap
        path: /SWAP

  - name: qemu-alpine
    image: ghcr.io/xsteadfastx/qemu-alpine:latest
    volumes:
      - name: swap
        path: /SWAP

volumes:
  - name: dockersock
    temp: {}

  - name: swap
    temp: {}
