From b97eabc403593a3f9a0d05fe1715f48d9e84a478 Mon Sep 17 00:00:00 2001
From: Cyril Danilevski <cydanil@gmail.com>
Date: Thu, 8 Feb 2024 21:59:45 +0100
Subject: [PATCH] Add Git commit sha and time to CI build

---
 .gitlab-ci.yml |  2 +-
 README.md      |  9 ++++++++-
 rest.cpp       | 14 +++++++++++++-
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index fdf8712..93241b8 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -30,7 +30,7 @@ build:
     - arduino-cli lib install --git-url https://github.com/patricklaf/SNMP.git --config-file arduino-cli.yaml
 
     # Compile artifact
-    - arduino-cli compile --fqbn esp32:esp32:esp32wroverkit icbm.ino --output-dir firmware
+    - arduino-cli compile --fqbn esp32:esp32:esp32wroverkit icbm.ino --output-dir firmware --build-property build.extra_flags="-DICBM_GIT_VERSION=$CI_COMMIT_SHORT_SHA -DICBM_GIT_TIMESTAMP=$CI_COMMIT_TIMESTAMP"
     - zip -r firmware.zip firmware
   artifacts:
     when: always
diff --git a/README.md b/README.md
index e4c58da..56d5331 100644
--- a/README.md
+++ b/README.md
@@ -35,7 +35,7 @@ board_manager:
       - http://arduino.esp8266.com/stable/package_esp8266com_index.json
 ```
 
-Update the arduino index with:
+Update the Arduino index with:
 
 ```bash
 arduino-cli core update-index
@@ -55,6 +55,13 @@ And uploaded so:
 
     arduino-cli upload -p /dev/ttyUSB1 --fqbn esp32:esp32:esp32wroverkit
 
+
+### Versioning
+
+The firmware version is provided as part of the `/idn` route.  
+This is the git tag or commit, if `-DICBM_GIT_VERSION` is set.  
+The git date can be set via `-DICBM_GIT_TIMESTAMP`.  
+
 ### CI Artefacts
 
 CI artefacts can be uploaded so:
diff --git a/rest.cpp b/rest.cpp
index 1cade9c..93fb91e 100644
--- a/rest.cpp
+++ b/rest.cpp
@@ -1,6 +1,14 @@
 #include "rest.hpp"
 #include <ETH.h>
 
+#ifndef ICBM_GIT_VERSION
+#define ICBM_GIT_VERSION "DEVEL"  // set via -DICBM_GIT_VERSION
+#endif
+
+#ifndef ICBM_GIT_TIMESTAMP
+#define ICBM_GIT_TIMESTAMP "UNKNOWN"
+#endif
+
 WebServer restServer(80);
 
 void initializeRoutes() {
@@ -26,7 +34,11 @@ void identify() {
 	message += "\",\n";
 
 	message += "\"version\":\"";
-	message += "0.1";  // TODO: Get from Git
+	message += ICBM_GIT_VERSION;
+	message += "\",\n";
+
+	message += "\"build date\":\"";
+	message += ICBM_GIT_TIMESTAMP;
 	message += "\",\n";
 
 	message += "\"uptime\": \"";
-- 
GitLab