pipeline {
  agent { label 'linux' }
  options { buildDiscarder(logRotator(numToKeepStr: '2')) }
  stages {
    stage('Package') {
      steps {
        sh '''
          mkdir -p dist
          echo "build $BUILD_NUMBER" > app.txt
          tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz app.txt
          (cd dist && sha256sum app.tar.gz > app.tar.gz.sha256)
        '''
        stash name: 'dist', includes: 'dist/*'
      }
    }
    stage('Check in a clean workspace') {
      steps {
        cleanWs()
        unstash 'dist'
        sh 'cd dist && sha256sum -c app.tar.gz.sha256'
      }
    }
  }
  post {
    success { archiveArtifacts artifacts: 'dist/*', fingerprint: true, onlyIfSuccessful: true }
    cleanup { cleanWs() }
  }
}
