library 'whatsout'

node('deploy-gitops'){
	container('run'){
		stage( 'Checkout'){
			checkout scm
		}

		def gitTag = "${VERSION_TO_DEPLOY}"
		tag = gitTag.replaceFirst('v', '')

		if(params.ENVIRONMENT) {
			wrap([$class: 'BuildUser']) {
				sshagent(credentials: ['gitops']) {
					stage('backend') {
						sh( "fnxctl deploy --method=gitops --env='${params.ENVIRONMENT}' ${tag}")
					}

				}
			}
			env = params.ENVIRONMENT
		} else{
			def credId = "${CLUSTER_CREDENTIALS}"
			def cluster = "${CLUSTER}"
			wrap([$class: 'KubectlBuildWrapper', serverUrl: cluster, credentialsId: credId ]) {
				stage('backend'){
					sh( "fnxctl deploy ${tag}")
				}

				env = sh(script: 'kubectl get configmap route -o go-template --template="{{ .data.environment }}"', returnStdout: true).trim()
			}
		}

		setDeployed(tag, 'se.fortnox', 'rocket-fuel', env)
	}
}