Better version handling

Always generate the version through git when possible, and encode that within
the binaries so that `catala --version` does'nt give misleading information.

Previously we used dune's builtin functionality, but that resorts to a hack at
install time which is unpleasant and doesn't work with our use of `opam
install`.

The cost is a re-linking of catala_utils and the binaries upon git commit, which
is hardly noticeable.
This commit is contained in:
Louis Gesbert 2024-01-22 14:57:43 +01:00
parent 45a2aec1c9
commit 7b25a42970
8 changed files with 64 additions and 26 deletions

View File

@ -15,7 +15,17 @@ jobs:
packages: write packages: write
outputs: outputs:
image: ghcr.io/catalalang/catala@${{ steps.image.outputs.digest }} image: ghcr.io/catalalang/catala@${{ steps.image.outputs.digest }}
version: ${{ steps.describe.outputs.version }}
steps: steps:
- name: Checkout
# This is *only* needed to extract the git version...
# Approaches like proudust/gh-describe@v1 don't work
uses: actions/checkout@v3
with:
fetch-depth: 0 # Need full commit history
- name: Get git-describe version
id: describe
run: echo "version=$(git describe --tags)" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
- name: Login to GHCR - name: Login to GHCR
@ -37,7 +47,8 @@ jobs:
uses: docker/build-push-action@v5 uses: docker/build-push-action@v5
with: with:
push: true push: true
tags: ghcr.io/catalalang/catala:${{ github.run_id }} tags: ghcr.io/catalalang/catala:${{ steps.describe.outputs.version }}
build-args: "CATALA_VERSION=${{ steps.describe.outputs.version }}"
labels: org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} labels: org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
cache-from: type=registry,ref=ghcr.io/catalalang/catala:build-cache cache-from: type=registry,ref=ghcr.io/catalalang/catala:build-cache
cache-to: type=registry,ref=ghcr.io/catalalang/catala:build-cache,mode=max cache-to: type=registry,ref=ghcr.io/catalalang/catala:build-cache,mode=max
@ -120,20 +131,19 @@ jobs:
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v3 uses: actions/checkout@v3
with: - name: Get git-describe version
fetch-depth: 0 id: ghd
uses: proudust/gh-describe@v1
- name: Build release binaries - name: Build release binaries
run: mkdir -p artifacts && ./build_release.sh -C artifacts run: |
mkdir -p artifacts
export CATALA_VERSION="${{ steps.ghd.outputs.describe }}"
./build_release.sh -C artifacts
- name: Upload artifacts - name: Upload artifacts
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:
name: Catala binaries name: Catala binaries
path: artifacts/* path: artifacts/*
- name: Extract Catala version
id: version
run: echo "catala_version=$(git describe --tags)" >> "$GITHUB_OUTPUT"
outputs:
version: ${{ steps.version.outputs.catala_version }}
pages: pages:
name: Publish static content to github-pages name: Publish static content to github-pages

View File

@ -45,6 +45,8 @@ ENV OCAMLRUNPARAM=b
# defined in ./dune) # defined in ./dune)
ENV DUNE_PROFILE=check ENV DUNE_PROFILE=check
ARG CATALA_VERSION
# Check the build # Check the build
RUN opam exec -- make build js_build RUN opam exec -- make build js_build

View File

@ -2,7 +2,7 @@
set -ue set -ue
RELEASE_TAG=${RELEASE_TAG:-$(git describe --tags 2>/dev/null || echo dev)} CATALA_VERSION=${CATALA_VERSION:-$(git describe --tags 2>/dev/null || echo dev)}
BIN_TAG=${BIN_TAG:-$(uname -s)-$(uname -m)} BIN_TAG=${BIN_TAG:-$(uname -s)-$(uname -m)}
@ -30,6 +30,7 @@ docker run --rm -i registry.gitlab.inria.fr/verifisc/docker-catala:ocaml.4.14-z3
sh -uexc \ sh -uexc \
'{ tar x && '{ tar x &&
cd catala && cd catala &&
export CATALA_VERSION='"${CATALA_VERSION}"' &&
echo "'"${CUSTOM_LINKING_CATALA_Z3}"'" >compiler/custom_linking.sexp && echo "'"${CUSTOM_LINKING_CATALA_Z3}"'" >compiler/custom_linking.sexp &&
echo "'"${CUSTOM_LINKING_CLERK}"'" >build_system/custom_linking.sexp && echo "'"${CUSTOM_LINKING_CLERK}"'" >build_system/custom_linking.sexp &&
opam --cli=2.1 install ./catala.opam --destdir ../release.out/ && opam --cli=2.1 install ./catala.opam --destdir ../release.out/ &&
@ -38,8 +39,8 @@ docker run --rm -i registry.gitlab.inria.fr/verifisc/docker-catala:ocaml.4.14-z3
echo "'"${CUSTOM_LINKING_CATALA_NOZ3}"'" >compiler/custom_linking.sexp && echo "'"${CUSTOM_LINKING_CATALA_NOZ3}"'" >compiler/custom_linking.sexp &&
opam --cli=2.1 install ./catala.opam --destdir ../release.out/ && opam --cli=2.1 install ./catala.opam --destdir ../release.out/ &&
for f in ../release.out/bin/*; do case ${f} in for f in ../release.out/bin/*; do case ${f} in
*.js) mv ${f} ${f%.js}-'"${RELEASE_TAG}"'.js;; *.js) mv ${f} ${f%.js}-'"${CATALA_VERSION}"'.js;;
*) strip ${f}; mv ${f} ${f}-'"${RELEASE_TAG}"'-'"${BIN_TAG}"';; *) strip ${f}; mv ${f} ${f}-'"${CATALA_VERSION}"'-'"${BIN_TAG}"';;
esac; done; esac; done;
} >&2 && tar c -hC ../release.out/bin .' | } >&2 && tar c -hC ../release.out/bin .' |
tar vx "$@" tar vx "$@"

View File

@ -25,7 +25,6 @@ depends: [
"cppo" {>= "1"} "cppo" {>= "1"}
"dates_calc" {>= "0.0.4"} "dates_calc" {>= "0.0.4"}
"dune" {>= "3.11"} "dune" {>= "3.11"}
"dune-build-info" {>= "3.0"}
"js_of_ocaml-ppx" {= "4.1.0"} "js_of_ocaml-ppx" {= "4.1.0"}
"menhir" {>= "20200211"} "menhir" {>= "20200211"}
"menhirLib" {>= "20200211"} "menhirLib" {>= "20200211"}

View File

@ -415,10 +415,7 @@ module Flags = struct
end end
(* Retrieve current version from dune *) (* Retrieve current version from dune *)
let version = let version = Version.v
Option.value ~default:"dev"
Build_info.V1.(Option.map Version.to_string (version ()))
let s_plugins = "INSTALLED PLUGINS" let s_plugins = "INSTALLED PLUGINS"
let info = let info =

View File

@ -1,16 +1,24 @@
(library (library
(name catala_utils) (name catala_utils)
(public_name catala.catala_utils) (public_name catala.catala_utils)
(libraries (modules
unix (:standard \ get_version))
cmdliner (libraries unix cmdliner ubase ocolor re bindlib catala.runtime_ocaml))
ubase
ocolor (executable
re (name get_version)
bindlib (modules get_version)
catala.runtime_ocaml (libraries unix))
dune-build-info))
(documentation (documentation
(package catala) (package catala)
(mld_files catala_utils)) (mld_files catala_utils))
(rule
(deps
(universe)
(env_var CATALA_VERSION))
(action
(with-stdout-to
version.ml
(run %{exe:get_version.exe}))))

View File

@ -0,0 +1,17 @@
(** This trivial binary is run at build-time to get the correct version from the
build environment (either the CATALA_VERSION) environment variable if
defined, or `git describe`, or resorting to just "dev" if none of these can
be found *)
let v =
match Sys.getenv_opt "CATALA_VERSION" with
| None | Some "" -> (
let ic = Unix.open_process_in "git describe --tags --dirty 2>/dev/null" in
let v = try input_line ic with _ -> "dev" in
match Unix.close_process_in ic with Unix.WEXITED 0 -> v | _ -> "dev")
| Some v -> v
let () =
print_string "let v = \"";
print_string (String.escaped v);
print_endline "\""

View File

@ -0,0 +1,4 @@
(** The implementation of this module is generated by the build system (through
[get_version.ml]), and should only contain a static string *)
val v : string