#!/usr/bin/env groovy

import groovy.json.JsonOutput

properties([
    // Parameters set by Skadi for deployments, not set for regular builds.
    parameters([
        string(name: 'GIT_COMMIT', defaultValue: '', description: 'Git Sha', ),
        string(name: 'ENVIRONMENT', defaultValue: '', description: 'Environment', ),
        string(name: 'BRANCH', defaultValue: '', description: 'Branch', ),
        string(name: 'SKADI_ID', defaultValue: '', description: 'Skadi host', ),
        string(name: 'HOSTS', defaultValue: '', description: 'Hosts to deploy to (unused)', )
 ])
])

node {
  stage("Checkout") {
    checkout scm
  }

  echo "branch: $env.BRANCH_NAME"
  params.each { key, value ->
    echo "param $key=$value"
  }

  def jenkinsBranch = env.BRANCH_NAME
  def skadiBranch = params.BRANCH

  if (skadiBranch != "") {
    if (jenkinsBranch == "master" && skadiBranch != jenkinsBranch) {
      stage("Delegate") {
        def downstreamParams = params.collect { key, value -> string(name: key, value: value) }
        build(job: skadiBranch, parameters: downstreamParams)
      }
    } else {
      stage("Deploy") {
        sh "echo yay from other branch"
      }
    }
  } else {
    stage("Build") {
      sh "echo build yay"
    }
  }
}
