A small agent to run CI scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.5 KiB

(require spork)
(def start-time (os/time))
(assert (= :linux (os/which)) "This tool can only run on a Linux-based system")
(def os-release
(let (run (os/spawn @("sh" "-c" ". /etc/os-release; echo $ID") :p {:out :pipe}))
(:wait run)
(string/trim (:read (run :out) :all))))
(def pkg-install-command
(case os-release
"arch" "pacman -S --noconfirm"
"alpine" "apk add --update-cache"
"debian" "apt update; apt upgrade -y; apt install -y"))
(def rules (require "/archetype"))
(assert (get rules 'runner) "A runner must be defined in the archetype document")
(if-let (secrets (get rules 'secrets))
(map (fn [var] (let (name (string var))
(assert (os/getenv name)
(string name ": Undefined environment variable"))))
(get secrets :value)))
(if-let (secret-files (get rules 'secret-files))
(map (fn [var] (let ([name path] var
var (os/getenv (string name)))
(assert var (string name ": Undefined environment variable"))
(assert (os/shell (string "mkdir -p " (path/dirname path)))
"Impossible to create target directory")
(spit path var)
(get secret-files :value)))
(if-let (pkgs (get rules 'pkgs))
(assert (= 0 (os/shell (string/join ~(,pkg-install-command
,;(map string (get pkgs :value)))
" ")))
"Error during package installation"))
(if-let (requires (get rules 'requires))
(map (fn [block]
(print (get block 0))
(match block
[:env vals] nil))
(get requires :value)))
(var step-counter 1)
(let (build (get rules 'build))
(assert build "You must have a build block")
(map (fn [rule]
(printf "Step #%d: %M" step-counter rule)
(if (not (= 0
(match rule
[:run command] (os/shell command)
[:upload origin dest_path] (os/shell (string "scp " origin " " dest_path))
_ (errorf "Unknown rule: %M" rule))))
(error "Build failed"))
(set step-counter (+ step-counter 1)))
(get build :value)))
(def end-time (os/time))
(let (total (- end-time start-time))
(printf "Build time: %d seconds" total))