From 4868599c9ba0078cb3e339929287d80066956e8e Mon Sep 17 00:00:00 2001 From: Florian Schrofner Date: Sat, 28 Jan 2023 22:17:06 +0100 Subject: [PATCH] add git tag feature to enterprise release publisher --- enterprise-release-publisher/erp.clj | 32 ++++++++++++++++++++++---- enterprise-release-publisher/readme.md | 19 ++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/enterprise-release-publisher/erp.clj b/enterprise-release-publisher/erp.clj index fb2b4cd..bb3565c 100755 --- a/enterprise-release-publisher/erp.clj +++ b/enterprise-release-publisher/erp.clj @@ -1,8 +1,13 @@ #! /usr/bin/bb (require '[babashka.curl :as curl]) +;;executes shell command but throws exception on error +(defn- safe-sh [& commands] + (as-> (apply shell/sh commands) $ + (if (= (:exit $) 0) $ (throw (Exception. (:err $)))))) + ;;gets the working directory, but removes the new line in the end -(def wd (as-> (:out (shell/sh "pwd")) $ +(def wd (as-> (:out (safe-sh "pwd")) $ (subs $ 0 (- (count $) 1)))) (def metadata-file "output-metadata.json") (def config-file "enterprise-releases.json") @@ -18,7 +23,7 @@ (defn- create-enterprise-release [module variant] "builds the app using the given module & variant and then returns the parsed metadata" (println "building project..") - (shell/sh (create-path-string wd "gradlew") (str ":" module ":assemble" (str/capitalize variant))) + (safe-sh (create-path-string wd "gradlew") (str ":" module ":assemble" (str/capitalize variant))) (json/parse-string (slurp (create-path-string (get-output-folder module variant) metadata-file)) true)) (defn- fetch-remote-config [server path file-name] @@ -51,13 +56,30 @@ (curl/post (str server path "/" file-name ".json") {:raw-args ["-T" updated-remote-file]})) (println (str "new version " version-name " (" new-version ")" " successfully uploaded to " (name (first config)))))) +(defn- add-git-tag [config metadata] + "creates a new git tag based on the defined format and pushes it to the remote repository" + (let [unstaged-changes (filter #(not (str/blank? %)) (str/split-lines (:out (safe-sh "git" "status" "-s" "uno")))) + nr-of-changes (count unstaged-changes)] + (if (= nr-of-changes 0) + (let [tag-format (:format config) + tag-args (map #(get-in metadata [:elements 0 %]) (map #(keyword %) (:arguments config))) + tag (apply format (conj tag-args tag-format)) + remote "origin"] + (safe-sh "git" "tag" tag) + (safe-sh "git" "push" remote tag) + (println (str "created tag " tag " and pushed it to remote " remote))) + (println "warning: there were uncommitted changes. git tag was not automatically created")))) + (defn- build-and-upload [config] "builds & uploads the application based on the handed over configuration" (let [info (second config) module (:module info) - variant (:variant info)] - (as-> (create-enterprise-release module variant) $ - (upload-version config $)))) + variant (:variant info) + git-tag (:gitTag info)] + (let [metadata (create-enterprise-release module variant)] + (upload-version config metadata) + (if (not (nil? git-tag)) + (add-git-tag git-tag metadata))))) (def config (json/parse-string (slurp (create-path-string wd config-file)) true)) diff --git a/enterprise-release-publisher/readme.md b/enterprise-release-publisher/readme.md index 25c7b05..1cf1351 100644 --- a/enterprise-release-publisher/readme.md +++ b/enterprise-release-publisher/readme.md @@ -11,7 +11,11 @@ This file can contain multiple release configurations and needs to specify a cou "variant": "debug", "server": "sftp://127.0.0.1", "path": "/var/www/apps/projects/test", - "fileName": "Enterprise-Test" + "fileName": "Enterprise-Test", + "gitTag": { + "format": "ent/v%d", + "arguments": ["versionCode"] + } }, "INT": { "module": "app", @@ -22,11 +26,14 @@ This file can contain multiple release configurations and needs to specify a cou } } ``` -- `module`: specifies the module built with Gradle -- `variant`: the variant that should be built. it's combined with the module from above to create the proper Gradle command, e.g. `:app:assembleDebug` -- `server`: the server (including protocol) to upload the files to -- `path`: the path on the remote server where the files should be placed -- `fileName`: the name to be used for the apk & json files on the remote +- `module`: Specifies the module built with Gradle. +- `variant`: The variant that should be built. It's combined with the module from above to create the proper Gradle command, e.g. `:app:assembleDebug`. +- `server`: The server (including protocol) to upload the files to. +- `path`: The path on the remote server where the files should be placed. +- `fileName`: The name to be used for the apk & json files on the remote. +- `gitTag`: Optional object specifying the format of git tags that should be created automatically. + - `format`: A string specifying the tag format. Uses classic [Java formatting](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html) of strings. + - `arguments`: Arguments supplied to the format string. You can use `versionCode` and/or `versionName` here. (Theoretically it could be any value inside the `elements` key from the `output-metadata.json` file generated by the Gradle build.) If there's only one release configuration defined, it will automatically pick that one. In case there are multiple configurations you have to add the configuration name you want to build & upload as argument (see examples below).