def majorMc = "1.20.2";
def JDK = "17"

pipeline {
    agent {
        docker {
            image "registry.firstdark.dev/java${JDK}:latest"
            alwaysPull true
            args '-v gradle-cache:/home/gradle/.gradle'
        }
    }

    environment {
        GRADLE_USER_HOME = '/home/gradle/.gradle'
    }

    stages {
        stage("Notify Discord") {
            steps {
                discordSend webhookURL: env.FDD_WH_ADMIN,
                        title: "Deploy Started: CraterLib ${majorMc} Deploy #${BUILD_NUMBER}",
                        link: env.BUILD_URL,
                        result: 'SUCCESS',
                        description: "Build: [${BUILD_NUMBER}](${env.BUILD_URL})"
            }
        }
        stage("Prepare") {
            steps {
                dir("${WORKSPACE}/${majorMc}") {
                    sh "chmod +x ./gradlew"
                    sh "./gradlew clean"
                }
            }
        }
        stage("Publish to Modrinth/Curseforge") {
            steps {
                dir("${WORKSPACE}/${majorMc}") {
                    sh "./gradlew publishMod -Prelease=true"
                }
            }
        }
        stage("Publish to Maven") {
            steps {
                dir("${WORKSPACE}/${majorMc}") {
                    sh "./gradlew publish -Prelease=true"
                }
            }
        }
    }
    post {
        always {
            dir("${WORKSPACE}/${majorMc}") {
                sh "./gradlew --stop"
                deleteDir()
            }

            discordSend webhookURL: env.FDD_WH_ADMIN,
                    title: "CraterLib Port Deploy #${BUILD_NUMBER}",
                    link: env.BUILD_URL,
                    result: currentBuild.currentResult,
                    description: "Build: [${BUILD_NUMBER}](${env.BUILD_URL})\nStatus: ${currentBuild.currentResult}"
        }
    }
}
