/*
 * Jakarta Validation API
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
 */

@Library('releng-pipeline') _

// Avoid running the pipeline on branch indexing
if (currentBuild.getBuildCauses().toString().contains('BranchIndexingCause')) {
	print "INFO: Build skipped due to trigger being Branch Indexing"
	currentBuild.result = 'NOT_BUILT'
	return
}

pipeline {
	agent {
		label 'basic'
	}
	tools {
		maven 'apache-maven-3.9.11'
		jdk 'openjdk-jdk25-latest'
	}
	options {
		// Wait for 1h before publishing snapshots, in case there's more commits.
		quietPeriod 3600
		// In any case, never publish snapshots more than once per hour.
		rateLimitBuilds(throttle: [count: 1, durationName: 'hour', userBoost: true])

		buildDiscarder(logRotator(numToKeepStr: '3', artifactNumToKeepStr: '3'))
		disableConcurrentBuilds(abortPrevious: false)
	}
	stages {
		stage('Publish Snapshot to Maven Central') {
			steps {
				withCredentials([file(credentialsId: 'secret-subkeys.asc', variable: 'KEYRING')]) {
					sh 'gpg --batch --import "${KEYRING}"'
					sh 'for fpr in $(gpg --list-keys --with-colons  | awk -F: \'/fpr:/ {print $10}\' | sort -u); do echo -e "5\ny\n" |  gpg --batch --command-fd 0 --expert --edit-key ${fpr} trust; done'

					sh "./mvnw clean deploy -Poss-release -Psnapshots -DskipTests=true"
				}
			}
		}
	}
}
