From daf7e04cb6175ea331532e6b058b9822ea2e2bc6 Mon Sep 17 00:00:00 2001 From: Tim Macdonald Date: Fri, 30 Aug 2024 08:45:07 +0100 Subject: [PATCH] Add deploy script for Clojars (#27) * Add deploy script for Clojars [Fixes #22] --- .github/workflows/deploy.yml | 44 ++++++++++++++ VERSION | 1 + build.clj | 114 +++++++++++++++++++++++++++++++++++ deps.edn | 6 ++ 4 files changed, 165 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 VERSION create mode 100644 build.clj diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..2f78a97 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,44 @@ +name: Deploy + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-20.04 + environment: Deployment + steps: + - uses: actions/checkout@v4.1.0 + with: + fetch-depth: 0 + - name: Prepare JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'temurin' + - name: Setup Clojure + uses: DeLaGuardo/setup-clojure@12.1 + with: + cli: 1.11.1.1413 + - name: Restore cache + uses: actions/cache@v3 + with: + path: | + ~/.m2/repository + ~/.gitlibs + ~/.deps.clj + key: v1-${{ hashFiles('./deps.edn') }}-deploy + restore-keys: | + v1-${{ hashFiles('./deps.edn') }}- + v1- + - name: Build Hawk + run: clojure -T:build jar + env: + GITHUB_SHA: ${{ env.GITHUB_SHA }} + - name: Deploy Hawk + run: clojure -T:build deploy + env: + CLOJARS_USERNAME: ${{ secrets.CLOJARS_USERNAME }} + CLOJARS_PASSWORD: ${{ secrets.CLOJARS_PASSWORD }} diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..e22c2af --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.x diff --git a/build.clj b/build.clj new file mode 100644 index 0000000..9df3258 --- /dev/null +++ b/build.clj @@ -0,0 +1,114 @@ +;; Further info: https://clojure.org/guides/tools_build#_mixed_java_clojure_build + +(ns build + (:refer-clojure :exclude [compile]) + (:require + [clojure.java.shell :as sh] + [clojure.string :as str] + [clojure.tools.build.api :as b] + [deps-deploy.deps-deploy :as dd])) + +(def lib 'io.github.metabase/hawk) +(def github-url "https://github.com/metabase/hawk") +(def scm-url "git@github.com:metabase/hawk.git") + +(def version-template (str/trim (slurp "VERSION"))) +(assert (re-matches #"\d+\.\d+\.x" version-template)) + +(def major-minor-version (str/replace version-template #"\.x$" "")) + +(defn sh [& args] + (let [{:keys [exit out]} (apply sh/sh args)] + (assert (zero? exit)) + (str/trim out))) + +(defn- commits-since-version-changed [] + (let [last-sha (sh "git" "log" "-1" "--format=%H" "--" "VERSION")] + (parse-long (sh "git" "rev-list" "--count" (str last-sha "..HEAD"))))) + +(defn commit-number [] + (if (= "master" (sh "git" "rev-parse" "--abbrev-ref" "HEAD")) + (commits-since-version-changed) + "9999-SNAPSHOT")) + +(def sha + (or (not-empty (System/getenv "GITHUB_SHA")) + (not-empty (-> (sh/sh "git" "rev-parse" "HEAD") + :out + str/trim)))) + +(def version (str major-minor-version \. (commit-number))) +(def target "target") +(def class-dir (format "%s/classes" target)) + +(def jar-file (format "target/%s-%s.jar" (name lib) version)) + +(def basis (delay (b/create-basis {:project "deps.edn"}))) + +(def pom-template + [[:description "It watches your code like a hawk!"] + [:url github-url] + [:licenses + [:license + [:name "Eclipse Public License"] + [:url "http://www.eclipse.org/legal/epl-v20.html"]]] + [:developers + [:developer + [:name "Cam Saul"]] + [:developer + [:name "John Cromartie"]] + [:developer + [:name "Nemanja Glumac"]] + [:developer + [:name "Cal Herries"]] + [:developer + [:name "Ngoc Khuat"]] + [:developer + [:name "Tim Macdonald"]] + [:developer + [:name "Case Nelson"]] + [:developer + [:name "Filipe Silva"]] + [:developer + [:name "Dan Sutton"]] + [:developer + [:name "Chris Truter"]]] + [:scm + [:url github-url] + [:connection (str "scm:git:" scm-url)] + [:developerConnection (str "scm:git:" scm-url)] + [:tag sha]]]) + +(def default-options + {:lib lib + :version version + :jar-file jar-file + :basis @basis + :class-dir class-dir + :target target + :src-dirs ["src"] + :pom-data pom-template}) + +(defn clean [_] + (b/delete {:path target})) + +(defn jar [opts] + (println "\nStarting to build a JAR...") + (println "\tWriting pom.xml...") + (b/write-pom (merge default-options opts)) + (println "\tCopying source...") + (b/copy-dir {:src-dirs ["src" "resources"] + :target-dir class-dir}) + (printf "\tBuilding %s...\n" jar-file) + (b/jar {:class-dir class-dir + :jar-file jar-file}) + (println "Done! 🦜")) + + +(defn deploy [opts] + (let [opts (merge default-options opts)] + (printf "Deploying %s...\n" jar-file) + (dd/deploy {:installer :remote + :artifact (b/resolve-path jar-file) + :pom-file (b/pom-path (select-keys opts [:lib :class-dir]))}) + (println "Deployed! 🦅"))) diff --git a/deps.edn b/deps.edn index a85e5f1..8c954a2 100644 --- a/deps.edn +++ b/deps.edn @@ -22,5 +22,11 @@ {:exec-fn mb.hawk.core/find-and-run-tests-cli :exec-args {:exclude-directories ["src" "resources"]}} + ;; clojure -T:build + :build + {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"} + slipset/deps-deploy {:mvn/version "0.2.1"}} + :ns-default build} + :ci {:jvm-opts ["-Dhawk.mode" "cli/ci"]}}}