Skip to content

Commit

Permalink
Add deploy script for Clojars (#27)
Browse files Browse the repository at this point in the history
* Add deploy script for Clojars

[Fixes #22]
  • Loading branch information
tsmacdonald authored Aug 30, 2024
1 parent ac3c663 commit daf7e04
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -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 }}
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.x
114 changes: 114 additions & 0 deletions build.clj
Original file line number Diff line number Diff line change
@@ -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! 🦅")))
6 changes: 6 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}}}

0 comments on commit daf7e04

Please sign in to comment.