From ea1812f0f14a06214d4c709f2005100a18d2c2a3 Mon Sep 17 00:00:00 2001
From: "Mr. Finch" <mr.finch@binary-io.net>
Date: Tue, 14 Jan 2025 19:30:55 +0000
Subject: [PATCH] ci: adds version calculation via gitversion and autotagging
 on defaultbranch

---
 .gitlab-ci.yml                       | 32 ++++++++++++++++++++++++++++
 .gitlab-ci/git/create_tag.yml        | 15 +++++++++++++
 .gitlab-ci/versioning/gitversion.yml | 31 +++++++++++++++++++++++++++
 Gitversion.yml                       |  3 +++
 4 files changed, 81 insertions(+)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 .gitlab-ci/git/create_tag.yml
 create mode 100644 .gitlab-ci/versioning/gitversion.yml
 create mode 100644 Gitversion.yml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..cd116fe
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,32 @@
+---
+
+include:
+  - local: .gitlab-ci/versioning/gitversion.yml
+  - local: .gitlab-ci/git/create_tag.yml
+
+stages:
+  - release
+
+gitversion:
+  extends: .versioning:gitversion
+  stage: .pre
+  tags:
+    - gitlab-org-docker
+  rules:
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH  # Run this job when commits are pushed or merged to the default branch
+
+create_tag:
+  extends: .git:create_tag
+  stage: release
+  tags:
+    - gitlab-org-docker
+  variables:
+    VERSION: v$GitVersion_SemVer
+    TOKEN: $GITLAB_TOKEN
+  needs:
+    - job: gitversion
+      artifacts: true
+  rules:
+    - if: $CI_COMMIT_TAG
+      when: never                                  # Do not run this job when a tag is created manually
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH  # Run this job when commits are pushed or merged to the default branch
diff --git a/.gitlab-ci/git/create_tag.yml b/.gitlab-ci/git/create_tag.yml
new file mode 100644
index 0000000..2c1afd7
--- /dev/null
+++ b/.gitlab-ci/git/create_tag.yml
@@ -0,0 +1,15 @@
+---
+
+.git:create_tag:
+  image: alpine:3.21
+  variables:
+    GIT_STRATEGY: clone
+    GIT_DEPTH: 0
+    GIT_LFS_SKIP_SMUDGE: 1
+    VERSION: ''
+    TOKEN: '' # Token with push privileges
+  script:
+    - apk add git
+    - git remote set-url origin https://oauth2:$TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH
+    - git tag $VERSION
+    - git push origin tag $VERSION
diff --git a/.gitlab-ci/versioning/gitversion.yml b/.gitlab-ci/versioning/gitversion.yml
new file mode 100644
index 0000000..5391fdf
--- /dev/null
+++ b/.gitlab-ci/versioning/gitversion.yml
@@ -0,0 +1,31 @@
+---
+
+.versioning:gitversion:
+  image:
+    name: mcr.microsoft.com/dotnet/sdk:9.0
+  variables:
+    GIT_STRATEGY: clone
+    GIT_DEPTH: 0 # force a deep/non-shallow fetch need by gitversion
+    GIT_LFS_SKIP_SMUDGE: 1
+  cache: [] # caches and before / after scripts can mess things up
+  script:
+    - |
+      dotnet tool install --global GitVersion.Tool --version 5.*
+      export PATH="$PATH:/root/.dotnet/tools"
+
+      dotnet-gitversion -output buildserver
+
+      # We could just collect the output file gitversion.properties (with artifacts:report:dotenv: gitversion.properties as it is already in DOTENV format,
+      # however it contains ~33 variables which unnecessarily consumes many of the 50 max DOTENV variables of the free GitLab version.
+      # Limits are higher for licensed editions, see https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsdotenv
+      grep 'GitVersion_LegacySemVer='    gitversion.properties >> gitversion.env
+      grep 'GitVersion_SemVer='          gitversion.properties >> gitversion.env
+      grep 'GitVersion_FullSemVer='      gitversion.properties >> gitversion.env
+      grep 'GitVersion_Major='           gitversion.properties >> gitversion.env
+      grep 'GitVersion_Minor='           gitversion.properties >> gitversion.env
+      grep 'GitVersion_Patch='           gitversion.properties >> gitversion.env
+      grep 'GitVersion_MajorMinorPatch=' gitversion.properties >> gitversion.env
+  artifacts:
+    reports:
+      # propagates variables into the pipeline level
+      dotenv: gitversion.env
diff --git a/Gitversion.yml b/Gitversion.yml
new file mode 100644
index 0000000..165f995
--- /dev/null
+++ b/Gitversion.yml
@@ -0,0 +1,3 @@
+---
+
+mode: Mainline