Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to question :: integration with gitlab CI #1213

Closed
stefangweichinger opened this issue Apr 7, 2019 · 19 comments
Closed

how to question :: integration with gitlab CI #1213

stefangweichinger opened this issue Apr 7, 2019 · 19 comments
Labels
docker documentation Documentation should be extended or updated

Comments

@stefangweichinger
Copy link

I am trying to write a .gitlab-ci.yml that uses "sbt docker:publish" to generate and push a new image for each new commit to the gitlab repo. And I still haven't figured out how and where to configure that.

I assume I would have to call "sbt docker" with the $CI_COMMIT_TAG variable or so, could someone please point me at some example or howto? Thanks a lot.

@muuki88 muuki88 added docker documentation Documentation should be extended or updated labels Apr 26, 2019
@muuki88
Copy link
Contributor

muuki88 commented Apr 26, 2019

Hi @stefangweichinger
Thanks for your question and sorry for the late reply.

The question is how you want to version (tag) your docker file. This is generally an sbt configuration. You could use the $CI_COMMIT_TAG env variable in your build.sbt and set the version with it.

version := "1.0-" + sys.env("$CI_COMMIT_TAG`")

@stefangweichinger
Copy link
Author

thanks @muuki88 , will try in a few days (after other projects)

@stefangweichinger
Copy link
Author

@muuki88 :: I changed my mind and tried it quickly, "sbt docker: publish" fails with

"[error] java.util.NoSuchElementException: key not found: $CI_COMMIT_TAG`". 

The quoting seems wrong to me, the "`" in your example is single ... even removing that single quote fails. trying to figure it out now.

@nigredo-tori
Copy link
Collaborator

@stefangweichinger, it should be this (without the dollar sign and the backtick):

version := "1.0-" + sys.env("CI_COMMIT_SHA")

Note that CI_COMMIT_TAG is only defined when building tags, which is probably not what you want.

There are other problems with this solution - for one, it doesn't work with local builds. These can be worked around, but I highly recommend using sbt-dynver instead.

@stefangweichinger
Copy link
Author

@nigredo-tori thanks for the pointer, that sounds very helpful. Starting to play with it and solving the next issues on my way.

@stefangweichinger
Copy link
Author

@nigredo-tori , unfortunately it bails out with:

[error] invalid argument "my_app:1.7.2+195-77097a7b+20190504-1512" for "-t, --tag" flag: invalid reference format

The format looks the same as in the README of the plugin ... maybe you can help?

@nigredo-tori
Copy link
Collaborator

@stefangweichinger, AFAICT, this should work.

@stefangweichinger
Copy link
Author

@nigredo-tori thanks, I overread that. Forgive my newbie-questions, do I simply have to add it to build.sbt?

@nigredo-tori
Copy link
Collaborator

do I simply have to add it to build.sbt?

Yes.

@stefangweichinger
Copy link
Author

didn't work for me, but the other lines do:

version in ThisBuild ~= (_.replace('+', '-'))
 dynver in ThisBuild ~= (_.replace('+', '-'))

@nigredo-tori
Copy link
Collaborator

didn't work for me, but the other lines do:

Sorry, looks like dynverSeparator has not made it into a release yet. The .replace thing should work OK.

@stefangweichinger
Copy link
Author

yes, it does so far. I close here, thanks

@stefangweichinger
Copy link
Author

one more, I struggle with getting the image-name right.
I try that via setting the variable dockerRepository and it seems I am thinking too complicated.

At best I would prefer to be able to make use of the variables the GitLab CI provides, for example

CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest

(How) can I use such variables with sbt docker:publish ? maybe someone might share a pointer or an example ...

@elha-da
Copy link

elha-da commented May 17, 2019

Hi,

I set version of my docker image like that sbt 'set version in Docker := "dev"' docker:publish I think you can do same with packageName ...
I hope helped you

Me, when I run sbt docker:publich in sbt-openjdk-8-alpine container I get this Exception

java.io.IOException: Cannot run program "docker" (in directory ..../target/docker/stage"): error=2, No such file or directory

.........

Caused by: java.io.IOException: error=2, No such file or directory

.........

any ideas ?

Thanks

@nigredo-tori
Copy link
Collaborator

nigredo-tori commented May 17, 2019

@stefangweichinger, you can do something along the lines of:

dockerAlias := sys.env.getOrElse("CI_REGISTRY_IMAGE", sys.error("CI_REGISTRY_IMAGE not set"))

See https://www.scala-sbt.org/sbt-native-packager/formats/docker.html for more.

@elha-da, you don't have docker in your build container. The way DockerPlugin works, you need SBT, JDK and Docker in your build environment at the same time. You would have to find or make an image like this, and run your build in that.

@elha-da
Copy link

elha-da commented May 17, 2019

@nigredo-tori Thanks 👍

@nigredo-tori
Copy link
Collaborator

nigredo-tori commented May 17, 2019

@elha-da, it seems I was not entirely correct. Skimming through the DockerPlugin, it seems that everything up to the actual build should work even without docker. So you can do this in your SBT container:

sbt Docker/stage

which would build a staging directory (target/docker/stage, I think) with the Dockerfile and all the necessary resources. You can then pass that to the next CI stage (that would have Docker but not SBT), and build/publish the actual image manually. This way you can avoid maintaining a "chimera" SBT + Docker image.

That said, I haven't tried this yet, and I can't be sure this will still be possible in the future SNP versions.

@stefangweichinger
Copy link
Author

@stefangweichinger, you can do something along the lines of:

dockerAlias := sys.env.getOrElse("CI_REGISTRY_IMAGE", sys.error("CI_REGISTRY_IMAGE not set"))

See https://www.scala-sbt.org/sbt-native-packager/formats/docker.html for more.

@elha-da, you don't have docker in your build container. The way DockerPlugin works, you need SBT, JDK and Docker in your build environment at the same time. You would have to find or make an image like this, and run your build in that.

Thanks, will try after my vacation ...

@elha-da
Copy link

elha-da commented May 20, 2019

@nigredo-tori You're right. With SBT and JDK I had to add Docker to the container. It works fine.

  script:
    - apk add docker
    - sbt 'set version in Docker := "dev"' docker:publish

Thanks a lot for your help

@muuki88 muuki88 closed this as completed May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker documentation Documentation should be extended or updated
Projects
None yet
Development

No branches or pull requests

4 participants