#!/usr/bin/env groovy

// View Jenkins jobs at https://jenkins.nsi.rackspace.com/view/all/job/NSI-nscli/job/nscli/

//TODO: Unit Tests, Artifact Creation, Deployment

pipeline {
    options { disableConcurrentBuilds() }
    agent { label "IAD-DEV-BUILD-1" }
    stages {
        stage("Checkout Code") {
            steps {
                checkout scm
            }
        }
        stage("Build Base Testing Container") {
            // nscli_base doesn't contain any secrets
            steps {
                sh "docker build --pull -t nscli_base:latest ."
            }
        }
        stage("Install NSCLI in dev mode and Load Credentials") {
            steps {
                script {
                    // nscli_loaded is loaded with secrets and the current commit
                    withCredentials([file(credentialsId: 'credentials-json', variable: 'FILE')]) {
                        try {
                            sh "cp $FILE docker/credentials.json"
                            sh "echo 'FROM nscli_base:latest' | docker build -t nscli_loaded:latest . -f -"
                        }
                        finally {
                            sh "rm -f docker/credentials.json"
                        }
                    }
                }
            }
        }
        stage("Lint") {
            steps {
                sh "docker run nscli_loaded:latest make lint"
            }
        }
    }
    post {
        cleanup {
            sh "docker container prune -f"
            sh "docker rmi nscli_loaded"
            sh "docker image prune -f"
        }
    }
}
