pipeline {
  options { disableConcurrentBuilds() }
  agent {
      ecs {
          inheritFrom 'GenericWorker'
          image 'docker.io/watsom27/jenkins:rust'
      }
  }
  stages {
    stage('Check Formatting') {
      steps {
        sh 'cargo +nightly fmt --check'
      }
    }
    stage('Run Build') {
      steps {
        sh 'cargo +stable build --release --workspace'
      }
    }
    stage('Run Build --all-features') {
      steps {
        script {
          try {
            sh 'cargo +stable build --release --all-features --workspace'
          } catch (err) {
            unstable(message: "Failed --all-features build")
          }
        }
      }
    }
    stage('Run Build --no-default-features') {
      steps {
        script {
          try {
            sh 'cargo +stable build --release --no-default-features --workspace'
          } catch (err) {
            unstable(message: "Failed --no-default-features")
          }
        }
      }
    }
    stage('Run Clippy') {
      steps {
        sh 'cargo +stable clippy --release --workspace'
      }
    }
    stage('Run Test') {
      steps {
        sh 'cargo +stable test --release --workspace'
      }
    }
    stage('Publish New Version') {
      when { branch "master" }
      steps {
        script {
          try {
            sh 'cargo publish'
          } catch (err) {
            unstable(message: "Failed to publish crate")
          }
        }
      }
    }
  }
}
