pipeline {

    agent any


    environment {
        ROOTFS_ARTIFACT = "espos-openrc-rootfs.tar.zst"
    }


    stages {


        stage('Checkout') {

            steps {
                checkout scm
            }
        }



        stage('Install dependencies') {

            steps {

                sh '''
                    sudo apt update

                    sudo apt install -y \
                        debootstrap \
                        xz-utils \
                        tar \
                        ca-certificates \
                        zstd \
                        pv

                '''

            }

        }



        stage('Build rootfs') {

            steps {

                sh '''

                    chmod +x build-rootfs.sh

                    sudo ./build-rootfs.sh

                '''

            }

        }



        stage('Verify artifact') {

            steps {

                sh '''

                    test -f ${ROOTFS_ARTIFACT}
                    ls -lh ${ROOTFS_ARTIFACT}

                '''

            }

        }



        stage('Archive artifact') {

            steps {

                archiveArtifacts \
                    artifacts: "${ROOTFS_ARTIFACT}",
                    fingerprint: true

            }

        }

    }



    post {

        success {

            echo "RootFS EspOS generado y guardado como artifact"

        }


        failure {

            echo "Error generando rootfs"

        }

    }

}