def PushToGit(env, params) {
    sh "git checkout ${params.VPC_ID} && git add . && git commit -am '${params.VPC_ID} destruction -- status: ${currentBuild.result}' && git push --set-upstream origin ${params.VPC_ID}"
}

pipeline {
    agent any
    stages {
        stage('Setup Virtualenv') {
            steps {
                sh 'rm -rf ./virtualenv && python3.5 -m venv virtualenv && . ./virtualenv/bin/activate && python --version && which python && pip --version && which pip && pip install wheel && pip install -r ./terraform/requirements.txt'
            }
        }
        stage('Git') {
            steps {
                sh "set -x && git checkout ${params.VPC_ID} && git merge origin/master"
            }
        }
        stage('Destroy') {
            steps {
                sh "${env.WORKSPACE}/virtualenv/bin/python ${env.WORKSPACE}/terraform/terraform.py destroy --vpc-id ${params.VPC_ID} -yf"
            }
        }
    }
    post {
        success {
            slackSend channel: '#terraform', color: 'good', message: "The ${params.VPC_ID} VPC has successfully been destroyed :) :tada:"
            PushToGit(env, params)
        }
        failure {
            slackSend channel: '#terraform', color: 'danger', message: "The destruction of ${params.VPC_ID} VPC has failed :feelsbadman: #blamejohnny?"
            PushToGit(env, params)
        }
    }
}
