|
| 1 | +#!/usr/bin/env bb |
| 2 | +;; credits: https://github.com/mauricioszabo/clj-lib-deployer/blob/master/deploy-lein.bb |
| 3 | + |
| 4 | +(defn- get-project! [] |
| 5 | + (-> "project.clj" |
| 6 | + slurp |
| 7 | + edn/read-string |
| 8 | + (nth 1))) |
| 9 | + |
| 10 | +(defn- get-version! [] |
| 11 | + (-> "project.clj" |
| 12 | + slurp |
| 13 | + edn/read-string |
| 14 | + (nth 2))) |
| 15 | + |
| 16 | +(defn- can-deploy? [] |
| 17 | + (let [curr-version (get-version!) |
| 18 | + status (:status (curl/get (str "https://clojars.org/" (get-project!) |
| 19 | + "/versions/" (get-version!)) |
| 20 | + {:throw false}))] |
| 21 | + (= 404 status))) |
| 22 | + |
| 23 | +(defn- tag-name [] (System/getenv "TAG")) |
| 24 | + |
| 25 | +(defn- decode-base64 [string] |
| 26 | + (-> java.util.Base64 |
| 27 | + .getDecoder |
| 28 | + (.decode string))) |
| 29 | + |
| 30 | +(defn run-shell-cmd [ & args] |
| 31 | + (let [{:keys [exit out err] :as result} (apply shell/sh args)] |
| 32 | + (when-not (zero? exit) |
| 33 | + (println "ERROR running command\nSTDOUT:") |
| 34 | + (println out "\nSTDERR:") |
| 35 | + (println err) |
| 36 | + (throw (ex-info "Error while runing shell command" {:status exit}))) |
| 37 | + result)) |
| 38 | + |
| 39 | +(defn- import-gpg! [] |
| 40 | + (let [secret (System/getenv "GPG_SECRET_KEYS") |
| 41 | + ownertrust (System/getenv "GPG_OWNERTRUST")] |
| 42 | + (when-not (and secret ownertrust) (throw (ex-info "Can't find GPG keys!" {}))) |
| 43 | + (run-shell-cmd "gpg" "--import" :in (decode-base64 secret)) |
| 44 | + (run-shell-cmd "gpg" "--import-ownertrust" :in (decode-base64 ownertrust)))) |
| 45 | + |
| 46 | +(defn deploy! [] |
| 47 | + (let [tag (not-empty (tag-name))] |
| 48 | + (when-not (can-deploy?) |
| 49 | + (throw (ex-info "Can't deploy this version - release version already exist on clojars" |
| 50 | + {:version (get-version!)}))) |
| 51 | + |
| 52 | + (when (some-> tag (str/replace-first #"v" "") (not= (get-version!))) |
| 53 | + (throw (ex-info "Tag version mismatches with project.clj" |
| 54 | + {:tag-name tag |
| 55 | + :version (get-version!)}))) |
| 56 | + |
| 57 | + (if tag |
| 58 | + (do |
| 59 | + (import-gpg!) |
| 60 | + (println "Deploying a release version")) |
| 61 | + (do |
| 62 | + (println "Deploying a snapshot version") |
| 63 | + (run-shell-cmd "lein" "change" "version" "str" "\"-SNAPSHOT\""))) |
| 64 | + |
| 65 | + (run-shell-cmd "lein" "change" ":deploy-repositories" "concat" |
| 66 | + (pr-str [["releases" {:url "https://repo.clojars.org/" |
| 67 | + :username :env/clojars_login |
| 68 | + :password :env/clojars_password}]])) |
| 69 | + (run-shell-cmd "lein" "deploy" "releases") |
| 70 | + (println "Deploy was successful"))) |
| 71 | + |
| 72 | +(deploy!) |
0 commit comments