def projectName = "CraterLib";
def projectIcon = "https://cdn.modrinth.com/data/Nn8Wasaq/a172c634683a11a2e9ae593e56eba7885743bb44.png";
def JDK = "17";
def majorMc = "1.20.4";
def modLoaders = "neoforge|forge|fabric|quilt";
def supportedMc = "1.20.4";
def reltype = "snapshot";

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.SSS_WEBHOOK,
                        title: "Deploy Started: ${projectName} ${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 build -PreleaseType=${reltype}"
                }
            }
        }

        stage("Publish to Maven") {
            steps {
                dir ("${WORKSPACE}/${majorMc}") {
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                        sh "./gradlew publish -PreleaseType=${reltype}"
                    }
                }
            }
        }
    }

    post {
        always {
            dir("${WORKSPACE}/${majorMc}") {
                sh "./gradlew --stop"
                archiveArtifacts artifacts: 'artifacts/*.jar'

                fddsnapshotter apiKey: env.PLATFORM_KEY,
                    projectSlug: "craterlib",
                    projectName: "${projectName}",
                    projectIcon: "${projectIcon}",
                    modLoaders: "${modLoaders}",
                    minecraftVersions: "${supportedMc}",
                    type: "snapshot",
                    dependsOn: "",
                    failWebhook: env.SSS_WEBHOOK,
                    publishWebhooks: "${env.SSS_WEBHOOK}|${env.FDD_WH}"

                deleteDir()
            }
        }
    }
}
