pipeline {
    agent any

    environment {
        ROOTFS_JOB = "espos-rootfs"
    }

    stages {
        stage('Clean') {
            steps {
                deleteDir()
            }
        }
        stage("Checkout") {
            steps {
                checkout scm
            }
        }
        stage('Download RootFS') {
            steps {
                copyArtifacts(
                    projectName: env.ROOTFS_JOB,
                    selector: lastSuccessful()
                )
            }
        }

        stage('Build ISO') {
            steps {
                sh '''
                    chmod +x build-liveiso.sh
                    sudo ./build-liveiso.sh
                '''
            }
        }
    }

    post {
        success {
            archiveArtifacts(
                artifacts: '*.iso',
                fingerprint: true
            )
        }
    }
}