---
kind: pipeline
type: docker
name: default

environment:
  K8S_REPO_URL: ssh://git@git.novaloop.ch:8022/novaloop-mastodon/mastodon-k8s.git

image_pull_secrets:
  - docker-auth-config

.image-build: &image-build
  - echo building docker image "git.novaloop.ch/${DRONE_REPO}:${DRONE_COMMIT_SHA}"
  - mkdir -p /kaniko/.docker
  - echo "$DOCKER_AUTH_CONFIG" > /kaniko/.docker/config.json
  - >-
    /kaniko/executor
    --context .
    --build-arg "ARG_APP_VERSION=${DRONE_TAG}"
    --cache=true
    --compressed-caching=false
    --snapshotMode=redo
    --use-new-run
    --dockerfile "deployment/$ENVIRONMENT/$ENVIRONMENT.dockerfile"
    --destination "git.novaloop.ch/${DRONE_REPO}:${DRONE_COMMIT_SHA}"
    --destination "git.novaloop.ch/${DRONE_REPO}:${DRONE_TAG}"
    --destination "git.novaloop.ch/${DRONE_REPO}:latest"

.image-deploy: &image-deploy
  - echo deploy ${DRONE_TAG} of ${DRONE_REPO} to $ENVIRONMENT
  # configure git
  - mkdir -p ~/.ssh
  - echo "$DRONE_CI_SSH_KEY" > ~/.ssh/id_rsa
  - chmod 600 ~/.ssh/id_rsa
  - ssh-keyscan -p 8022 -H git.novaloop.ch > ~/.ssh/known_hosts
  - git config --global user.name ${DRONE_REPO}
  - git config --global user.email ${DRONE_REPO}@git.novaloop.ch

  # clone k8s repo
  - git clone -b "$ENVIRONMENT" "$K8S_REPO_URL" k8s

  # update manifests
  - cd "k8s/mastolists/overlays/$ENVIRONMENT"
  - kustomize edit set image "git.novaloop.ch/${DRONE_REPO}=git.novaloop.ch/${DRONE_REPO}:${DRONE_TAG}"
  - git add .
  - git commit -m "deploy ${DRONE_REPO_NAME} updating $ENVIRONMENT to ${DRONE_TAG}"
  - git push

steps:
  - name: build staging image
    image: gcr.io/kaniko-project/executor:v1.9.1-debug # without DIND requirement
    commands:
      *image-build
    environment:
      ENVIRONMENT: staging
      DOCKER_AUTH_CONFIG: 
        from_secret: docker-auth-config
    when:
      ref: 
        include:
          - refs/tags/v[0-9]*.[0-9]*.[0-9]*-* # glob pattern matching (limited https://pkg.go.dev/github.com/bmatcuk/doublestar?utm_source=godoc)
  
  - name: deploy staging image
    image: git.novaloop.ch/novaloop-hosting/gitlab-build-docker-image:latest
    commands:
      *image-deploy
    environment:
      ENVIRONMENT: staging
      DRONE_CI_SSH_KEY: 
        from_secret: DRONE_CI_SSH_KEY
    when:
      ref: 
        include:
          - refs/tags/v[0-9]*.[0-9]*.[0-9]*-* # glob pattern matching (limited https://pkg.go.dev/github.com/bmatcuk/doublestar?utm_source=godoc)

  - name: build production image
    image: gcr.io/kaniko-project/executor:v1.9.1-debug # without DIND requirement
    commands:
      *image-build
    environment:
      ENVIRONMENT: production
      DOCKER_AUTH_CONFIG: 
        from_secret: docker-auth-config
    when:
      ref: 
        include:
          - refs/tags/v[0-9]*.[0-9]*.[0-9]* # glob pattern matching (limited https://pkg.go.dev/github.com/bmatcuk/doublestar?utm_source=godoc)
        exclude:
          - refs/tags/v[0-9]*.[0-9]*.[0-9]*-*
          
  - name: deploy production image
    image: git.novaloop.ch/novaloop-hosting/gitlab-build-docker-image:latest
    commands:
      *image-deploy
    environment:
      ENVIRONMENT: production
      DRONE_CI_SSH_KEY: 
        from_secret: DRONE_CI_SSH_KEY
    when:
      ref: 
        include:
          - refs/tags/v[0-9]*.[0-9]*.[0-9]* # glob pattern matching (limited https://pkg.go.dev/github.com/bmatcuk/doublestar?utm_source=godoc)
        exclude:
          - refs/tags/v[0-9]*.[0-9]*.[0-9]*-*
