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

platform:
  os: linux
  arch: amd64

steps:
  - name: generate go and OpenAPI files
    image: golang:1-alpine
    commands:
      - sed -i '/npm run build/d;/npm run generate:api/d' web/assets.go web-admin/assets.go
      - go install github.com/swaggo/swag/cmd/swag@latest
      - go generate ./...

  - name: frontend
    image: node:24-alpine
    commands:
      - apk --no-cache add tar
      - yarn config set network-timeout 100000
      - yarn --cwd web install
      - tar --transform="s@.@./happydomain-${DRONE_COMMIT}@" --exclude-vcs --exclude=./web/node_modules/.cache -czf /dev/shm/happydomain-src.tar.gz .
      - mkdir deploy
      - mv /dev/shm/happydomain-src.tar.gz deploy
      - yarn --cwd web --offline run svelte-kit sync && yarn --cwd web --offline generate:api && sed -i "s/hey-api\.ts';/hey-api';/" web/src/lib/api-base/client.gen.ts
      - yarn --cwd web --offline build
      - yarn --cwd web-admin --offline run svelte-kit sync && yarn --cwd web-admin --offline generate:api && sed -i "s/hey-api\.ts';/hey-api';/" web/src/lib/api-admin/client.gen.ts
      - yarn --cwd web-admin --offline build

  - name: backend-commit
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X "main.Version=${DRONE_BRANCH}-${DRONE_COMMIT}" -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
      - ln deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} happydomain
    environment:
      CGO_ENABLED: 0
    when:
      event:
        exclude:
          - tag

  - name: backend-tag
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X main.Version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
      - ln deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} happydomain
    environment:
      CGO_ENABLED: 0
    when:
      event:
        - tag

  - name: generate SBOM
    image: nemunaire/drone-syft
    settings:
      select_catalogers: go,npm
      output: spdx-json=deploy/happydomain-sbom.spdx.json
      source_name: happyDomain

  - name: build-commit macOS
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X "main.Version=${DRONE_BRANCH}-${DRONE_COMMIT}" -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-darwin-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
    environment:
      CGO_ENABLED: 0
      GOOS: darwin
      GOARCH: amd64
    when:
      event:
        exclude:
          - tag

  - name: build-tag macOS
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X "main.Version=${DRONE_TAG##v}" -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-darwin-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
    environment:
      CGO_ENABLED: 0
      GOOS: darwin
      GOARCH: amd64
    when:
      event:
        - tag

  - name: deploy
    image: plugins/s3
    settings:
      endpoint: https://blob.nemunai.re
      path_style: true
      region: garage
      bucket: happydomain-dl
      access_key:
        from_secret: s3_access_key
      secret_key:
        from_secret: s3_secret_key
      source: deploy/*
      target: /${DRONE_BRANCH//\//-}/
      strip_prefix: deploy/
    when:
      event:
        - push
      branch:
        exclude:
          - renovate/*

  - name: deploy release
    image: plugins/s3
    settings:
      endpoint: https://blob.nemunai.re
      path_style: true
      region: garage
      bucket: happydomain-dl
      access_key:
        from_secret: s3_access_key
      secret_key:
        from_secret: s3_secret_key
      source: deploy/*
      target: /${DRONE_TAG}/
      strip_prefix: deploy/
    when:
      event:
        - tag

  - name: publish on Docker Hub
    image: plugins/docker
    settings:
      repo: happydomain/happydomain
      auto_tag: true
      auto_tag_suffix: ${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
      dockerfile: Dockerfile-builded
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password

  - name: publish release on gitea
    image: plugins/gitea-release
    settings:
      api_key:
        from_secret: git_nemunaire_token
      base_url: https://git.nemunai.re
      draft: true
      files:
        - happydomain-src.tar.gz
        - happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
        - happydomain-darwin-${DRONE_STAGE_ARCH}
        - happydomain-sbom.spdx.json
    when:
      event:
        - tag

  - name: publish release on codeberg
    image: plugins/gitea-release
    settings:
      api_key:
        from_secret: codeberg_token
      base_url: https://codeberg.org
      draft: true
      files:
        - happydomain-src.tar.gz
        - happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
        - happydomain-darwin-${DRONE_STAGE_ARCH}
        - happydomain-sbom.spdx.json
    when:
      event:
        - tag

  - name: publish release on github
    image: plugins/github-release
    settings:
      api_key:
        from_secret: github_release_token
      draft: true
      github_url: https://github.com
      files:
        - happydomain-src.tar.gz
        - happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
        - happydomain-darwin-${DRONE_STAGE_ARCH}
        - happydomain-sbom.spdx.json
    when:
      event:
        - tag

trigger:
  branch:
    exclude:
      - renovate/*
  event:
    - cron
    - push
    - tag

---
kind: pipeline
type: docker
name: build-arm64

platform:
  os: linux
  arch: arm64

steps:
  - name: generate go and OpenAPI files
    image: golang:1-alpine
    commands:
      - sed -i '/npm run build/d;/npm run generate:api/d' web/assets.go web-admin/assets.go
      - go install github.com/swaggo/swag/cmd/swag@latest
      - go generate ./...

  - name: frontend
    image: node:24-alpine
    commands:
      - cd web
      - npm install --network-timeout=100000
      - npx svelte-kit sync && npm run generate:api
      - npm test
      - npm run build
      - cd ../web-admin
      - npx svelte-kit sync && npm run generate:api
      - npm test
      - npm run build

  - name: backend-commit
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X "main.Version=${DRONE_BRANCH}-${DRONE_COMMIT}" -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
      - ln deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} happydomain
    environment:
      CGO_ENABLED: 0
    when:
      event:
        exclude:
          - tag

  - name: backend-tag
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X main.Version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
      - ln deploy/happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} happydomain
    environment:
      CGO_ENABLED: 0
    when:
      event:
        - tag

  - name: vet and tests
    image: golang:1-alpine
    commands:
      - apk --no-cache add build-base git
      - go vet ./...
      - go test ./...
    environment:
      CGO_ENABLED: 0

  - name: build-commit macOS
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X "main.Version=${DRONE_BRANCH}-${DRONE_COMMIT}" -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-darwin-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
    environment:
      CGO_ENABLED: 0
      GOOS: darwin
      GOARCH: arm64
    when:
      event:
        exclude:
          - tag

  - name: build-tag macOS
    image: golang:1-alpine
    commands:
      - apk add --no-cache git
      - go build -tags netgo,swagger,web -ldflags '-w -X "main.Version=${DRONE_TAG##v}" -X main.build=${DRONE_BUILD_NUMBER}' -o deploy/happydomain-darwin-${DRONE_STAGE_ARCH} ./cmd/happyDomain/
    environment:
      CGO_ENABLED: 0
      GOOS: darwin
      GOARCH: arm64
    when:
      event:
        - tag

  - name: deploy
    image: plugins/s3
    settings:
      endpoint: https://blob.nemunai.re
      path_style: true
      region: garage
      bucket: happydomain-dl
      access_key:
        from_secret: s3_access_key
      secret_key:
        from_secret: s3_secret_key
      source: deploy/*
      target: /${DRONE_BRANCH//\//-}/
      strip_prefix: deploy/
    when:
      event:
        - push
      branch:
        exclude:
          - renovate/*

  - name: deploy release
    image: plugins/s3
    settings:
      endpoint: https://blob.nemunai.re
      path_style: true
      region: garage
      bucket: happydomain-dl
      access_key:
        from_secret: s3_access_key
      secret_key:
        from_secret: s3_secret_key
      source: deploy/*
      target: /${DRONE_TAG}/
      strip_prefix: deploy/
    when:
      event:
        - tag

  - name: publish on Docker Hub
    image: plugins/docker
    settings:
      repo: happydomain/happydomain
      auto_tag: true
      auto_tag_suffix: ${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
      dockerfile: Dockerfile-builded
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password

  - name: publish release on gitea
    image: plugins/gitea-release
    settings:
      api_key:
        from_secret: git_nemunaire_token
      base_url: https://git.nemunai.re
      draft: true
      prerelease: true
      files:
        - happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
        - happydomain-darwin-${DRONE_STAGE_ARCH}
    when:
      event:
        - tag

  - name: publish release on codeberg
    image: plugins/gitea-release
    settings:
      api_key:
        from_secret: codeberg_token
      base_url: https://codeberg.org
      draft: true
      prerelease: true
      files:
        - happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
        - happydomain-darwin-${DRONE_STAGE_ARCH}
    when:
      event:
        - tag

  - name: publish release on github
    image: plugins/github-release
    settings:
      api_key:
        from_secret: github_release_token
      draft: true
      prerelease: true
      github_url: https://github.com
      files:
        - happydomain-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH}
        - happydomain-darwin-${DRONE_STAGE_ARCH}
    when:
      event:
        - tag

trigger:
  event:
    - cron
    - push
    - tag

---
kind: pipeline
name: docker-manifest

platform:
  os: linux
  arch: arm64

steps:
  - name: publish on Docker Hub
    image: plugins/manifest
    settings:
      auto_tag: true
      ignore_missing: true
      spec: .drone-manifest.yml
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password

trigger:
  branch:
    exclude:
      - renovate/*
  event:
    - cron
    - push
    - tag

depends_on:
  - build-amd64
  - build-arm64
