---
kind: pipeline
type: docker
name: Build

platform:
  os: linux
  arch: amd64

steps:
  - name: build
    image: ruby:3.1-slim-bullseye
    commands:
      - apt update && apt install -y --no-install-recommends build-essential git
      - make install
      - make build

---
kind: pipeline
type: docker
name: Publish

platform:
  os: linux
  arch: amd64

steps:
  - name: Build and publish docker image
    image: plugins/docker
    settings:
      registry: ghcr.io
      repo: ghcr.io/kevinmidboe/schleppe-lab
      dockerfile: Dockerfile
      username:
        from_secret: GITHUB_USERNAME
      password:
        from_secret: GHCR_UPLOAD_TOKEN
      tags:
        - latest
        - ${DRONE_COMMIT_SHA}

trigger:
  event:
    include:
      - push
    exclude:
      - pull_request
  branch:
    - main

depends_on:
  - Build

---
kind: pipeline
type: docker
name: Deploy

platform:
  os: linux
  arch: amd64

steps:
  - name: Prepare kubernetes environment
    image: alpine/k8s:1.25.15
    commands:
      - mkdir -p /root/.kube
      - 'curl -s
        -H "X-Vault-Token: $VAULT_TOKEN"
        $VAULT_HOST/v1/schleppe/data/kazan/${DRONE_REPO_NAME}
        | jq -r ".data.data.KUBE_CONFIG" > /root/.kube/config'
      - 'curl -s
        -H "X-Vault-Token: $VAULT_TOKEN"
        $VAULT_HOST/v1/schleppe/data/kazan/${DRONE_REPO_NAME}
        | jq -r ".data | .data | .[\"ghcr-login-secret\"]" > /root/.kube/dockerconfig.json'
    environment:
      VAULT_TOKEN:
        from_secret: VAULT_TOKEN
      VAULT_HOST:
        from_secret: VAULT_HOST
    volumes:
    - name: kube-config
      path: /root/.kube

  - name: Deploy to kubernetes
    image: alpine/k8s:1.25.15
    commands:
      - export DOCKER_CONFIG_BASE64=$(cat /root/.kube/dockerconfig.json | tr -d "\n\t " | base64 -w 0)
      - export IMAGE="ghcr.io/kevinmidboe/${DRONE_REPO_NAME}:${DRONE_COMMIT_SHA}"
      - cat .kubernetes/*.yml
        | envsubst
        | kubectl --kubeconfig=/root/.kube/config apply -f -
    volumes:
    - name: kube-config
      path: /root/.kube

trigger:
  event:
    include:
      - push
    exclude:
      - pull_request
  branch:
    - main

depends_on:
  - Build
  - Publish

volumes:
- name: kube-config
  temp: {}

---
kind: signature
hmac: fe9955b547b6cb815b7989dd203a8e4ee91e33cefedb16ee2232795875ba9b4b

...
