Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Last active January 31, 2024 12:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wingyplus/ffa1f7b8f6a548715b9a11501f632c14 to your computer and use it in GitHub Desktop.
Save wingyplus/ffa1f7b8f6a548715b9a11501f632c14 to your computer and use it in GitHub Desktop.
Mix.install([
{:dagger, "~> 0.9.4"}
])
defmodule Ci do
alias Dagger.{Client, Container, Directory, Host}
@workdir "/app"
# TODO: change to your release name.
@release "hello_world_dagger"
def run do
client = Dagger.connect!()
project =
client
|> Client.host()
|> Host.directory(".")
builder =
client
|> Client.container()
|> Container.from("hexpm/elixir:1.15.7-erlang-26.2.1-debian-bookworm-20231009-slim")
|> Container.with_workdir(@workdir)
|> Container.with_exec(~w"apt update -y")
|> Container.with_exec(~w"apt install -y build-essential git wget")
|> Container.with_directory("config", Directory.directory(project, "/config"))
|> Container.with_file("mix.exs", Directory.file(project, "mix.exs"))
|> Container.with_file("mix.lock", Directory.file(project, "mix.lock"))
|> Container.with_exec(~w"mix local.rebar --force")
|> Container.with_exec(~w"mix local.hex --force")
|> Container.with_exec(~w"mix deps.get")
|> Container.with_env_variable("MIX_ENV", "prod")
|> Container.with_directory(
"assets",
Directory.directory(project, "/assets")
)
|> Container.with_directory(
"priv",
Directory.directory(project, "/priv")
)
|> Container.with_directory(
"lib",
Directory.directory(project, "/lib")
)
|> Container.with_exec(~w"mix assets.deploy")
|> Container.with_exec(~w"mix compile")
|> Container.with_directory("rel", Directory.directory(project, "."))
|> Container.with_exec(~w"mix release")
prod =
client
|> Client.container()
|> Container.from("debian:bookworm")
|> Container.with_workdir("/#{@release}")
|> Container.with_exec([
"sh",
"-c",
"apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales && apt-get clean && rm -f /var/lib/apt/lists/*_*"
])
|> Container.with_exec([
"sh",
"-c",
"sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen"
])
|> Container.with_env_variable("LANG", "en_US.UTF-8")
|> Container.with_env_variable("LANGUAGE", "en_US.UTF-8")
|> Container.with_env_variable("LC_ALL", "en_US.UTF-8")
|> Container.with_directory(
".",
Container.directory(builder, "_build/prod/rel/#{@release}")
)
|> Container.with_exposed_port(4000)
|> Container.with_entrypoint(["bin/#{@release}", "start"])
# Export image as tar to local system
Container.export(prod, "./test.tar")
Dagger.close(client)
end
end
Ci.run()
defmodule Mix.Tasks.Dagger.Test do
# NOTE: Add `{:dagger, "~> 0.9"}` into mix.exs file. If found `:req` version conflict,
# add `{:req, "~> 0.4", override: true}` could solve the issue.
use Mix.Task
alias Dagger.{Client, Container, Directory, Host}
@workdir "/app"
# TODO: change to your release name.
@release "hello_world_dagger"
def run(_args) do
Application.ensure_all_started(:dagger)
client = Dagger.connect!()
project =
client
|> Client.host()
|> Host.directory(".")
builder =
client
|> Client.container()
|> Container.from("hexpm/elixir:1.15.7-erlang-26.2.1-debian-bookworm-20231009-slim")
|> Container.with_workdir(@workdir)
|> Container.with_exec(~w"apt update -y")
|> Container.with_exec(~w"apt install -y build-essential git wget")
|> Container.with_directory("config", Directory.directory(project, "/config"))
|> Container.with_file("mix.exs", Directory.file(project, "mix.exs"))
|> Container.with_file("mix.lock", Directory.file(project, "mix.lock"))
|> Container.with_exec(~w"mix local.rebar --force")
|> Container.with_exec(~w"mix local.hex --force")
|> Container.with_exec(~w"mix deps.get")
|> Container.with_env_variable("MIX_ENV", "prod")
|> Container.with_directory(
"assets",
Directory.directory(project, "/assets")
)
|> Container.with_directory(
"priv",
Directory.directory(project, "/priv")
)
|> Container.with_directory(
"lib",
Directory.directory(project, "/lib")
)
|> Container.with_exec(~w"mix assets.deploy")
|> Container.with_exec(~w"mix compile")
|> Container.with_directory("rel", Directory.directory(project, "."))
|> Container.with_exec(~w"mix release")
prod =
client
|> Client.container()
|> Container.from("debian:bookworm")
|> Container.with_workdir("/#{@release}")
|> Container.with_exec([
"sh",
"-c",
"apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales && apt-get clean && rm -f /var/lib/apt/lists/*_*"
])
|> Container.with_exec([
"sh",
"-c",
"sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen"
])
|> Container.with_env_variable("LANG", "en_US.UTF-8")
|> Container.with_env_variable("LANGUAGE", "en_US.UTF-8")
|> Container.with_env_variable("LC_ALL", "en_US.UTF-8")
|> Container.with_directory(
".",
Container.directory(builder, "_build/prod/rel/#{@release}")
)
|> Container.with_exposed_port(4000)
|> Container.with_entrypoint(["bin/#{@release}", "start"])
# Export image as tar to local system
Container.export(prod, "./test.tar")
Dagger.close(client)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment