This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.clj
143 lines (128 loc) · 4.45 KB
/
build.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(ns build
(:require
[borkdude.gh-release-artifact :as gh]
[clojure.tools.build.api :as b]
[deps-deploy.deps-deploy :as dd])
(:import
[java.nio.file Paths]
[com.google.cloud.tools.jib.api Jib Containerizer RegistryImage TarImage]
[com.google.cloud.tools.jib.api.buildplan AbsoluteUnixPath Port]))
(def lib 'io.replikativ/datahike-server)
(def version (format "0.1.%s" (b/git-count-revs nil)))
(def current-commit (gh/current-commit))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def jar-path (format "target/%s-%s.jar" (name lib) version))
(def uber-file (format "%s-%s-standalone.jar" (name lib) version))
(def uber-path (format "target/%s" uber-file))
(def image (format "docker.io/replikativ/datahike-server:%s" version))
(def latest-image (format "docker.io/replikativ/datahike-server:latest"))
(defn get-version
[_]
(println version))
(defn clean
[_]
(b/delete {:path "target"}))
(defn compile
[_]
(b/javac {:src-dirs ["java"]
:class-dir class-dir
:basis basis
:javac-opts ["-source" "8" "-target" "8"]}))
(defn jar
[_]
(compile nil)
(b/write-pom {:class-dir class-dir
:src-pom "./template/pom.xml"
:lib lib
:version version
:basis basis
:src-dirs ["src"]})
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/jar {:class-dir class-dir
:jar-file jar-path}))
(defn uber
[_]
(clean nil)
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/compile-clj {:basis basis
:src-dirs ["src"]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-path
:basis basis
:main 'datahike-server.core}))
(defn deploy
"Don't forget to set CLOJARS_USERNAME and CLOJARS_PASSWORD env vars."
[_]
(dd/deploy {:installer :remote :artifact jar-path
:pom-file (b/pom-path {:lib lib :class-dir class-dir})}))
(defn fib [a b]
(lazy-seq (cons a (fib b (+ a b)))))
(defn retry-with-fib-backoff [retries exec-fn failed?-fn]
(loop [idle-times (take retries (fib 1 2))]
(let [result (exec-fn)]
(if (failed?-fn result)
(when-let [sleep-ms (first idle-times)]
(println "Returned: " result)
(println "Retrying with remaining back-off times (in s): " idle-times)
(Thread/sleep (* 1000 sleep-ms))
(recur (rest idle-times)))
result))))
(defn try-release []
(try (gh/overwrite-asset {:org "replikativ"
:repo (name lib)
:tag version
:commit current-commit
:file uber-path
:content-type "application/java-archive"})
(catch clojure.lang.ExceptionInfo e
(assoc (ex-data e) :failed? true))))
(defn release
[_]
(-> (retry-with-fib-backoff 10 try-release :failed?)
:url
println))
(defn install
[_]
(clean nil)
(jar nil)
(b/install {:basis (b/create-basis {})
:lib lib
:version version
:jar-file jar-path
:class-dir class-dir}))
(defn deploy-image
[{:keys [docker-login docker-password]}]
(if-not (and docker-login docker-password)
(println "Docker credentials missing.")
(let [container-builder (-> (Jib/from "gcr.io/distroless/java17-debian11")
(.addLayer [(Paths/get uber-path (into-array String[]))] (AbsoluteUnixPath/get "/"))
(.setProgramArguments [(format "/%s" uber-file)])
(.addExposedPort (Port/tcp 3000)))]
(.containerize
container-builder
(Containerizer/to
(-> (RegistryImage/named image)
(.addCredential (str docker-login) (str docker-password)))))
(.containerize
container-builder
(Containerizer/to
(-> (RegistryImage/named latest-image)
(.addCredential (str docker-login) (str docker-password)))))))
(println "Deployed new image to Docker Hub: " image))
(comment
(def docker-login "")
(def docker-password "")
(b/pom-path {:lib lib :class-dir class-dir})
(clean nil)
(compile nil)
(jar nil)
(uber nil)
(deploy-image {:docker-login docker-login
:docker-password docker-password})
(deploy nil)
(release nil)
(install nil))