From 53032fcbe136fa401cb4739fa1fc4eec68b55f7f Mon Sep 17 00:00:00 2001 From: Jamil Date: Thu, 16 Jan 2025 14:26:22 -0800 Subject: [PATCH] fix(ci): Populate elixir vsn from env at build time (#7773) Dependabot's workflow is set up in such a way it seems that it can't find our `sha.exs` file. This is a cleaner approach that doesn't rely on using external files for the application version. Interesting note: `mix compile` will happily use the cached `version` even though it's computed from an env var, because `mix compile` uses file hash and mtime to know when to recompile. See https://github.com/firezone/firezone/network/updates/942719116 --- elixir/Dockerfile | 11 +++++------ elixir/apps/api/mix.exs | 9 ++++++--- elixir/apps/domain/mix.exs | 9 ++++++--- elixir/apps/web/mix.exs | 9 ++++++--- elixir/mix.exs | 9 ++++++--- elixir/sha.exs | 7 ------- 6 files changed, 29 insertions(+), 25 deletions(-) delete mode 100644 elixir/sha.exs diff --git a/elixir/Dockerfile b/elixir/Dockerfile index c149654be..9a37e9436 100644 --- a/elixir/Dockerfile +++ b/elixir/Dockerfile @@ -226,10 +226,10 @@ COPY apps/domain/mix.exs ./apps/domain/mix.exs COPY apps/web/mix.exs ./apps/web/mix.exs COPY apps/api/mix.exs ./apps/api/mix.exs COPY config config -COPY sha.exs . # Fetch and compile the dependencies ARG MIX_ENV="prod" +ARG GIT_SHA RUN mix deps.get --only ${MIX_ENV} RUN mix deps.compile --skip-umbrella-children @@ -251,7 +251,9 @@ RUN cd apps/web \ && mix assets.deploy # Copy the rest of the application files and compile them -RUN mix compile +# mix doesn't know when GIT_SHA changes, so --force is needed to avoid +# pulling in a cached version. +RUN mix compile --force FROM elixir AS builder @@ -269,6 +271,7 @@ COPY rel rel ARG APPLICATION_NAME ARG MIX_ENV="prod" +ARG GIT_SHA RUN mix release ${APPLICATION_NAME} # start a new build stage so that the final image will only contain @@ -301,10 +304,6 @@ COPY --from=builder /app/_build/${MIX_ENV}/rel/${APPLICATION_NAME} ./ # Allow the default user to write to the priv directory of some libraries RUN chmod -R ugo+rw /app/lib/tzdata-*/priv -# Populate the SHA pointing to this build -ARG GIT_SHA -RUN echo ${GIT_SHA} > ./GIT_SHA - # Change user to "default" to limit runtime privileges USER default diff --git a/elixir/apps/api/mix.exs b/elixir/apps/api/mix.exs index fbbf94198..4407eb9cb 100644 --- a/elixir/apps/api/mix.exs +++ b/elixir/apps/api/mix.exs @@ -2,11 +2,9 @@ defmodule API.MixProject do use Mix.Project def project do - {sha, _} = Code.eval_file(Path.join([__DIR__, "..", "..", "sha.exs"])) - [ app: :api, - version: "0.1.0+#{sha}", + version: version(), build_path: "../../_build", config_path: "../../config/config.exs", deps_path: "../../deps", @@ -79,4 +77,9 @@ defmodule API.MixProject do test: ["ecto.create --quiet", "ecto.migrate", "test"] ] end + + defp version do + sha = System.get_env("GIT_SHA", "deadbeef") |> String.trim() + "0.1.0+#{sha}" + end end diff --git a/elixir/apps/domain/mix.exs b/elixir/apps/domain/mix.exs index 336fae6d6..46e875a7e 100644 --- a/elixir/apps/domain/mix.exs +++ b/elixir/apps/domain/mix.exs @@ -2,11 +2,9 @@ defmodule Domain.MixProject do use Mix.Project def project do - {sha, _} = Code.eval_file(Path.join([__DIR__, "..", "..", "sha.exs"])) - [ app: :domain, - version: "0.1.0+#{sha}", + version: version(), build_path: "../../_build", config_path: "../../config/config.exs", deps_path: "../../deps", @@ -112,4 +110,9 @@ defmodule Domain.MixProject do test: ["ecto.create --quiet", "ecto.migrate", "test"] ] end + + defp version do + sha = System.get_env("GIT_SHA", "deadbeef") |> String.trim() + "0.1.0+#{sha}" + end end diff --git a/elixir/apps/web/mix.exs b/elixir/apps/web/mix.exs index 4b25d1a2b..4fadfecae 100644 --- a/elixir/apps/web/mix.exs +++ b/elixir/apps/web/mix.exs @@ -2,11 +2,9 @@ defmodule Web.MixProject do use Mix.Project def project do - {sha, _} = Code.eval_file(Path.join([__DIR__, "..", "..", "sha.exs"])) - [ app: :web, - version: "0.1.0+#{sha}", + version: version(), build_path: "../../_build", config_path: "../../config/config.exs", deps_path: "../../deps", @@ -104,4 +102,9 @@ defmodule Web.MixProject do test: ["ecto.create --quiet", "ecto.migrate", "test"] ] end + + defp version do + sha = System.get_env("GIT_SHA", "deadbeef") |> String.trim() + "0.1.0+#{sha}" + end end diff --git a/elixir/mix.exs b/elixir/mix.exs index 64567d55e..bf6a5614c 100644 --- a/elixir/mix.exs +++ b/elixir/mix.exs @@ -2,12 +2,10 @@ defmodule Firezone.MixProject do use Mix.Project def project do - {sha, _} = Code.eval_file(Path.join([__DIR__, "sha.exs"])) - [ name: :firezone, apps_path: "apps", - version: "0.1.0+#{sha}", + version: version(), start_permanent: Mix.env() == :prod, test_coverage: [tool: ExCoveralls], preferred_cli_env: [ @@ -95,4 +93,9 @@ defmodule Firezone.MixProject do ] ] end + + defp version do + sha = System.get_env("GIT_SHA", "deadbeef") |> String.trim() + "0.1.0+#{sha}" + end end diff --git a/elixir/sha.exs b/elixir/sha.exs deleted file mode 100644 index 30b43ac26..000000000 --- a/elixir/sha.exs +++ /dev/null @@ -1,7 +0,0 @@ -case File.read(Path.join([__DIR__, "GIT_SHA"])) do - {:ok, sha} -> - sha |> String.trim() - - _ -> - "deadbeef" -end