From 7e1cdd9cf8586c8fcd888ea118a44822e29579f1 Mon Sep 17 00:00:00 2001 From: Drew Hess Date: Sun, 24 Sep 2023 18:25:48 +0100 Subject: [PATCH] feat: add support for Buildkite agent-stack-k8s pipelines This mode is set by setting the environment variable `PIPELINE_TYPE` to `kubernetes`. The container image used in the pipeline is set by the `KUBERNETES_CONTAINER_IMAGE` environment variable, and is `nixos/nix:latest` by default. Signed-off-by: Drew Hess --- exe-nix-buildkite/Main.hs | 50 ++++++++++++++++++++++++++++++++++++-- nix-buildkite-plugin.cabal | 2 +- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/exe-nix-buildkite/Main.hs b/exe-nix-buildkite/Main.hs index 90f6563..39367d4 100644 --- a/exe-nix-buildkite/Main.hs +++ b/exe-nix-buildkite/Main.hs @@ -45,10 +45,26 @@ import Data.Text (Text, isPrefixOf, pack, unpack) import qualified Data.Text as T import Data.Text.IO (readFile) +data PipelineType = Nix | Kubernetes + main :: IO () main = do jobsExpr <- fromMaybe "./jobs.nix" . listToMaybe <$> getArgs + pipelineType <- do + pt <- lookupEnv "PIPELINE_TYPE" + case pt of + Nothing -> return Nix + Just "kubernetes" -> return Kubernetes + Just "nix" -> return Nix + Just invalid -> error $ "Invalid PIPELINE_TYPE" <> invalid + + containerImage <- do + image <- lookupEnv "KUBERNETES_CONTAINER_IMAGE" + case image of + Nothing -> return "nixos/nix:latest" + Just image' -> return $ pack image' + postBuildHook <- do cmd <- lookupEnv "POST_BUILD_HOOK" case cmd of @@ -106,7 +122,37 @@ main = do g <- foldr (\(_, drv) m -> m >>= \g -> add g drv) (pure empty) drvs - let steps = map (uncurry step) drvs + let steps Kubernetes = map (uncurry step) drvs + where + step label drvPath = + ( label + , object + [ "label" .= unpack label + , "plugins" + .= [ object + [ "kubernetes" + .= [ object + [ "podSpec" + .= object + [ "containers" + .= [ object + [ "image" .= containerImage + , "command" .= String "nix-store" + , "args" .= (postBuildHook <> ["-r", drvPath]) + ] + ] + ] + ] + ] + ] + ] + , "key" .= stepify drvPath + , "depends_on" .= dependencies + ] + ) + where + dependencies = map stepify $ filter (`elem` map snd drvs) $ drop 1 $ reachable drvPath g + steps Nix = map (uncurry step) drvs where step label drvPath = ( label @@ -124,7 +170,7 @@ main = do encode $ object [ "agents" .= agentTags - , "steps" .= map snd (sortOn fst steps) + , "steps" .= map snd (sortOn fst $ steps pipelineType) ] -- Transform nix platforms into buildkite emoji diff --git a/nix-buildkite-plugin.cabal b/nix-buildkite-plugin.cabal index 06aa816..6a13b99 100644 --- a/nix-buildkite-plugin.cabal +++ b/nix-buildkite-plugin.cabal @@ -1,6 +1,6 @@ cabal-version: 3.0 name: nix-buildkite-plugin -version: 0.2.0.0 +version: 0.3.0.0 license-file: LICENSE author: Hackworth Ltd maintainer: src@hackworthltd.com