-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.clj
176 lines (147 loc) · 5.38 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(ns monkey.ci.build
"Functions for working with the build object in the runtime. This
represents the current build."
(:require [babashka.fs :as fs]
[clojure.java.io :as io]
[clojure.string :as cs]
[medley.core :as mc]
[monkey.ci
[runtime :as rt]
[storage :as st]
[utils :as u]]
[monkey.ci.build.core :as bc]))
(def account->sid (juxt :customer-id :repo-id))
(def build-sid-length 3)
(defn get-sid
"Gets current build sid from the runtime. This is either specified directly,
or taken from account settings."
[rt]
(or (get-in rt [:build :sid])
(let [sid (->> (account->sid (rt/account rt))
(take-while some?))]
(when (= (dec build-sid-length) (count sid))
sid))))
(defn get-build-id [rt]
(or (get-in rt [:build :build-id]) "unknown-build"))
(def get-job-sid
"Creates a job sid using the build id and job id. Note that this does
not include the customer and repo ids, so this is only unique within the repo."
(comp (partial mapv str)
(juxt get-build-id
(comp bc/job-id :job))))
(def get-job-id
"Creates a string representation of the job sid"
(comp (partial cs/join "-") get-job-sid))
(def rt->job-id (comp bc/job-id :job))
(defn- maybe-set-git-opts
"If a git url is specified, updates the build with git information, taken from
the arguments and from runtime."
[build rt]
(let [{:keys [git-url branch commit-id dir]} (rt/args rt)
existing (get-in rt [:build :git])]
(cond-> build
git-url (-> (assoc :git (assoc existing
:url git-url
:branch (or branch "main")
:id commit-id))
;; Overwrite script dir cause it will be calculated by the git checkout
(assoc-in [:script :script-dir] dir)))))
(defn- includes-build-id? [sid]
(= build-sid-length (count sid)))
(defn make-build-ctx
"Creates a build context that can be added to the runtime."
[rt]
(let [work-dir (rt/work-dir rt)
orig-sid (or (some->> (:sid (rt/args rt))
(u/parse-sid)
(take build-sid-length))
(get-sid rt))
;; Either generate a new build id, or use the one given
sid (st/->sid (if (or (empty? orig-sid) (includes-build-id? orig-sid))
orig-sid
(concat orig-sid [(u/new-build-id)])))
id (or (last sid) (u/new-build-id))]
(maybe-set-git-opts
{:customer-id (first sid)
:repo-id (second sid)
:build-id id
:checkout-dir work-dir
:script {:script-dir (u/abs-path work-dir (rt/get-arg rt :dir))}
:pipeline (rt/get-arg rt :pipeline)
:sid sid}
rt)))
(def script "Gets script from the build"
:script)
(def script-dir
"Gets script dir from the build"
(comp :script-dir script))
(defn set-script-dir [b d]
(assoc-in b [:script :script-dir] d))
(def rt->script-dir "Gets script dir for the build from runtime"
(comp script-dir rt/build))
(defn- build-related-dir
([base-dir-key rt build-id]
(some-> rt
(base-dir-key)
(u/combine build-id)))
([base-dir-key rt]
(build-related-dir base-dir-key rt (get-in rt [:build :build-id]))))
(def calc-checkout-dir
"Calculates the checkout directory for the build, by combining the checkout
base directory and the build id."
(partial build-related-dir (rt/from-config :checkout-base-dir)))
(def checkout-dir
"Gets the checkout dir as stored in the build structure"
:checkout-dir)
(defn set-checkout-dir [b d]
(assoc b :checkout-dir d))
(def rt->checkout-dir (comp checkout-dir rt/build))
(def default-script-dir ".monkeyci")
(defn calc-script-dir
"Given an (absolute) working directory and scripting directory, determines
the absolute script dir."
[wd sd]
(->> (or sd default-script-dir)
(u/abs-path wd)
(io/file)
(.getCanonicalPath)))
(def ssh-keys-dir
"Calculates ssh keys dir for the build"
(partial build-related-dir (rt/from-config :ssh-keys-dir)))
(defn exit-code->status [exit]
(when (number? exit)
(if (zero? exit) :success :error)))
(defn build->evt
"Prepare build object so it can be added to an event"
[build]
(mc/update-existing build :git dissoc :ssh-keys))
(defn build-end-evt
"Creates a `build/end` event"
[build & [exit-code]]
{:type :build/end
:sid (:sid build)
:build (-> build
(build->evt)
(assoc :end-time (u/now))
(mc/assoc-some :status (exit-code->status exit-code)))})
(def ^:deprecated build-completed-evt build-end-evt)
(defn job-work-dir
"Given a runtime, determines the job working directory. This is either the
work dir as configured on the job, or the context work dir, or the process dir."
[rt]
(-> (if-let [jwd (get-in rt [:job :work-dir])]
(if (fs/absolute? jwd)
jwd
(if-let [cd (rt->checkout-dir rt)]
(fs/path cd jwd)
jwd))
(or (rt->checkout-dir rt) (u/cwd)))
(fs/canonicalize)
(str)))
(defn job-relative-dir
"Calculates path `p` as relative to the work dir for the current job"
[rt p]
(u/abs-path (job-work-dir rt) p))
(defmethod rt/setup-runtime :build [conf _]
;; Just copy the build info to the runtime
(get conf :build))