Glef
Glef

Reputation: 383

How to use Environment Variables in build.sbt?

I would like to use environment variables to pass my repo credentials when using built.sbt.

I have tried things like this but the details arent picked up.

credentials += Credentials("Some Nexus Repository Manager", "my.artifact.repo.net", System.getenv("USERNAME"), System.getenv("PASSWORD"))

I have also tried a credentials file under ~/.sbt/ but I'm not sure how to add environment variables to that.

If I just type my username and password normally in the credentials file it works so I know that the log in details are ok.

Additional: I source the environment vars in a shell before running sbt compile.

Running

credentials += Credentials("Realm", "my.artifact.repo.net", sys.env("USERNAME"), sys.env("PASSWORD"))

results in a forbidden url error.

I should say I'm stuck trying to resolve dependencies

UPDATE: The Following returns the correct value

eval scala.sys.env("ARTIFACTORY_USERNAME")

But when I add this into my script

val username = scala.sys.env("ARTIFACTORY_USERNAME")
val password = scala.sys.env("ARTIFACTORY_PASSWORD")
credentials += Credentials("Artifactory Realm", "artifactory.link.io", username, password)

resolvers ++= Seq(
"Artifactory Snapshot" at "https://artifactory.link.io/art/libs-snapshot"
)

or

credentials += Credentials("Artifactory Realm", "my.artifact.repo.net", sys.env("ARTIFACTORY_USERNAME"), sys.env("ARTIFACTORY_PASSWORD"))   

I get a FORBIDDEN URL error which suggests that the scala part runs ok but for some reason the credentials are still incorrect. If I explicitly set the credentials in the build.sbt it works.

Upvotes: 31

Views: 37830

Answers (6)

Michel Hua
Michel Hua

Reputation: 1777

(sys.env.get("USERNAME"), sys.env.get("PASSWORD")) match ... didn't work with sbt 1.1.1.

Alternative solution from JFrog, here is a working example on github

https://github.com/jfrog/project-examples/tree/master/circleci-example/circleci-sbt-artifactory

Create a credentials.properties file at the root of your project instead of ~/.sbt/credentials. Add it to .gitignore.

cat <<EOF > credentials.properties
realm=Artifactory Realm
host=gcartifactory-us.jfrog.info
user=$ARTIFACTORY_USERNAME
password=$ARTIFACTORY_PASSWORD
EOF

Generate the file during before build time and make a reference to it in your build.sbt

credentials += Credentials(new File("credentials.properties"))

Upvotes: 0

Raman Mishra
Raman Mishra

Reputation: 2686

You can also use

val config :Config = ConfigFactory.load()
val username=config.getString(“USERNAME”)
val password = config.getString(“PASSWORD”)

Upvotes: 1

Pietrotull
Pietrotull

Reputation: 497

You can get env variables with the commands mentioned other responses like:

sys.env.get("USERNAME")
sys.env.get("PASSWORD")

but they return an option of type Option[String]

To turn this into string you need to either do a match or simply use

sys.env.get("USERNAME").get
sys.env.get("USERNAME").getOrElse("some default value")

if you need to set some default value. Warning! calling .get of an option that does not have value will give you a runtime error.

Upvotes: 26

Don Branson
Don Branson

Reputation: 13709

Adding your code to the example, sending Credentials strings for user id and password:

(sys.env.get("USERNAME"), sys.env.get("PASSWORD")) match {
  case (Some(username), Some(password)) => 
    println(s"Do my thing $username/$password")
    credentials += Credentials("Realm", "my.artifact.repo.net", username, password)
  case _ => 
    println("USERNAME and/or PASSWORD is missing")
    credentials ++= Seq()
}

Upvotes: 4

Justin Kaeser
Justin Kaeser

Reputation: 5948

Getting the environment variables via sys.env("VARIABLE") or System.getenv("VARIABLE") will both work in build.sbt if they are correctly exported.

You may need to setup your credentials correctly (general instructions).

For Artifactory, it should look like this:

publishTo := Some("Artifactory Realm" at "http://<host>:<port>/artifactory/<repo-key>")
credentials += Credentials("Artifactory Realm", "<host>", "<USERNAME>", "<PASS>")

Note that "Artifactory Realm" here is not an arbitrary string and must actually be exactly as stated.

Full instructions: https://www.jfrog.com/confluence/display/RTF/SBT+Repositories

Upvotes: 2

OlivierBlanvillain
OlivierBlanvillain

Reputation: 7768

You can use anything that works in Scala in sbt, for instance:

sys.env.get("PASSWORD")

Upvotes: 9

Related Questions