Search code examples
gradleartifactory

How Do I Set The Username/Password For The Build Info Using The Artifactory Gradle Plugin?


I have set the username and password for publishing to artifactory using gradle, however, the build fails on publishing the build.info.

artifactory {
    publish {
        contextUrl = 'http://localhost:8081/artifactory'
        repository {
            repoKey = "libs-snapshot-local"
            username = "user"
            password = "pass"
            maven = true
        }
        defaults {
            publications = ('mavenJava')
        }
    }
}

The user I am running gradle with doesn't have access to the artifactory repository but the user in the artifactory block does.

It seems like the build.info is being published to artifactory with the gradle user and not the user in the build.gradle.

How do I set the username/password so that the build.info is published using a user that has permissions?


Solution

  • There are two sets of Artifactory credentials you can configure for the Gradle Artifactory plugin:

    Credentials used for artifacts resolution

    repositories {
        jcenter()
        maven {
            url "http://repo.myorg.com/artifactory/my-repo" // The Artifactory (preferably virtual) repository to resolve from
            credentials {               // Optional resolver credentials (leave out to use anonymous resolution)
                username = "username" // Artifactory user name
                password = "password" // Password or API Key
            }
        }
    }
    

    Credentials used for deploying artifacts and publishing build info. This is the set of credentials you need to use for deploying the artifacts and build info into Artifactory.
    You need to make sure this user has permissions for Repositories:Deploy and Builds:Deploy.
    The OS user which is running gradle (gradle user) is not being used for authentication and is not recognized as an Artifactory user. It is, however, captured as part of the build info.

    artifactory {
      contextUrl = 'http://repo.myorg.com/artifactory'   // The base Artifactory URL if not overridden by the publisher/resolver
      publish {
        contextUrl = 'http://repo.myorg.com/artifactory'   //The base Artifactory URL for the publisher
        //A closure defining publishing information
        repository {
          repoKey = 'my-repo'   //The Artifactory repository key to publish to
          username = 'stackoverflow'          //The publisher user name
          password = 'password'       //The publisher password or API key
        }
      }
    }
    

    The expected behavior is that when running the following command

    ./gradlew clean artifactoryPublish
    

    The build artifacts and build info will be deployed to Artifactory

    [pool-17-thread-1] Deploying artifact: http://127.0.0.1:8081/artifactory/libs-snapshot-local/gradle-example-minimal/1.0-SNAPSHOT/gradle-example-minimal-1.0-SNAPSHOT.jar
    [pool-17-thread-1] Deploying artifact: http://127.0.0.1:8081/artifactory/libs-snapshot-local/gradle-example-minimal/1.0-SNAPSHOT/gradle-example-minimal-1.0-SNAPSHOT.pom
    
    > Task :artifactoryDeploy
    Deploying build descriptor to: http://127.0.0.1:8081/artifactory/api/build
    Build successfully deployed. Browse it in Artifactory under http://127.0.0.1:8081/artifactory/webapp/builds/gradle-example-minimal/1602276439713
    
    BUILD SUCCESSFUL in 903ms
    7 actionable tasks: 7 executed
    

    In Artifactory you will see that the artifacts where deployed using the username specified in the publish section

    enter image description here

    As well as the build-info JSON file

    enter image description here

    In the build info you will see that 2 user types are being captured:

    1. Artifactory principal - this is the Artifactory user which was used to deploy the build info
    2. Principal - this is the OS user which was running the gradle build. This information is captured inside the build info JSON as "principal"

    enter image description here