Skip to content

Commit 07a5c00

Browse files
authored
Merge pull request #61 from Flexiana/deploy-clojars
Add Clojars deployment
2 parents df9e276 + 6a59b27 commit 07a5c00

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

.github/clojars_deploy.bb

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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!)

.github/workflows/deploy.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Push to Clojars
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
jobs:
9+
push-to-clojars:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Setup Clojure
16+
uses: DeLaGuardo/[email protected]
17+
with:
18+
cli: '1.10.2.796'
19+
20+
- name: Cache
21+
uses: actions/[email protected]
22+
with:
23+
path: ~/.m2
24+
key: m2-${{ hashFiles('project.clj') }}
25+
restore-keys: |
26+
m2-
27+
28+
- name: Setup babashka
29+
run: |
30+
curl -L https://github.com/borkdude/babashka/releases/download/v0.2.5/babashka-0.2.5-linux-amd64.zip -o bb.zip
31+
unzip bb.zip
32+
chmod +x bb
33+
sudo mv bb /usr/bin
34+
35+
- name: Set TAG env variable
36+
run: echo "TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
37+
38+
- name: Deploy to Clojars
39+
env:
40+
CLOJARS_LOGIN: ${{ secrets.CLOJARS_LOGIN }}
41+
CLOJARS_PASSWORD: ${{ secrets.CLOJARS_PASSWORD }}
42+
GPG_OWNERTRUST: ${{ secrets.GPG_OWNERTRUST }}
43+
GPG_SECRET_KEYS: ${{ secrets.GPG_SECRET_KEYS }}
44+
TAG: ${{ env.TAG }}
45+
run: /usr/bin/bb ./.github/clojars_deploy.bb

0 commit comments

Comments
 (0)