GitLab CI secret variables for Gradle publish

后端 未结 3 1838
臣服心动
臣服心动 2021-02-20 14:39

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

         


        
相关标签:
3条回答
  • 2021-02-20 15:09

    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).

    0 讨论(0)
  • 2021-02-20 15:22

    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
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-20 15:32

    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 "…"
        }
    }
    
    0 讨论(0)
提交回复
热议问题