SBT docker:publish authentication issue - scala

I am Using SBT Native Packager to build Docker Image,
and my build.sbt is :
packageName in Docker := name.value
version in Docker := version.value
dockerBaseImage := "java"
dockerRepository :=Some("index.docker.io/xyz")
dockerExposedPorts := Seq(8283)
I am trying to publish image to private docker hub,but its giving
[error] unauthorized: authentication required.
in build.sbt how can i specify hub userName and Password.
is there any other configuration that i am missing here.

Just like Abanoub said this isn't possible via configuration. But you should be able to make the build work by executing docker login in your shell and the packager should then work as expected.

Related

How to exclude classes from compiling when using executing sbt docker:publishLocal?

My build.sbt file has some configuration to use AkkaGrpcPlugin and DockerPlugin because I am publishing the image at docker hub.
lazy val akkaGrpcVersion = "1.0.2"
lazy val protobufVersion = "3.11.4"
enablePlugins(JavaAppPackaging, JavaServerAppPackaging, AkkaGrpcPlugin, DockerPlugin)
akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Java)
libraryDependencies ++= Seq(
......
)
dockerUsername := Some("felipeogutierrez")
The sbt compile and sbt run work just fine, but the command sbt docker:publishLocal is not working because it tries to find some classes created by the gRPC in the target directory.
[error] /home/felipe/workspace-idea/explore-akka/target/scala-2.12/
akka-grpc/main/org/github/felipegutierrez/explore/akka/rpc/greeting/HelloRequest.java:29:7:
not found: type UnusedPrivateParameter
[error] UnusedPrivateParameter unused) {
[error] ^
these classes belong to the classes at package org.github.felipegutierrez.explore.akka.rpc.greeting and I would like to exclude them from the docker image when I am running sbt docker:publishLocal. I tried this solution but it didn't work. Or find some solution to make this work.
The problem was that I generated the Java files using AkkaGrpcPlugin and they were placed on the target directory of my project. So the docker compiler could not see it. I removed the AkkaGrpcPlugin and the akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Java) from the build.sbt and installed the protobuf compiler to generate the Java files by myself.
sudo apt install protobuf-compiler
protoc --java_out=main/java main/protobuf/helloworld.proto
Then the files are now generated on the src directory and Docker can see them. So, I don't have to exclude the files anymore in the build.sbt.

How to activate the sbt DockerPlugin in scala?

I have two scala projects, one is already defined to build its docker container through the sbt docker plugin. The other one I want to dockerify as well.
The working one has in its build.sbt the following lines relevant to the docker config:
organization := "com.namespace"
name := "dockerized-app"
version := sys.env.getOrElse("PIPELINE_VERSION", "0.1.0_local")
scalaVersion := "2.12.4"
enablePlugins(JavaAppPackaging)
enablePlugins(DockerPlugin)
packageName in Docker := packageName.value
dockerRepository := Some("our-docker.io:5001")
dockerExposedPorts := Seq(8080)
I thought that I could copy paste the relevant lines to the new project, change the name, and make it work.
Yet when I add the line to the about to be dockerified scala project:
enablePlugins(DockerPlugin)
I get the error:
Cannot resolve symbol DockerPlugin
I've looked through the prexisting projects libraryDependencies, yet it doesn't seem to be configured that way. In the the pre-configured project, IntellJ somehow knows the plugin, I can track that the DockerPlugin comes from com.typesafe.sbt.packager.docker. This made me assume that sbt comes shipped with it by default.
Yet apparently I have to activate it somehow.
Digging deeper I also tried adding this to my plugins.sbt to no avail:
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.2")
How to activate DockerPlugin using sbt in scala?
In order to make it working properly you need to add the following line:
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.2")
in your project/plugins.sbt file.
Then refresh your project and it should work.
For further information, please check the Sbt Native Packager documentation.

Running application using sbt run and some flags

I am currently running a akka application with the below command after I run
sbt assembly
java -Dconfig.resource=/application.test.conf -cp /path/to/folder:./target/scala-2.11/app-name.jar ca.path.to.main
Is there a way I can pass this information using sbt and some flags so I don't have to run the sbt assembly task everytime just to run the application?
sbt run config=/application.test.conf cp=/path/to/folder:
(something like the above)
Options that are passed to JVM are read by sbt from the javaOptions setting. So you can configure this setting to have the options you want and then tell sbt to fork new JVM process everytime you run your app from sbt so these options are applied. You can do this from sbt console:
set javaOptions += "-Dconfig.resource=/application.test.conf"
set fork := true
run
Or in your build.sbt file:
javaOptions += "-Dconfig.resource=/application.test.conf"
fork := true
However this might not be the most idiomatic approach to reach your underlying end goal.

Send module version as command line argument to SBT

I am using TeamCity to run a bash script that is utilizing SBT Native Packager to publish an image to Docker. The sbt portion of the bash script looks something like this:
sbt -DdockerRepository=$repo -DpackageName=$packageName -D myproject/docker:publish
I want to pass on the TeamCity build number as a version number to my package. Today I specify the version number manually in settings in build.sbt:
settings(
version := "0.20",
....,
dockerBaseImage := "example.com:5000/linux/java8:latest",
dockerRepository in Docker := Some("example.com/myoldrepo"),
dockerUpdateLatest := true'
)
I want to be able to do it like this:
activator -Dversion=0.21 -DpackageName=myproject -D myproject/docker:publish
but this does not seem to work. Yet overriding the dockerRepository like I do above is working.
How can I pass my desired version number into SBT from the command line/TeamCity?
You could set version before publish:
sbt 'set version := "1.0"' docker:publish
Try something like this:
val myVersion = util.Properties.propOrNone("version").getOrElse("0.20")
val myDockerBaseImage = util.Properties.propOrNone("dockerBaseImage").
getOrElse("example.com:5000/linux/java8:latest")
lazy val myProject = Project("myProject",file("path")).settings(
version := myVersion,
dockerBaseImage := myDockerBaseImage,
....,
dockerRepository in Docker := Some("example.com/myoldrepo"),
dockerUpdateLatest := true
)
And then call it (depends on your sbt installation):
SBT_OPTS="-Dversion=0.21" sbt
sbt -Dversion=0.21
activator -Dversion=0.21

Intellij 13 and importing an SBT project

I just installed Intellij 13 and tried importing some basic SBT project. However I received the following error:
The error message tells that org.jetbrains#sbt-structure;latest.integration cannot be found.
I am using SBT 0.13 and everything (compiling, running and etc..) seems to work from the CLI. I have also followed the installation instructions of the idea-sbt plugin (https://github.com/mpeltonen/sbt-idea).
build.sbt that I am using:
name := "demo"
version := "1.0"
scalaVersion := "2.9.2"
project/plugins.sbt:
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2")
Did anyone had this issue and managed to resolve it?
Thanks!
This was a known issue and is resolved in the latest version 0.26.327 (try to run 'Check for Updates...').
If the problem persists, it must be due to another issue but not dependency resolution. In that case you can resort to using the sbt-idea plugin. From a terminal inside your project directory execute sbt gen-idea, afterwards you should be able to use 'Open Project...' in IDEA.