How to setup Gradle publish task user credentials with GitLab CI secret variables? I am using gradle maven publish plugin, and here is snippet from build.gradle
you can use environment variables directly to set gradle properties, see full documentation here.
in your case set artifactUser
and artifactPass
as env variables (best as secrect ones).
Here is how I resolved it (unfortunately the official GitLab doco is very focused on Maven... :(
apply plugin: 'java'
apply plugin: 'maven-publish'
compileJava.options.encoding = 'UTF-8'
group = 'com.example'
version = '1.0.9'
task zipSource(type: Zip) {
from file('files/test.zip')
archiveClassifier = 'testZip'
}
publishing {
repositories {
maven {
name 'GitLab'
url 'https://gitlab.my-company.com/api/v4/projects/2302/packages/maven'
credentials(HttpHeaderCredentials) {
name = "Job-Token"
value = System.getenv("CI_JOB_TOKEN")
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
publications {
mavenJava(MavenPublication) {
artifactId = 'project1-sample'
//deploy jar vom Java
from components.java
//deploy arbitrary Zip file
artifact zipSource
}
}
}
You don't need env.
prefinx in your .gitlab-ci.yml
. You don't need to re-export the variables as well.
If you have defined a variables named MAVEN_REPO_USER
and MAVEN_REPO_PASS
in Gitlab CI/CD settings for the project, you can just use them in Gradle script:
repositories {
maven {
credentials {
username System.getenv("MAVEN_REPO_USER")
password System.getenv("MAVEN_REPO_PASS")
}
url "…"
}
}