pipeline {
  agent { label 'builder' }
  options { timestamps(); disableConcurrentBuilds() }
  environment { TAG = "b${env.BUILD_NUMBER}" }
  stages {
    stage('Checkout') {
      steps { checkout scm }
    }
    stage('Maven') {
      steps {
        container('maven') {
          withCredentials([usernamePassword(credentialsId: 'nexus-creds', usernameVariable: 'NU', passwordVariable: 'NP')]) {
            sh '''#!/bin/sh -e
cat > settings.xml <<EOF
<settings>
  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>https://nexus.otone.run/repository/maven-central/</url>
    </mirror>
  </mirrors>
  <servers>
    <server>
      <id>nexus-releases</id>
      <username>${NU}</username>
      <password>${NP}</password>
    </server>
  </servers>
</settings>
EOF
cd backend-maven
mvn -s ../settings.xml -B -q -Drevision=1.0.$BUILD_NUMBER deploy
'''
          }
        }
      }
    }
    stage('Gradle') {
      steps {
        container('gradle') {
          withCredentials([usernamePassword(credentialsId: 'nexus-creds', usernameVariable: 'NU', passwordVariable: 'NP')]) {
            sh 'cd backend-gradle && gradle --no-daemon -q publish -PnexusUser=$NU -PnexusPass=$NP'
          }
        }
      }
    }
    stage('Frontend') {
      steps {
        container('node') {
          withCredentials([usernamePassword(credentialsId: 'nexus-creds', usernameVariable: 'NU', passwordVariable: 'NP')]) {
            sh '''#!/bin/sh -e
              cd frontend
              echo "registry=https://nexus.otone.run/repository/npm-proxy/" > .npmrc
              npm ci
              npm run build
              cd pkg
              npm version 1.0.$BUILD_NUMBER --allow-same-version --no-git-tag-version
              printf '//nexus.otone.run/repository/npm-hosted/:_auth=%s\nregistry=https://nexus.otone.run/repository/npm-hosted/\n' "$(printf '%s:%s' "$NU" "$NP" | base64)" > .npmrc
              npm publish
            '''
          }
        }
      }
    }
    stage('Image') {
      steps {
        container('kaniko') {
          withCredentials([usernamePassword(credentialsId: 'harbor-creds', usernameVariable: 'HU', passwordVariable: 'HP')]) {
            sh '''#!/busybox/sh -e
              mkdir -p /kaniko/.docker
              printf '{"auths":{"harbor.otone.run":{"auth":"%s"}}}' "$(printf '%s:%s' "$HU" "$HP" | base64 | tr -d '\n')" > /kaniko/.docker/config.json
              /kaniko/executor --context $WORKSPACE --dockerfile docker/backend.Dockerfile --destination harbor.otone.run/otone/demo-api:$TAG --cleanup
              /kaniko/executor --context $WORKSPACE --dockerfile docker/frontend.Dockerfile --destination harbor.otone.run/otone/demo-web:$TAG --cleanup
            '''
          }
        }
      }
    }
    stage('Deploy') {
      steps {
        container('kubectl') {
          withCredentials([file(credentialsId: 'kubeconfig', variable: 'KC')]) {
            sh 'sed "s/__TAG__/$TAG/g" k8s.yaml | kubectl --kubeconfig $KC apply -f -'
          }
        }
      }
    }
    stage('Verify') {
      steps {
        container('maven') {
          retry(10) {
            sh 'sleep 6; curl -sf https://demo.otone.run/ >/dev/null && curl -sf https://demo-api.otone.run/ >/dev/null && echo ALL-GREEN'
          }
        }
      }
    }
  }
}
