pipeline {
    agent none

    options {
        timestamps()
    }

    parameters{
        string defaultValue: 'registry.milvus.io', description: 'Local Docker registry URL', name: 'LOCAL_DOKCER_REGISTRY_URL', trim: true
        string defaultValue: 'registry-1.docker.io', description: 'Remote Docker registry URL', name: 'REMOTE_DOKCER_REGISTRY_URL', trim: true
        string defaultValue: 'milvus-docker-access-token', description: 'Remote Docker credentials id', name: 'REMOTE_DOCKER_CREDENTIALS_ID', trim: true
        string(defaultValue: "master", description: 'Milvus server version', name: 'BRANCH')
    }

    environment {
        DAILY_BUILD_VERSION = VersionNumber([
            versionNumberString : '${BUILD_DATE_FORMATTED, "yyyyMMdd"}'
        ]);
    }

    stages {
        stage('Push Daily Docker Images') {
            matrix {
                agent none
                axes {
                    axis {
                        name 'OS_NAME'
                        values 'ubuntu18.04', 'centos7'
                    }

                    axis {
                        name 'CPU_ARCH'
                        values 'amd64'
                    }

                    axis {
                        name 'BINARY_VERSION'
                        values 'gpu', 'cpu'
                    }
                }

                stages {
                    stage("Publish Docker Images") {
                        environment {
                            DOCKER_VERSION = "${params.BRANCH}-${BINARY_VERSION}-${OS_NAME}-release"
                            REMOTE_DOCKER_VERSION = "${params.BRANCH}-${OS_NAME}-${BINARY_VERSION}-${DAILY_BUILD_VERSION}"
                            REMOTE_DOCKER_LATEST_VERSION = "${params.BRANCH}-${OS_NAME}-${BINARY_VERSION}-latest"
                        }

                        agent {
                            kubernetes {
                                label "${OS_NAME}-${BINARY_VERSION}-publish-${env.BUILD_NUMBER}"
                                defaultContainer 'jnlp'
                                yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: publish
    componet: docker
spec:
  containers:
  - name: publish-images
    image: registry.milvus.io/library/docker:v1.0.0
    securityContext:
      privileged: true
    command:
    - cat
    tty: true
    resources:
      limits:
        memory: "4Gi"
        cpu: "1.0"
      requests:
        memory: "2Gi"
        cpu: "0.5"
    volumeMounts:
    - name: docker-sock
      mountPath: /var/run/docker.sock
  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock
                                """
                            }
                        }

                        stages {
                            stage('Publish') {
                                steps {
                                    container('publish-images') {
                                        script {
                                            load "${env.WORKSPACE}/ci/jenkinsfile/publishDailyImages.groovy"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
