This repository has been archived by the owner on Feb 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifactory-credentials.gradle
49 lines (41 loc) · 2.53 KB
/
artifactory-credentials.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Allow application of credentials to a sub-set of repositories
//This fille is intended to be used in Gradle builds via "apply from: '{url}'", within blocks which have a "repositories" property (buildscript, project, allprojects, subprojects, etc)
//Check the various methods for setting the user's "Gradle home", and apply any gradle properties from that location
def gradleUserHome = ''
if (System.properties['gradle.user.home']){
gradleUserHome = System.properties['gradle.user.home']
} else if (System.getenv('GRADLE_USER_HOME')){
gradleUserHome = System.getenv('GRADLE_USER_HOME')
} else if (System.properties['user.home']){
gradleUserHome = "${System.properties['user.home']}/.gradle"
}
Properties props = new Properties()
if (new File("$gradleUserHome/gradle.properties").exists()){
props.load(new FileInputStream("$gradleUserHome/gradle.properties"))
}
//Read in user and password credentials, and a filter for which repositories to apply use of those credentials to
def artifactoryUser = System.getenv('ARTIFACTORY_USER') ?: props.ARTIFACTORY_USER
def artifactoryPassword = System.getenv('ARTIFACTORY_PASSWORD') ?: props.ARTIFACTORY_PASSWORD
def artifactoryHostPattern = (System.getenv('ARTIFACTORY_HOST_PATTERN') ?: props.ARTIFACTORY_HOST_PATTERN) ?: 'blackducksoftware.com/artifactory'
//Apply credentials, or if any reequired information is missing, log what is needed
if(artifactoryUser != null && artifactoryPassword != null && artifactoryHostPattern != null){
logger.lifecycle("Applying credentials for repositories matching ${artifactoryHostPattern}")
//Apply credentials to repositories which have already been configured
repositories.each{
configureRepositoryCredentials(it, artifactoryUser, artifactoryPassword, artifactoryHostPattern)
}
//whenObjectAdded is invoked to configure any repositories added after application of the configuration file
repositories.whenObjectAdded{
configureRepositoryCredentials(it, artifactoryUser, artifactoryPassword, artifactoryHostPattern)
}
}else{
logger.error("Credentials or address not provided for repository configuration Provider: (address: {}, user: {}, password: {})", (artifactoryHostPattern != null), (artifactoryUser != null), (artifactoryPassword != null))
}
def configureRepositoryCredentials(def repository, def artifactoryUser, def artifactoryPassword, def artifactoryHostPattern){
if (repository.hasProperty('url') && repository.url.toString().contains("${artifactoryHostPattern}")){
repository.credentials{
username = artifactoryUser
password = artifactoryPassword
}
}
}