// Documentation: https://github.com/coreos/coreos-ci/blob/main/README-upstream-ci.md

properties([
    // abort previous runs when a PR is updated to save resources
    disableConcurrentBuilds(abortPrevious: true)
])

buildPod {
    checkout scm

    // hack to satisfy golang compiler wanting to cache things
    shwrap("mkdir cache")
    withEnv(["XDG_CACHE_HOME=${env.WORKSPACE}/cache"]) {
        // XXX: convert all this to coreos-ci-lib sugar
        stage("Build") {
            shwrap("make")
            shwrap("make install DESTDIR=install")
            stash name: 'build', includes: 'install/**'
        }
        // first, run gofmt/govet/unit tests
        stage("Unit Tests") {
            shwrap("./test")
        }
        stage("Build Blackbox Tests") {
            shwrap("""
                ./build_blackbox_tests
                mkdir -p tests/kola/blackbox/data/bin
                mv tests.test bin/amd64/* tests/kola/blackbox/data/bin
            """)
            stash name: 'blackbox', includes: 'tests/kola/blackbox/data/bin/**'
        }
    }
}

cosaPod {
    checkout scm

    unstash name: 'build'
    cosaBuild(skipKola: true, overlays: ["install"])

    // This is a temporary hack we use during spec stabilization to work around
    // external tests which use experimental versions. It should be commented
    // out in normal times.
    //if (shwrapRc("grep -nr 3.3.0-experimental /srv/coreos/src/config/tests/kola") == 0) {
    //    shwrap("find /srv/coreos/src/config/tests/kola -type f -exec sed -i 's/3.3.0-experimental/3.3.0/' {} \\;")
    //} else {
    //    throw new Exception("No 3.3.0-experimental external tests found; comment out this workaround.")
    //}
    // Similarly, external tests might use Butane experimental specs which
    // transpile to Ignition experimental specs. Uncomment this to work
    // around the problem, and re-comment it afterward. If kola sees a
    // non-experimental spec it doesn't recognize, it'll automatically add
    // -experimental and then strip -experimental from the resulting
    // Ignition config.
    //if (shwrapRc("grep -nr 1.6.0-experimental /srv/coreos/src/config/tests/kola") == 0) {
    //    shwrap("find /srv/coreos/src/config/tests/kola -type f -exec sed -i 's/1.6.0-experimental/1.6.0/' {} \\;")
    //} else {
    //    throw new Exception("No 1.6.0-experimental external tests found; comment out this workaround.")
    //}

    // we run the blackbox tests separately instead of as part of the main kola
    // run since it's a distinct kind of test and we want to draw more
    // attention to it in the Jenkins UI
    kola(extraArgs: "--denylist-test ext.*.blackbox")

    parallel blackbox: {
        unstash name: 'blackbox'
        kola(extraArgs: "ext.*.blackbox", skipUpgrade: true)
    }, testiso: {
        shwrap("""
            cd /srv/coreos
            cosa osbuild metal metal4k live
        """)
        kolaTestIso()
    }
}
