Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Buildkite agent-stack-k8s pipelines #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions exe-nix-buildkite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nix-buildkite-plugin.cabal
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
maintainer: [email protected]
Expand Down