#!groovy

@Library('szint-global-pipeline-libraries@2.2.0')  _
import de.sueddeutsche.docker.ImageName

def imageName
def nodeVersion

stage("prepare") {
  node {
    checkout scm
    if (params.branch) {
      git branch: params.branch
    }

    imageName = new ImageName([
      appName: getAppName(),
      version: params.version ? params.version : getNPMPackageVersion()
    ])

  }

  nodeVersion = getBuildPipelineNodeVersion()
}

stage("bundle") {
  node {
    nvm('version': "v${nodeVersion}") {
      sh "npm run build"
    }
  }
}

stage("build") {
  node {
    sh """ \
      docker build \
      -t ${imageName.absoluteWithVersion} \
      --build-arg VERSION=${imageName.version} \
      --build-arg NPM_TOKEN=$NPM_TOKEN \
      --build-arg GITHASH=\$(git log -n1 --pretty='%h') \
      --build-arg BUILDNUMBER=${BUILD_NUMBER} \
      --build-arg NODE_VERSION=${nodeVersion} \
      -f ./deployment/production.Dockerfile \
      .
    """
  }
}

stage("publish") {
  node {
    sh "docker push ${imageName.absoluteWithVersion}"
  }
}
