library 'status-jenkins-lib@v1.3.3'

pipeline {
  agent { label 'linux' }

  parameters {
    string(
      name: 'BRANCH',
      defaultValue: 'develop',
      description: 'Name of branch to build.'
    )
    booleanParam(
      name: 'RELEASE',
      defaultValue: false,
      description: 'Enable to create build for release.',
    )
  }

  options {
    timestamps()
    disableConcurrentBuilds()
    /* Go requires a certain directory structure */
    checkoutToSubdirectory('src/github.com/status-im/status-go')
    /* manage how many builds we keep */
    buildDiscarder(logRotator(
      numToKeepStr: '5',
      daysToKeepStr: '30',
    ))
  }

  environment {
    TARGET = "docker"
    REPO   = "${env.WORKSPACE}/src/github.com/status-im/status-go"
    GOPATH = "${env.WORKSPACE}"
    PATH   = "/usr/local/go/bin:${env.PATH}:${env.GOPATH}/bin"
    /* docker image settings */
    IMAGE_NAME  = "statusteam/status-go"
    IMAGE_TAG   = "deploy-test"
  }

  stages {
    stage('Prep') { steps { dir(env.REPO) { script {
      println("Output: ${env.IMAGE_NAME}:${env.IMAGE_TAG}")
    } } } }

    stage('Build') { steps { dir(env.REPO) { script {
      sh 'make docker-image'
      image = docker.image("${env.IMAGE_NAME}:v${utils.getVersion()}")
    } } } }

    stage('Push') { steps { dir(env.REPO) { script {
      withDockerRegistry([credentialsId: "dockerhub-statusteam-auto", url: ""]) {
        image.push()
      }
    } } } }

    stage('Deploy') {
      when { expression { params.RELEASE == true } }
      steps { dir(env.REPO) { script {
        withDockerRegistry([credentialsId: "dockerhub-statusteam-auto", url: ""]) {
          image.push(env.IMAGE_TAG)
        }
    } } } }
  } // stages
  post {
    success { script { github.notifyPR(true) } }
    failure { script { github.notifyPR(false) } }
    always  { dir(env.REPO) {
      sh 'make clean-docker-images'
    } }
  } // post
} // pipeline