add git tag feature to enterprise release publisher

master
Florian Schrofner 2023-01-28 22:17:06 +01:00
parent f95b7ccd7d
commit 4868599c9b
2 changed files with 40 additions and 11 deletions

View File

@ -1,8 +1,13 @@
#! /usr/bin/bb #! /usr/bin/bb
(require '[babashka.curl :as curl]) (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 ;;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)))) (subs $ 0 (- (count $) 1))))
(def metadata-file "output-metadata.json") (def metadata-file "output-metadata.json")
(def config-file "enterprise-releases.json") (def config-file "enterprise-releases.json")
@ -18,7 +23,7 @@
(defn- create-enterprise-release [module variant] (defn- create-enterprise-release [module variant]
"builds the app using the given module & variant and then returns the parsed metadata" "builds the app using the given module & variant and then returns the parsed metadata"
(println "building project..") (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)) (json/parse-string (slurp (create-path-string (get-output-folder module variant) metadata-file)) true))
(defn- fetch-remote-config [server path file-name] (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]})) (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)))))) (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] (defn- build-and-upload [config]
"builds & uploads the application based on the handed over configuration" "builds & uploads the application based on the handed over configuration"
(let [info (second config) (let [info (second config)
module (:module info) module (:module info)
variant (:variant info)] variant (:variant info)
(as-> (create-enterprise-release module variant) $ git-tag (:gitTag info)]
(upload-version config $)))) (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)) (def config (json/parse-string (slurp (create-path-string wd config-file)) true))

View File

@ -11,7 +11,11 @@ This file can contain multiple release configurations and needs to specify a cou
"variant": "debug", "variant": "debug",
"server": "sftp://127.0.0.1", "server": "sftp://127.0.0.1",
"path": "/var/www/apps/projects/test", "path": "/var/www/apps/projects/test",
"fileName": "Enterprise-Test" "fileName": "Enterprise-Test",
"gitTag": {
"format": "ent/v%d",
"arguments": ["versionCode"]
}
}, },
"INT": { "INT": {
"module": "app", "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 - `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` - `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 - `server`: The server (including protocol) to upload the files to.
- `path`: the path on the remote server where the files should be placed - `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 - `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). 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).