Use env.AWS_ROLE_ARN for publishing

This commit is contained in:
Nathan Adams 2018-07-02 12:35:33 +02:00 committed by Fry
parent 1ecf0558a0
commit 235f0c9b28

View file

@ -1,4 +1,6 @@
import groovy.io.FileType
import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider
import com.amazonaws.services.s3.AmazonS3Client
apply plugin: 'java-library'
apply plugin: 'maven-publish'
@ -10,6 +12,19 @@ task wrapper(type: Wrapper) {
gradleVersion = '4.0'
}
buildscript {
repositories {
mavenCentral()
maven {
url "https://libraries.minecraft.net"
}
}
dependencies {
classpath 'com.amazonaws:aws-java-sdk:1.11.33'
}
}
repositories {
maven {
url "https://libraries.minecraft.net"
@ -79,6 +94,18 @@ task report {
}
}
def publishDir = file("$buildDir/repo")
def uploadFile(s3, bucket, path, filename) {
println "Uploading $filename to $bucket as $path"
s3.putObject(bucket, path, filename)
}
clean.doLast {
delete publishDir
}
if (version.endsWith("SNAPSHOT")) {
publishing.repositories {
mavenLocal()
@ -86,9 +113,19 @@ if (version.endsWith("SNAPSHOT")) {
} else {
publishing.repositories {
maven {
url "s3://minecraft-libraries/"
authentication {
awsIm(AwsImAuthentication)
url "$buildDir/repo"
}
}
publish.doLast {
def AWSRoleARN = (System.getenv("AWS_ROLE_ARN") != null && System.getenv("AWS_ROLE_ARN") != "" ? System.getenv("AWS_ROLE_ARN") : null)
if (AWSRoleARN == null) throw new GradleException("AWS Role has not been configured, use the `AWS_ROLE_ARN` environment variable")
def auth = new STSAssumeRoleSessionCredentialsProvider.Builder(AWSRoleARN, "JavaBrigadierPublish").build()
def s3 = new AmazonS3Client(auth)
publishDir.eachFileRecurse {
if (!it.name.contains(".xml") && it.isFile()) {
def relPath = publishDir.toPath().relativize(it.toPath()).toFile().toString().replaceAll('\\\\', '/')
uploadFile(s3, "minecraft-libraries", relPath, it)
}
}
}