pipeline {
  // Some steps hang forever. Rather than take an agent, we are good Jenkins denizens and explicitly say when we want
  // a jenkins worker
  agent none
  options {
    timestamps()
    ansiColor('xterm')
  }

  stages {
    stage("Run tests using docker-compose") {
      agent any
      environment {
        // Isolate the resources creted by docker-compose by setting the project name to something
        // that is unique to the build.  docker-compose uses this project name when naming networks,
        // containers, etc.  We want the resources to get unique names so that parallel builds don't
        // interfere with each other.
        COMPOSE_PROJECT_NAME = "${env.BUILD_TAG}"
      }
      steps {
        // Print debug test
        sh 'env'
        sh 'docker --version'
        sh 'docker-compose --version'
        sh 'docker-compose ps'

        // Spin up a dev environment and run integration tests.
        sh 'docker-compose up ' +
          '--abort-on-container-exit ' + // Stop the containers after integration exits.
          '--exit-code-from integration' // Pass up the exit code from the integration container.
                                         // This allows us to fail the build if the tests fail.
      }
      post {
        always {
            // Remove the containers, and network created by docker-compose.
            sh 'docker-compose down'
            sh 'docker-compose ps'
        }
      }
    }
  }
}
