2021-12-28 11:20:54 +01:00
|
|
|
# Logic for generating Buildkite pipelines from Nix build targets read
|
|
|
|
# by //nix/readTree.
|
|
|
|
#
|
|
|
|
# It outputs a "YAML" (actually JSON) file which is evaluated and
|
|
|
|
# submitted to Buildkite at the start of each build.
|
|
|
|
#
|
|
|
|
# The structure of the file that is being created is documented here:
|
|
|
|
# https://buildkite.com/docs/pipelines/defining-steps
|
2022-02-07 11:24:18 +01:00
|
|
|
{ depot, pkgs, ... }:
|
2021-12-28 11:20:54 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (builtins)
|
|
|
|
attrValues
|
2022-06-02 18:59:56 +02:00
|
|
|
concatLists
|
2021-12-28 11:20:54 +01:00
|
|
|
concatStringsSep
|
2022-06-02 19:42:56 +02:00
|
|
|
elem
|
2021-12-28 11:20:54 +01:00
|
|
|
foldl'
|
2022-01-16 13:55:36 +01:00
|
|
|
hasAttr
|
2022-01-19 19:18:52 +01:00
|
|
|
hashString
|
|
|
|
isNull
|
|
|
|
isString
|
2021-12-28 11:20:54 +01:00
|
|
|
length
|
2021-10-01 12:43:53 +02:00
|
|
|
listToAttrs
|
2021-12-28 11:20:54 +01:00
|
|
|
mapAttrs
|
2021-10-01 12:43:53 +02:00
|
|
|
toJSON
|
|
|
|
unsafeDiscardStringContext;
|
2021-12-28 11:20:54 +01:00
|
|
|
|
2022-09-26 19:33:05 +02:00
|
|
|
inherit (pkgs) lib runCommand writeText;
|
2022-02-07 11:24:18 +01:00
|
|
|
inherit (depot.nix.readTree) mkLabel;
|
2024-03-10 18:05:49 +01:00
|
|
|
|
|
|
|
inherit (depot.nix) dependency-analyzer;
|
2021-12-28 11:20:54 +01:00
|
|
|
in
|
|
|
|
rec {
|
2024-03-09 13:15:34 +01:00
|
|
|
# Create a unique key for the buildkite pipeline based on the given derivation
|
|
|
|
# or drvPath. A consequence of using such keys is that every derivation may
|
|
|
|
# only be exposed as a single, unique step in the pipeline.
|
|
|
|
keyForDrv = drvOrPath:
|
|
|
|
let
|
|
|
|
drvPath =
|
|
|
|
if lib.isDerivation drvOrPath then drvOrPath.drvPath
|
|
|
|
else if lib.isString drvOrPath then drvOrPath
|
|
|
|
else builtins.throw "keyForDrv: expected string or derivation";
|
|
|
|
|
|
|
|
# Only use the drv hash to prevent escaping problems. Buildkite also has a
|
|
|
|
# limit of 100 characters on keys.
|
|
|
|
in
|
|
|
|
"drv-" + (builtins.substring 0 32
|
|
|
|
(builtins.baseNameOf (unsafeDiscardStringContext drvPath))
|
|
|
|
);
|
|
|
|
|
2024-02-13 17:36:26 +01:00
|
|
|
# Given an arbitrary attribute path generate a Nix expression which obtains
|
|
|
|
# this from the root of depot (assumed to be ./.). Attributes may be any
|
|
|
|
# Nix strings suitable as attribute names, not just Nix literal-safe strings.
|
|
|
|
mkBuildExpr = attrPath:
|
2021-12-28 11:20:54 +01:00
|
|
|
let
|
|
|
|
descend = expr: attr: "builtins.getAttr \"${attr}\" (${expr})";
|
|
|
|
in
|
2024-02-13 17:36:26 +01:00
|
|
|
foldl' descend "import ./. {}" attrPath;
|
2021-12-28 11:20:54 +01:00
|
|
|
|
2022-01-16 13:55:36 +01:00
|
|
|
# Determine whether to skip a target if it has not diverged from the
|
|
|
|
# HEAD branch.
|
2024-02-13 17:14:08 +01:00
|
|
|
shouldSkip = { parentTargetMap ? { }, label, drvPath }:
|
2022-01-16 13:55:36 +01:00
|
|
|
if (hasAttr label parentTargetMap) && parentTargetMap."${label}".drvPath == drvPath
|
|
|
|
then "Target has not changed."
|
|
|
|
else false;
|
2021-12-28 11:20:54 +01:00
|
|
|
|
2024-02-13 17:36:26 +01:00
|
|
|
# Create build command for an attribute path pointing to a derivation.
|
2024-02-13 18:35:38 +01:00
|
|
|
mkBuildCommand = { attrPath, drvPath, outLink ? "result" }: concatStringsSep " " [
|
2024-03-31 21:03:05 +02:00
|
|
|
# If the nix build fails, the Nix command's exit status should be used.
|
|
|
|
"set -o pipefail;"
|
|
|
|
|
2022-01-19 19:18:52 +01:00
|
|
|
# First try to realise the drvPath of the target so we don't evaluate twice.
|
|
|
|
# Nix has no concept of depending on a derivation file without depending on
|
|
|
|
# at least one of its `outPath`s, so we need to discard the string context
|
|
|
|
# if we don't want to build everything during pipeline construction.
|
2024-02-13 18:35:38 +01:00
|
|
|
#
|
|
|
|
# To make this more uniform with how nix-build(1) works, we call realpath(1)
|
|
|
|
# on nix-store(1)'s output since it has the habit of printing the path of the
|
|
|
|
# out link, not the store path.
|
2024-03-31 21:03:05 +02:00
|
|
|
"(nix-store --realise '${drvPath}' --add-root '${outLink}' --indirect | xargs -r realpath)"
|
2022-01-19 19:18:52 +01:00
|
|
|
|
|
|
|
# Since we don't gcroot the derivation files, they may be deleted by the
|
|
|
|
# garbage collector. In that case we can reevaluate and build the attribute
|
|
|
|
# using nix-build.
|
2024-02-13 18:35:38 +01:00
|
|
|
"|| (test ! -f '${drvPath}' && nix-build -E '${mkBuildExpr attrPath}' --show-trace --out-link '${outLink}')"
|
2022-01-19 19:18:52 +01:00
|
|
|
];
|
|
|
|
|
2024-02-13 18:00:43 +01:00
|
|
|
# Attribute path of a target relative to the depot root. Needs to take into
|
|
|
|
# account whether the target is a physical target (which corresponds to a path
|
|
|
|
# in the filesystem) or the subtarget of a physical target.
|
|
|
|
targetAttrPath = target:
|
|
|
|
target.__readTree
|
|
|
|
++ lib.optionals (target ? __subtarget) [ target.__subtarget ];
|
|
|
|
|
2024-03-10 18:05:49 +01:00
|
|
|
# Given a derivation (identified by drvPath) that is part of the list of
|
|
|
|
# targets passed to mkPipeline, determine all derivations that it depends on
|
|
|
|
# and are also part of the pipeline. Finally, return the keys of the steps
|
|
|
|
# that build them. This is used to populate `depends_on` in `mkStep`.
|
|
|
|
#
|
|
|
|
# See //nix/dependency-analyzer for documentation on the structure of `targetDepMap`.
|
|
|
|
getTargetPipelineDeps = targetDepMap: drvPath:
|
|
|
|
# Sanity check: We should only call this function on targets explicitly
|
|
|
|
# passed to mkPipeline. Thus it should have been passed as a “known” drv to
|
|
|
|
# dependency-analyzer.
|
|
|
|
assert targetDepMap.${drvPath}.known;
|
|
|
|
builtins.map keyForDrv targetDepMap.${drvPath}.knownDeps;
|
|
|
|
|
2021-12-28 11:20:54 +01:00
|
|
|
# Create a pipeline step from a single target.
|
2024-03-10 18:05:49 +01:00
|
|
|
mkStep = { headBranch, parentTargetMap, targetDepMap, target, cancelOnBuildFailing }:
|
2022-01-16 13:55:36 +01:00
|
|
|
let
|
|
|
|
label = mkLabel target;
|
|
|
|
drvPath = unsafeDiscardStringContext target.drvPath;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
label = ":nix: " + label;
|
2024-03-09 13:15:34 +01:00
|
|
|
key = keyForDrv target;
|
2024-02-13 17:14:08 +01:00
|
|
|
skip = shouldSkip { inherit label drvPath parentTargetMap; };
|
2024-02-13 17:36:26 +01:00
|
|
|
command = mkBuildCommand {
|
2024-02-13 18:00:43 +01:00
|
|
|
attrPath = targetAttrPath target;
|
2024-02-13 17:36:26 +01:00
|
|
|
inherit drvPath;
|
|
|
|
};
|
2022-01-19 19:18:52 +01:00
|
|
|
env.READTREE_TARGET = label;
|
2022-09-01 10:10:48 +02:00
|
|
|
cancel_on_build_failing = cancelOnBuildFailing;
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-28 11:20:54 +01:00
|
|
|
# Add a dependency on the initial static pipeline step which
|
|
|
|
# always runs. This allows build steps uploaded in batches to
|
|
|
|
# start running before all batches have been uploaded.
|
2024-03-10 18:05:49 +01:00
|
|
|
depends_on = [ ":init:" ]
|
|
|
|
++ getTargetPipelineDeps targetDepMap drvPath
|
|
|
|
++ lib.optionals (target ? meta.ci.buildkiteExtraDeps) target.meta.ci.buildkiteExtraDeps;
|
2023-07-14 18:16:02 +02:00
|
|
|
} // lib.optionalAttrs (target ? meta.timeout) {
|
|
|
|
timeout_in_minutes = target.meta.timeout / 60;
|
2023-07-24 10:55:57 +02:00
|
|
|
# Additional arguments to set on the step.
|
|
|
|
# Keep in mind these *overwrite* existing step args, not extend. Use with caution.
|
|
|
|
} // lib.optionalAttrs (target ? meta.ci.buildkiteExtraStepArgs) target.meta.ci.buildkiteExtraStepArgs;
|
2021-12-28 11:20:54 +01:00
|
|
|
|
|
|
|
# Helper function to inelegantly divide a list into chunks of at
|
|
|
|
# most n elements.
|
|
|
|
#
|
|
|
|
# This works by assigning each element a chunk ID based on its
|
|
|
|
# index, and then grouping all elements by their chunk ID.
|
|
|
|
chunksOf = n: list:
|
|
|
|
let
|
|
|
|
chunkId = idx: toString (idx / n + 1);
|
|
|
|
assigned = lib.imap1 (idx: value: { inherit value; chunk = chunkId idx; }) list;
|
|
|
|
unchunk = mapAttrs (_: elements: map (e: e.value) elements);
|
|
|
|
in
|
|
|
|
unchunk (lib.groupBy (e: e.chunk) assigned);
|
|
|
|
|
|
|
|
# Define a build pipeline chunk as a JSON file, using the pipeline
|
|
|
|
# format documented on
|
|
|
|
# https://buildkite.com/docs/pipelines/defining-steps.
|
2022-01-22 12:32:19 +01:00
|
|
|
makePipelineChunk = name: chunkId: chunk: rec {
|
|
|
|
filename = "${name}-chunk-${chunkId}.json";
|
2021-12-28 11:20:54 +01:00
|
|
|
path = writeText filename (toJSON {
|
|
|
|
steps = chunk;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-02 18:59:14 +02:00
|
|
|
# Split the pipeline into chunks of at most 192 steps at once, which
|
2021-12-28 11:20:54 +01:00
|
|
|
# are uploaded sequentially. This is because of a limitation in the
|
|
|
|
# Buildkite backend which struggles to process more than a specific
|
|
|
|
# number of chunks at once.
|
2022-01-22 12:32:19 +01:00
|
|
|
pipelineChunks = name: steps:
|
2022-01-29 01:10:37 +01:00
|
|
|
attrValues (mapAttrs (makePipelineChunk name) (chunksOf 192 steps));
|
2021-12-28 11:20:54 +01:00
|
|
|
|
|
|
|
# Create a pipeline structure for the given targets.
|
|
|
|
mkPipeline =
|
|
|
|
{
|
|
|
|
# HEAD branch of the repository on which release steps, GC
|
|
|
|
# anchoring and other "mainline only" steps should run.
|
|
|
|
headBranch
|
|
|
|
, # List of derivations as read by readTree (in most cases just the
|
|
|
|
# output of readTree.gather) that should be built in Buildkite.
|
|
|
|
#
|
|
|
|
# These are scheduled as the first build steps and run as fast as
|
|
|
|
# possible, in order, without any concurrency restrictions.
|
|
|
|
drvTargets
|
2022-01-16 13:55:36 +01:00
|
|
|
, # Derivation map of a parent commit. Only targets which no longer
|
|
|
|
# correspond to the content of this map will be built. Passing an
|
|
|
|
# empty map will always build all targets.
|
|
|
|
parentTargetMap ? { }
|
2021-12-28 11:20:54 +01:00
|
|
|
, # A list of plain Buildkite step structures to run alongside the
|
|
|
|
# build for all drvTargets, but before proceeding with any
|
|
|
|
# post-build actions such as status reporting.
|
|
|
|
#
|
|
|
|
# Can be used for things like code formatting checks.
|
|
|
|
additionalSteps ? [ ]
|
|
|
|
, # A list of plain Buildkite step structures to run after all
|
|
|
|
# previous steps succeeded.
|
|
|
|
#
|
|
|
|
# Can be used for status reporting steps and the like.
|
|
|
|
postBuildSteps ? [ ]
|
2022-10-10 13:20:56 +02:00
|
|
|
# The list of phases known by the current Buildkite
|
|
|
|
# pipeline. Dynamic pipeline chunks for each phase are uploaded
|
|
|
|
# to Buildkite on execution of static part of the
|
|
|
|
# pipeline. Phases selection is hard-coded in the static
|
|
|
|
# pipeline.
|
|
|
|
#
|
|
|
|
# Pipeline generation will fail when an extra step with
|
|
|
|
# unregistered phase is added.
|
|
|
|
#
|
|
|
|
# Common scenarios for different phase:
|
|
|
|
# - "build" - main phase for building all Nix targets
|
|
|
|
# - "release" - pushing artifacts to external repositories
|
|
|
|
# - "deploy" - updating external deployment configurations
|
|
|
|
, phases ? [ "build" "release" ]
|
|
|
|
# Build phases that are active for this invocation (i.e. their
|
2022-06-02 19:45:45 +02:00
|
|
|
# steps should be generated).
|
|
|
|
#
|
|
|
|
# This can be used to disable outputting parts of a pipeline if,
|
|
|
|
# for example, build and release phases are created in separate
|
|
|
|
# eval contexts.
|
|
|
|
#
|
|
|
|
# TODO(tazjin): Fail/warn if unknown phase is requested.
|
2022-10-10 13:20:56 +02:00
|
|
|
, activePhases ? phases
|
2022-09-01 10:10:48 +02:00
|
|
|
# Setting this attribute to true cancels dynamic pipeline steps
|
|
|
|
# as soon as the build is marked as failing.
|
|
|
|
#
|
|
|
|
# To enable this feature one should enable "Fail Fast" setting
|
|
|
|
# at Buildkite pipeline or on organization level.
|
|
|
|
, cancelOnBuildFailing ? false
|
2021-12-28 11:20:54 +01:00
|
|
|
}:
|
|
|
|
let
|
2022-06-02 19:45:45 +02:00
|
|
|
# List of phases to include.
|
2022-10-10 13:20:56 +02:00
|
|
|
enabledPhases = lib.intersectLists activePhases phases;
|
2022-06-02 18:59:56 +02:00
|
|
|
|
2022-06-02 19:50:01 +02:00
|
|
|
# Is the 'build' phase included? This phase is treated specially
|
|
|
|
# because it always contains the plain Nix builds, and some
|
|
|
|
# logic/optimisation depends on knowing whether is executing.
|
2022-10-10 13:20:56 +02:00
|
|
|
buildEnabled = elem "build" enabledPhases;
|
2022-06-02 19:50:01 +02:00
|
|
|
|
2024-03-10 18:05:49 +01:00
|
|
|
# Dependency relations between the `drvTargets`. See also //nix/dependency-analyzer.
|
|
|
|
targetDepMap = dependency-analyzer (dependency-analyzer.drvsToPaths drvTargets);
|
|
|
|
|
2022-06-02 18:59:56 +02:00
|
|
|
# Convert a target into all of its steps, separated by build
|
|
|
|
# phase (as phases end up in different chunks).
|
2022-01-19 19:18:52 +01:00
|
|
|
targetToSteps = target:
|
|
|
|
let
|
2024-02-13 17:14:08 +01:00
|
|
|
mkStepArgs = {
|
2024-03-10 18:05:49 +01:00
|
|
|
inherit headBranch parentTargetMap targetDepMap target cancelOnBuildFailing;
|
2024-02-13 17:14:08 +01:00
|
|
|
};
|
|
|
|
step = mkStep mkStepArgs;
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2022-02-02 10:22:58 +01:00
|
|
|
# Same step, but with an override function applied. This is
|
|
|
|
# used in mkExtraStep if the extra step needs to modify the
|
|
|
|
# parent derivation somehow.
|
|
|
|
#
|
|
|
|
# Note that this will never affect the label.
|
2024-02-13 17:14:08 +01:00
|
|
|
overridable = f: mkStep (mkStepArgs // { target = (f target); });
|
2022-02-02 10:22:58 +01:00
|
|
|
|
2022-06-02 18:59:56 +02:00
|
|
|
# Split extra steps by phase.
|
|
|
|
splitExtraSteps = lib.groupBy ({ phase, ... }: phase)
|
2022-10-10 13:20:56 +02:00
|
|
|
(attrValues (mapAttrs (normaliseExtraStep phases overridable)
|
2022-01-20 19:20:56 +01:00
|
|
|
(target.meta.ci.extraSteps or { })));
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2022-06-02 19:50:01 +02:00
|
|
|
extraSteps = mapAttrs
|
|
|
|
(_: steps:
|
2024-02-13 18:35:38 +01:00
|
|
|
map (mkExtraStep (targetAttrPath target) buildEnabled) steps)
|
2022-06-02 19:50:01 +02:00
|
|
|
splitExtraSteps;
|
2022-01-20 19:20:56 +01:00
|
|
|
in
|
2022-06-02 19:51:58 +02:00
|
|
|
if !buildEnabled then extraSteps
|
|
|
|
else extraSteps // {
|
2022-06-02 18:59:56 +02:00
|
|
|
build = [ step ] ++ (extraSteps.build or [ ]);
|
2022-01-20 19:20:56 +01:00
|
|
|
};
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2022-06-02 18:59:56 +02:00
|
|
|
# Combine all target steps into step lists per phase.
|
|
|
|
#
|
|
|
|
# TODO(tazjin): Refactor when configurable phases show up.
|
|
|
|
globalSteps = {
|
|
|
|
build = additionalSteps;
|
|
|
|
release = postBuildSteps;
|
|
|
|
};
|
|
|
|
|
2022-10-10 13:20:56 +02:00
|
|
|
phasesWithSteps = lib.zipAttrsWithNames enabledPhases (_: concatLists)
|
2022-06-02 18:59:56 +02:00
|
|
|
((map targetToSteps drvTargets) ++ [ globalSteps ]);
|
|
|
|
|
|
|
|
# Generate pipeline chunks for each phase.
|
|
|
|
chunks = foldl'
|
|
|
|
(acc: phase:
|
|
|
|
let phaseSteps = phasesWithSteps.${phase} or [ ]; in
|
|
|
|
if phaseSteps == [ ]
|
|
|
|
then acc
|
|
|
|
else acc ++ (pipelineChunks phase phaseSteps))
|
|
|
|
[ ]
|
2022-10-10 13:20:56 +02:00
|
|
|
enabledPhases;
|
2022-06-02 18:59:56 +02:00
|
|
|
|
2021-12-28 11:20:54 +01:00
|
|
|
in
|
2022-09-26 19:33:05 +02:00
|
|
|
runCommand "buildkite-pipeline" { } ''
|
2021-12-28 11:20:54 +01:00
|
|
|
mkdir $out
|
|
|
|
echo "Generated ${toString (length chunks)} pipeline chunks"
|
|
|
|
${
|
|
|
|
lib.concatMapStringsSep "\n"
|
|
|
|
(chunk: "cp ${chunk.path} $out/${chunk.filename}") chunks
|
|
|
|
}
|
|
|
|
'';
|
2021-10-01 12:43:53 +02:00
|
|
|
|
|
|
|
# Create a drvmap structure for the given targets, containing the
|
|
|
|
# mapping of all target paths to their derivations. The mapping can
|
|
|
|
# be persisted for future use.
|
|
|
|
mkDrvmap = drvTargets: writeText "drvmap.json" (toJSON (listToAttrs (map
|
|
|
|
(target: {
|
|
|
|
name = mkLabel target;
|
|
|
|
value = {
|
|
|
|
drvPath = unsafeDiscardStringContext target.drvPath;
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-10-01 12:43:53 +02:00
|
|
|
# Include the attrPath in the output to reconstruct the drv
|
|
|
|
# without parsing the human-readable label.
|
2024-02-13 18:00:43 +01:00
|
|
|
attrPath = targetAttrPath target;
|
2021-10-01 12:43:53 +02:00
|
|
|
};
|
|
|
|
})
|
|
|
|
drvTargets)));
|
2022-01-19 19:18:52 +01:00
|
|
|
|
|
|
|
# Implementation of extra step logic.
|
|
|
|
#
|
|
|
|
# Each target extra step is an attribute specified in
|
|
|
|
# `meta.ci.extraSteps`. Its attribute name will be used as the step
|
|
|
|
# name on Buildkite.
|
|
|
|
#
|
|
|
|
# command (required): A command that will be run in the depot
|
|
|
|
# checkout when this step is executed. Should be a derivation
|
|
|
|
# resulting in a single executable file, e.g. through
|
|
|
|
# pkgs.writeShellScript.
|
|
|
|
#
|
|
|
|
# label (optional): Human-readable label for this step to display
|
|
|
|
# in the Buildkite UI instead of the attribute name.
|
|
|
|
#
|
2022-01-20 19:20:56 +01:00
|
|
|
# prompt (optional): Setting this blocks the step until confirmed
|
|
|
|
# by a human. Should be a string which is displayed for
|
|
|
|
# confirmation. These steps always run after the main build is
|
|
|
|
# done and have no influence on CI status.
|
|
|
|
#
|
2022-01-19 19:18:52 +01:00
|
|
|
# needsOutput (optional): If set to true, the parent derivation
|
|
|
|
# will be built in the working directory before running the
|
|
|
|
# command. Output will be available as 'result'.
|
|
|
|
# TODO: Figure out multiple-output derivations.
|
|
|
|
#
|
2022-02-02 10:22:58 +01:00
|
|
|
# parentOverride (optional): A function (drv -> drv) to override
|
|
|
|
# the parent's target definition when preparing its output. Only
|
|
|
|
# used in extra steps that use needsOutput.
|
|
|
|
#
|
2022-01-22 14:04:39 +01:00
|
|
|
# branches (optional): Git references (branches, tags ... ) on
|
|
|
|
# which this step should be allowed to run. List of strings.
|
2022-01-19 19:18:52 +01:00
|
|
|
#
|
|
|
|
# alwaysRun (optional): If set to true, this step will always run,
|
|
|
|
# even if its parent has not been rebuilt.
|
2022-01-20 19:20:56 +01:00
|
|
|
#
|
|
|
|
# Note that gated steps are independent of each other.
|
|
|
|
|
|
|
|
# Create a gated step in a step group, independent from any other
|
|
|
|
# steps.
|
2022-01-22 14:04:39 +01:00
|
|
|
mkGatedStep = { step, label, parent, prompt }: {
|
2022-02-24 17:33:13 +01:00
|
|
|
inherit (step) depends_on;
|
2022-01-20 19:20:56 +01:00
|
|
|
group = label;
|
|
|
|
skip = parent.skip or false;
|
|
|
|
|
|
|
|
steps = [
|
|
|
|
{
|
|
|
|
inherit prompt;
|
2022-10-20 16:20:41 +02:00
|
|
|
branches = step.branches or [ ];
|
2022-01-20 19:20:56 +01:00
|
|
|
block = ":radio_button: Run ${label}? (from ${parent.env.READTREE_TARGET})";
|
|
|
|
}
|
2022-01-19 19:18:52 +01:00
|
|
|
|
2022-01-20 19:20:56 +01:00
|
|
|
# The explicit depends_on of the wrapped step must be removed,
|
|
|
|
# otherwise its dependency relationship with the gate step will
|
|
|
|
# break.
|
|
|
|
(builtins.removeAttrs step [ "depends_on" ])
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2022-06-02 18:59:56 +02:00
|
|
|
# Validate and normalise extra step configuration before actually
|
|
|
|
# generating build steps, in order to use user-provided metadata
|
|
|
|
# during the pipeline generation.
|
2022-10-10 13:20:56 +02:00
|
|
|
normaliseExtraStep = phases: overridableParent: key:
|
2022-02-02 10:22:58 +01:00
|
|
|
{ command
|
|
|
|
, label ? key
|
|
|
|
, needsOutput ? false
|
|
|
|
, parentOverride ? (x: x)
|
|
|
|
, branches ? null
|
|
|
|
, alwaysRun ? false
|
2022-06-02 19:26:18 +02:00
|
|
|
, prompt ? false
|
2022-10-24 16:16:40 +02:00
|
|
|
, softFail ? false
|
2023-07-14 17:53:55 +02:00
|
|
|
, phase ? "build"
|
2022-07-22 12:28:56 +02:00
|
|
|
, skip ? false
|
2022-07-23 00:47:21 +02:00
|
|
|
, agents ? null
|
2022-06-02 18:59:56 +02:00
|
|
|
}:
|
2022-01-19 19:18:52 +01:00
|
|
|
let
|
2022-02-02 10:22:58 +01:00
|
|
|
parent = overridableParent parentOverride;
|
2022-01-19 19:18:52 +01:00
|
|
|
parentLabel = parent.env.READTREE_TARGET;
|
2022-06-02 19:42:56 +02:00
|
|
|
|
2022-10-10 13:20:56 +02:00
|
|
|
validPhase = lib.throwIfNot (elem phase phases) ''
|
2022-06-02 19:42:56 +02:00
|
|
|
In step '${label}' (from ${parentLabel}):
|
|
|
|
|
|
|
|
Phase '${phase}' is not valid.
|
|
|
|
|
2022-10-10 13:20:56 +02:00
|
|
|
Known phases: ${concatStringsSep ", " phases}
|
2022-06-02 19:42:56 +02:00
|
|
|
''
|
|
|
|
phase;
|
2022-06-02 18:59:56 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit
|
|
|
|
alwaysRun
|
|
|
|
branches
|
|
|
|
command
|
|
|
|
key
|
|
|
|
label
|
|
|
|
needsOutput
|
|
|
|
parent
|
2022-07-22 12:28:56 +02:00
|
|
|
parentLabel
|
2022-10-24 16:16:40 +02:00
|
|
|
softFail
|
2022-07-23 00:47:21 +02:00
|
|
|
skip
|
|
|
|
agents;
|
2022-06-02 18:59:56 +02:00
|
|
|
|
2023-07-14 17:53:55 +02:00
|
|
|
phase = validPhase;
|
2022-06-02 19:26:18 +02:00
|
|
|
|
|
|
|
prompt = lib.throwIf (prompt != false && phase == "build") ''
|
|
|
|
In step '${label}' (from ${parentLabel}):
|
|
|
|
|
2024-04-22 15:08:11 +02:00
|
|
|
The 'prompt' feature can not be used by steps in the "build"
|
2022-06-02 19:26:18 +02:00
|
|
|
phase, because CI builds should not be gated on manual human
|
|
|
|
approvals.
|
|
|
|
''
|
|
|
|
prompt;
|
2022-06-02 18:59:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
# Create the Buildkite configuration for an extra step, optionally
|
|
|
|
# wrapping it in a gate group.
|
2024-02-13 18:35:38 +01:00
|
|
|
mkExtraStep = parentAttrPath: buildEnabled: cfg:
|
2022-06-02 18:59:56 +02:00
|
|
|
let
|
2024-02-13 18:35:38 +01:00
|
|
|
# ATTN: needs to match an entry in .gitignore so that the tree won't get dirty
|
|
|
|
commandScriptLink = "nix-buildkite-extra-step-command-script";
|
|
|
|
|
2022-01-19 19:18:52 +01:00
|
|
|
step = {
|
2024-03-09 13:15:34 +01:00
|
|
|
key = "extra-step-" + hashString "sha1" "${cfg.label}-${cfg.parentLabel}";
|
2022-06-02 18:59:56 +02:00
|
|
|
label = ":gear: ${cfg.label} (from ${cfg.parentLabel})";
|
2022-09-30 14:21:11 +02:00
|
|
|
skip =
|
|
|
|
let
|
|
|
|
# When parent doesn't have skip attribute set, default to false
|
|
|
|
parentSkip = cfg.parent.skip or false;
|
|
|
|
# Extra step skip parameter can be string explaining the
|
|
|
|
# skip reason.
|
|
|
|
extraStepSkip = if builtins.isString cfg.skip then true else cfg.skip;
|
|
|
|
# Don't run if extra step is explicitly set to skip. If
|
|
|
|
# parameter is not set or equal to false, follow parent behavior.
|
|
|
|
skip' = if extraStepSkip then cfg.skip else parentSkip;
|
|
|
|
in
|
|
|
|
if cfg.alwaysRun then false else skip';
|
2022-06-02 19:50:01 +02:00
|
|
|
|
|
|
|
depends_on = lib.optional
|
|
|
|
(buildEnabled && !cfg.alwaysRun && !cfg.needsOutput)
|
|
|
|
cfg.parent.key;
|
|
|
|
|
2022-06-02 18:59:56 +02:00
|
|
|
command = pkgs.writeShellScript "${cfg.key}-script" ''
|
2022-01-19 19:18:52 +01:00
|
|
|
set -ueo pipefail
|
2022-06-02 18:59:56 +02:00
|
|
|
${lib.optionalString cfg.needsOutput
|
|
|
|
"echo '~~~ Preparing build output of ${cfg.parentLabel}'"
|
|
|
|
}
|
|
|
|
${lib.optionalString cfg.needsOutput cfg.parent.command}
|
2024-02-13 18:35:38 +01:00
|
|
|
echo '--- Building extra step script'
|
|
|
|
command_script="$(${
|
|
|
|
# Using command substitution in this way assumes the script drv only has one output
|
|
|
|
assert builtins.length cfg.command.outputs == 1;
|
|
|
|
mkBuildCommand {
|
|
|
|
# script is exposed at <parent>.meta.ci.extraSteps.<key>.command
|
|
|
|
attrPath =
|
|
|
|
parentAttrPath
|
|
|
|
++ [ "meta" "ci" "extraSteps" cfg.key "command" ];
|
|
|
|
drvPath = unsafeDiscardStringContext cfg.command.drvPath;
|
|
|
|
# make sure it doesn't conflict with result (from needsOutput)
|
|
|
|
outLink = commandScriptLink;
|
|
|
|
}
|
|
|
|
})"
|
|
|
|
echo '+++ Running extra step script'
|
|
|
|
exec "$command_script"
|
2022-01-19 19:18:52 +01:00
|
|
|
'';
|
2022-10-24 16:16:40 +02:00
|
|
|
|
|
|
|
soft_fail = cfg.softFail;
|
2022-07-25 10:15:30 +02:00
|
|
|
} // (lib.optionalAttrs (cfg.agents != null) { inherit (cfg) agents; })
|
|
|
|
// (lib.optionalAttrs (cfg.branches != null) {
|
|
|
|
branches = lib.concatStringsSep " " cfg.branches;
|
|
|
|
});
|
2022-01-20 19:20:56 +01:00
|
|
|
in
|
2022-06-02 18:59:56 +02:00
|
|
|
if (isString cfg.prompt)
|
2022-01-20 19:20:56 +01:00
|
|
|
then
|
|
|
|
mkGatedStep
|
|
|
|
{
|
2022-06-02 18:59:56 +02:00
|
|
|
inherit step;
|
|
|
|
inherit (cfg) label parent prompt;
|
2022-01-20 19:20:56 +01:00
|
|
|
}
|
|
|
|
else step;
|
2021-12-28 11:20:54 +01:00
|
|
|
}
|