---
kind: pipeline
type: kubernetes
name: docker
concurrency:
  limit: 1
trigger:
  branch:
    - main
  event:
    - push
metadata:
  namespace: git
steps:
  - name: Fetch tags
    image: alpine/git
    commands:
      - git fetch --tags
  - name: Build site
    image: node:current
    volumes:
      - name: cache
        path: /cache
    environment:
      YARN_CACHE_FOLDER: /cache/.yarn-docker
      NODE_CONFIG_ENV: default
    commands:
      - apt update && apt install -y git
      - yarn install
      - yarn build
  - name: build docker image
    image: docker
    privileged: true
    volumes:
      - name: cache
        path: /cache
    environment:
      TOKEN:
        from_secret: docker_hub
    commands:
      - dockerd &
      - docker login -u voidic -p $TOKEN
      - docker buildx create --name mybuilder --bootstrap --use
      - docker buildx build -t voidic/snort:latest --platform linux/amd64,linux/arm64 --push -f Dockerfile.prebuilt .
      - kill $(cat /var/run/docker.pid)
volumes:
  - name: cache
    claim:
      name: docker-cache
---
kind: pipeline
type: kubernetes
name: test-lint
concurrency:
  limit: 1
metadata:
  namespace: git
steps:
  - name: Test/Lint
    image: node:current
    volumes:
      - name: cache
        path: /cache
    environment:
      YARN_CACHE_FOLDER: /cache/.yarn-test
      NODE_CONFIG_ENV: default
    commands:
      - yarn install
      - yarn build
      - yarn test
      - yarn workspace @snort/app eslint
      - yarn prettier --check .
volumes:
  - name: cache
    claim:
      name: docker-cache
---
kind: pipeline
type: kubernetes
name: crowdin
concurrency:
  limit: 1
trigger:
  branch:
    - main
  event:
    - push
metadata:
  namespace: git
steps:
  - name: Push/Pull translations
    image: node:current
    volumes:
      - name: cache
        path: /cache
    environment:
      YARN_CACHE_FOLDER: /cache/.yarn-translations
      NODE_CONFIG_ENV: default
      TOKEN:
        from_secret: gitea
      CTOKEN:
        from_secret: crowdin
    commands:
      - git config --global user.email drone@v0l.io
      - git config --global user.name "Drone CI"
      - git remote set-url origin https://drone:$TOKEN@git.v0l.io/Kieran/snort.git
      - yarn install
      - npx @crowdin/cli upload sources -b main -T $CTOKEN
      - npx @crowdin/cli pull -b main -T $CTOKEN
      - yarn prettier --write .
      - git add .
      - >
        if output=$(git status --porcelain) && [ -n "$output" ]; then
          git commit -a -m "chore: Update translations"
          git push -u origin main
        fi
volumes:
  - name: cache
    claim:
      name: docker-cache
---
kind: pipeline
type: kubernetes
name: docker-release
concurrency:
  limit: 1
trigger:
  event:
    - tag
metadata:
  namespace: git
steps:
  - name: Fetch tags
    image: alpine/git
    commands:
      - git fetch --tags
  - name: Build site
    image: node:current
    volumes:
      - name: cache
        path: /cache
    environment:
      YARN_CACHE_FOLDER: /cache/.yarn-docker-
      NODE_CONFIG_ENV: default
    commands:
      - apt update && apt install -y git
      - yarn install
      - yarn build
  - name: build docker image
    image: docker
    privileged: true
    volumes:
      - name: cache
        path: /cache
    environment:
      TOKEN:
        from_secret: docker_hub
    commands:
      - dockerd &
      - docker login -u voidic -p $TOKEN
      - docker buildx create --name mybuilder --bootstrap --use
      - docker buildx build -t voidic/snort:$DRONE_TAG --platform linux/amd64,linux/arm64 --push -f Dockerfile.prebuilt .
      - kill $(cat /var/run/docker.pid)
volumes:
  - name: cache
    claim:
      name: docker-cache
