From b3713a9c3b50a17ba526995a21b80630bc0082a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr>
Date: Thu, 24 Jun 2021 20:50:06 +0200
Subject: [PATCH] Add automatic tag creation on release CI step

---
 .gitlab-ci.yml            | 35 ++++++++++++++++++++++++++---------
 android/gradle.properties |  4 ++--
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 77543d5..2b99f15 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,18 +1,12 @@
 image: cirrusci/flutter:latest
 
 stages:
-  - test
   - update
   - build-debug
   - build-release
+  - release
   - deploy
 
-tests:
-  stage: test
-  script:
-    - flutter test
-  interruptible: true
-
 update:
   stage: update
   script:
@@ -74,11 +68,34 @@ android:build-release:
     expire_in: 1 week
   interruptible: true
 
-android:deploy:
-  stage: deploy
+application:release:
+  stage: release
+  image:
+    name: alpine/git
+    entrypoint: [""]
   only:
     - master
+  except:
+      - tags
   dependencies:
     - android:build-release
+  script:
+    - git config user.email "${GITLAB_USER_EMAIL}"
+    - git config user.name "${GITLAB_USER_NAME}"
+    - git remote set-url origin https://oauth2:${GITLAB_ACCESS_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}
+    - VERSION_FILE="$(find . -name 'gradle.properties' | head -n1)"
+    - VERSION_NAME="$(grep '^app.versionName=' "${VERSION_FILE}" | cut -d'=' -f2)"
+    - VERSION_CODE="$(grep '^app.versionCode=' "${VERSION_FILE}" | cut -d'=' -f2)"
+    - TAG_NAME="Release_${VERSION_CODE}_${VERSION_NAME}"
+    - echo "${TAG_NAME}"
+    - git tag -a "${TAG_NAME}" -m "Release ${VERSION_NAME} (${VERSION_CODE})"
+    - git push origin "${TAG_NAME}"
+
+android:deploy:
+  stage: deploy
+  only:
+    - tags
+  dependencies:
+    - application:release
   script:
     - wget ${REPOSITORY_UPDATE_WEBHOOK}?token=${REPOSITORY_TOKEN}
diff --git a/android/gradle.properties b/android/gradle.properties
index 379a5b7..05f2f47 100644
--- a/android/gradle.properties
+++ b/android/gradle.properties
@@ -1,5 +1,5 @@
 org.gradle.jvmargs=-Xmx1536M
 android.useAndroidX=true
 android.enableJetifier=true
-app.versionName=1.0.1
-app.versionCode=2
+app.versionName=1.0.2
+app.versionCode=3
-- 
GitLab