#!/usr/bin/env groovy

pipeline {
    agent any
    options {
        timestamps()
        ansiColor('xterm')
    }  
    stages {
        stage('Build package') {
            steps {
	        sh '''#!/bin/bash
                    PACKER_VERSION=1.6.5
                    PACKER=./packer
                    until [ -x $PACKER ] && [ "$($PACKER --version)" = "$PACKER_VERSION" ]; do
                        wget -cq "https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip"
                        unzip packer_${PACKER_VERSION}_linux_amd64.zip
                    done
                    $PACKER build packer.json
    	            '''
            }
        }
        stage('Upload package') {
            environment {
                ARTI_CONFIG_FILE = credentials('dta_arti_creds')
            }
            steps {
                script {
                    if (env.BRANCH_NAME == 'master') {
                        sh '''
                            arti -c $ARTI_CONFIG_FILE deb -o xenial -r ip-xenial-twitch output/*.deb
                            arti -c $ARTI_CONFIG_FILE deb -o bionic -r ip-bionic-twitch output/*.deb
                            arti -c $ARTI_CONFIG_FILE deb -o focal -r ip-focal-twitch output/*.deb
                            arti -c $ARTI_CONFIG_FILE rpm -o ip-centos7 output/*.rpm
                            '''
                    } else {
                        sh '''
                            arti -c $ARTI_CONFIG_FILE deb -o xenial -p testing -r ip-xenial-twitch output/*.deb
                            arti -c $ARTI_CONFIG_FILE deb -o bionic -p testing -r ip-bionic-twitch output/*.deb
                            arti -c $ARTI_CONFIG_FILE deb -o focal -p testing -r ip-focal-twitch output/*.deb
                            arti -c $ARTI_CONFIG_FILE rpm -r ip-vidcs-dev output/*.rpm
                            '''
                    }
                }
            }
        }
    }
}
