Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI secret variables for Gradle publish

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

repositories {
    maven {
      credentials {
        username artifactUser
        password artifactPass
      }
      url "..."
    }
  }

I've tried to use gradle.properties as below

artifactUser=${env.MAVEN_REPO_USER}
artifactPass=${env.MAVEN_REPO_PASS}

And several ways of accessing secret variables in .gitlab-ci.yml file (because gradle.properties is not picked up from gradle or variables are not transformed correctly, it is in root project dir)

Method 1

'./gradlew publish -x test -PartifactUser=${env.MAVEN_REPO_USER} -PartifactPass=${env.MAVEN_REPO_PASS}'

Error: /bin/bash: line 56: -PartifactUser=${env.MAVEN_REPO_USER}: bad substitution

Method 2

    before_script:
      - chmod +x ./gradlew
      - export REPO_USER=${env.MAVEN_REPO_USER}
      - export REPO_PASS=${env.MAVEN_REPO_PASS}
    ...
    deploy:
  stage: deploy
  script:
    - ./gradlew publish -x test -PartifactUser=$REPO_USER -PartifactPass=$REPO_PASS

I am using openjdk:8-jdk-slim image for build using gradle wrapper. Seems like there are several issues with this kind of variable usage, do we have any workaround?

like image 792
Ruwanka Madhushan Avatar asked Jul 02 '18 14:07

Ruwanka Madhushan


1 Answers

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 "…"
    }
}
like image 70
madhead - StandWithUkraine Avatar answered Sep 21 '22 14:09

madhead - StandWithUkraine