Ocaml project deploy with terraform, ansible and docker

I’m doing small pet project on ocaml and all went well until I decided to write some deployment scripts.
I must say, that I’m not seeking easy solution, my goal (just as an exercise) is total automation.
So, I thought about nix, but I don’t like the idea to use it for package management instead of opam + dune.
I decided to try terraform + anasible + docker.
Terraform for creating digital ocean droplet.
Ansible for basic droplet configuration.
Docker for building application.
There are 2 major problems:

  1. Building my project requires a lot of RAM, at least 3GB, so I have to use expensive server. But I guess I can overcome it by building image locally inside docker.
  2. Docker has mount=cache feature for caching dependencies, however I cannot make it work with opam. Are there some exmaples on how it can be done?

Or may be there is some other way I didn’t think of?

sounds like a static binary to me. Like e.g. https://demo.mro.name/geohash.cgi if for the web. The production machine needs no ocaml/opam/dune.

You sure will learn about developing with ocaml, but not about terraform, ansible or docker.

Well, in the end I decided to GH actions (Set up OCaml · Actions · GitHub Marketplace · GitHub) to build binary and publish it as an artefact. Terraform creates a droplet, ansible uses TF state to ssh to the created droplet, download the binary from GH actions, create a systemd service and restarts it.
The only challenge was to integrate ansible and gh-actions. I ended up writing my custom python ansible module, but actually I could may be use gh-cli via ansible for that.
Anyway, it works and I’m pretty happy.

The only problem is that ocaml gh action does not caches builded deps, so despite the fact that I deployed self-hosted GH actions runner on my home computer with Ryzen 5900, it still runs approx 5 minutes in total.

1 Like