#!groovy
load "$WORKSPACE/.env/.envfile"
pipeline {
    agent {
       label 'master'
    }

    options {
       timestamps()
    }

    stages {
      stage("build project") {
        steps {
          sh 'go build main.go webPage.go'
        }
      }
      stage("deploy docker") {
        steps{
          sh 'docker stop schedule || exit 0'
          sh 'docker rm schedule || exit 0'
          sh 'docker build -t basic .'
          sh 'docker run -d --name schedule -p ${env.PORT}:${env.PORT} -t basic'
        }
      } 
    }
    post {
      success {
        echo 'I succeeeded!'
        sh 'cat $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/log > /tmp/success.log'

      }
      unstable {
        echo 'I am unstable :/'
      }
      failure {
        echo 'I failed :((('
        sh 'cat $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/log > /tmp/fail.log'
        echo $port
      }
      changed {
        echo 'things were different before...'
      }
    }

}