1
votes

I have a Gradle project with the following uploadArchives configuration to publish its artifact to Nexus:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: 'http://releasesrepo')
            snapshotRepository(url: 'http://snapshotsrepo')
        }
    }
}

As you see there are no credentials in this configuration.

This build will be run from Jenkins as a Freestyle project so I will just have to specify in Build - Invoke Gradle Script - Tasks -> build upload

What's the right way to configure the Nexus credentials from Jenkins and pass them to the Gradle taks (I don't want them in the Git repo with the source code)?

1
Jenkins Credentials Store and define them in the global config file provider (init.gradle) Script if I correctly remember... - khmarbaise

1 Answers

1
votes

There are three different ways:

1.Using environmental variables

def nexusUser = hasProperty('nexusUsername') ? nexusUsername : System.getenv('nexusUsername')
def nexusPassword = hasProperty('nexusPassword') ? nexusPassword : System.getenv('nexusPassword')
def nexusReleaseUrl = hasProperty('nexusReleaseUrl') ? nexusReleaseUrl : System.getenv('nexusReleaseUrl')
def nexusSnapshotUrl = hasProperty('nexusSnapshotUrl') ? nexusSnapshotUrl : System.getenv('nexusSnapshotUrl')

repositories {
    mavenDeployer {
        repository(url: nexusReleaseUrl) {
            authentication(userName: nexusUser, password: nexusPassword);
        }
        snapshotRepository(url: nexusSnapshotUrl) {
            authentication(userName: nexusUser, password: nexusPassword);
        }
    }

}

2.Using ~/.gradle/gradle.properties (remember is under the user jenkins)

nexusUser=user
nexusPass=password

uploadArchives {
  def nexusUser = "${nexusUsername}"
  def nexusPassword = "${nexusPassword}"

  repositories {
    mavenDeployer {
        repository(url: 'your nexus releases') {
            authentication(userName: nexusUser, password: nexusPassword);
        }
        snapshotRepository(url: 'your nexus snapshot') {
            authentication(userName: nexusUser, password: nexusPassword);
        }
    }
  }
}

3.Using the global credentials

You have an example here (it is for Travis) if you want to have a look