Skip to content

Commit 397f2d9

Browse files
committed
Add github action
1 parent f3645b5 commit 397f2d9

File tree

5 files changed

+186
-115
lines changed

5 files changed

+186
-115
lines changed

action.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: nixidy action
2+
description: Generate manifest for a nixidy environment
3+
4+
inputs:
5+
environment:
6+
description: Name of the environment to build
7+
required: true
8+
cwd:
9+
description: Path to directory containing the checked out repository
10+
default: '.'
11+
12+
runs:
13+
using: composite
14+
steps:
15+
- shell: bash
16+
env:
17+
FLAKE: github:${{github.action_repository}}/${{github.action_ref}}
18+
DEST_DIR: ${{runner.temp}}/${{inputs.environment}}
19+
run: |
20+
TARGET_BRANCH=$(nix run '${{env.FLAKE}}#' -- info '${{inputs.cwd}}#${{inputs.environment}}' --json | jq -r .branch)
21+
22+
if git -C "${{inputs.cwd}}" fetch origin "$TARGET_BRANCH"; then
23+
git -C "${{inputs.cwd}}" worktree add --checkout "${{env.DEST_DIR}}" "$TARGET_BRANCH"
24+
else
25+
git -C "${{inputs.cwd}}" worktree add --orphan -b "$TARGET_BRANCH" "${{env.DEST_DIR}}"
26+
fi
27+
28+
RESULT=$(nix run '${{env.FLAKE}}#' -- build '${{inputs.cwd}}#${{inputs.environment}}' --print-out-paths --no-link)
29+
30+
rsync --recursive --delete --exclude=.git -L "$RESULT/" "${{env.DEST_DIR}}"
31+
32+
echo "BRANCH=$TARGET_BRANCH" >> "$GITHUB_ENV"
33+
34+
- uses: EndBug/add-and-commit@v9
35+
env:
36+
DEST_DIR: ${{runner.temp}}/${{inputs.environment}}
37+
with:
38+
cwd: ${{env.DEST_DIR}}
39+
default_author: github_actions
40+
message: "chore(${{inputs.environment}}): promote to ${{inputs.environment}} ${{github.sha}}"
41+
fetch: false
42+
push: --set-upstream origin ${{env.BRANCH}}

modules/default.nix

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -18,112 +18,10 @@
1818
}
1919
// extraSpecialArgs;
2020
};
21-
22-
extras = pkgs.stdenv.mkDerivation {
23-
name = "nixidy-extras";
24-
25-
phases = ["installPhase"];
26-
27-
installPhase =
28-
''
29-
mkdir -p $out
30-
''
31-
+ (
32-
lib.concatStringsSep "\n" (lib.mapAttrsToList (n: f: ''
33-
mkdir -p $out/$(dirname ${f.path})
34-
cat <<EOF > $out/${f.path}
35-
${f.text}
36-
EOF
37-
'')
38-
module.config.nixidy.extraFiles)
39-
);
40-
};
41-
42-
apps =
43-
lib.mapAttrs (
44-
n: v: {
45-
name = n;
46-
path = v.output.path;
47-
resources = lib.flatten (
48-
lib.mapAttrsToList (
49-
group: groupData:
50-
lib.mapAttrsToList (
51-
kind: kindData:
52-
lib.mapAttrsToList (
53-
res: resData:
54-
resData
55-
// {
56-
apiVersion = group;
57-
kind = kind;
58-
metadata = {name = res;} // (resData.metadata or {});
59-
}
60-
)
61-
kindData
62-
)
63-
groupData
64-
)
65-
v.resources
66-
);
67-
}
68-
)
69-
module.config.applications;
70-
71-
mkApp = app: let
72-
resources =
73-
map (
74-
res: rec {
75-
filename = "${res.kind}-${builtins.replaceStrings ["."] ["-"] res.metadata.name}.yaml";
76-
manifest = let
77-
resource = builtins.toJSON res;
78-
in
79-
pkgs.stdenv.mkDerivation {
80-
inherit resource;
81-
82-
name = "nixidy-app-${app.name}-${filename}";
83-
84-
passAsFile = ["resource"];
85-
86-
phases = ["installPhase"];
87-
88-
installPhase = ''
89-
cat $resourcePath | ${pkgs.yq-go}/bin/yq -P > $out
90-
'';
91-
};
92-
}
93-
)
94-
app.resources;
95-
in
96-
pkgs.stdenv.mkDerivation {
97-
name = "nixidy-app-${app.name}";
98-
99-
phases = ["installPhase"];
100-
101-
installPhase =
102-
''
103-
mkdir -p $out
104-
''
105-
+ (lib.concatStringsSep "\n" (map (res: ''
106-
ln -s ${res.manifest} $out/${res.filename}
107-
'')
108-
resources));
109-
};
110-
111-
mkStage = apps: let
112-
appsJoined = pkgs.linkFarm "nixidy-stage-apps-joined" (lib.mapAttrsToList (_: app: {
113-
name = app.path;
114-
path = mkApp app;
115-
})
116-
apps);
117-
in
118-
pkgs.symlinkJoin {
119-
name = "nixidy-stage";
120-
paths = [appsJoined extras];
121-
};
12221
in {
123-
targetBranch = module.config.nixidy.target.branch;
12422
meta = {
12523
repository = module.config.nixidy.target.repository;
12624
branch = module.config.nixidy.target.branch;
12725
};
128-
result = mkStage apps;
26+
environmentPackage = module.config.nixidy.environmentPackage;
12927
}

modules/environment.nix

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
{
2+
lib,
3+
config,
4+
pkgs,
5+
...
6+
}: let
7+
envName = lib.replaceStrings ["/"] ["-"] config.nixidy.target.branch;
8+
9+
apps =
10+
lib.mapAttrs (
11+
n: v: {
12+
name = n;
13+
path = v.output.path;
14+
resources = lib.flatten (
15+
lib.mapAttrsToList (
16+
group: groupData:
17+
lib.mapAttrsToList (
18+
kind: kindData:
19+
lib.mapAttrsToList (
20+
res: resData:
21+
resData
22+
// {
23+
apiVersion = group;
24+
kind = kind;
25+
metadata = {name = res;} // (resData.metadata or {});
26+
}
27+
)
28+
kindData
29+
)
30+
groupData
31+
)
32+
v.resources
33+
);
34+
}
35+
)
36+
config.applications;
37+
38+
mkApp = app: let
39+
resources =
40+
map (
41+
res: rec {
42+
filename = "${res.kind}-${builtins.replaceStrings ["."] ["-"] res.metadata.name}.yaml";
43+
manifest = let
44+
resource = builtins.toJSON res;
45+
in
46+
pkgs.stdenv.mkDerivation {
47+
inherit resource;
48+
49+
name = "nixidy-app-${app.name}-${filename}";
50+
51+
passAsFile = ["resource"];
52+
53+
phases = ["installPhase"];
54+
55+
installPhase = ''
56+
cat $resourcePath | ${pkgs.yq-go}/bin/yq -P > $out
57+
'';
58+
};
59+
}
60+
)
61+
app.resources;
62+
in
63+
pkgs.stdenv.mkDerivation {
64+
name = "nixidy-app-${app.name}";
65+
66+
phases = ["installPhase"];
67+
68+
installPhase =
69+
''
70+
mkdir -p $out
71+
''
72+
+ (lib.concatStringsSep "\n" (map (res: ''
73+
ln -s ${res.manifest} $out/${res.filename}
74+
'')
75+
resources));
76+
};
77+
in {
78+
options = with lib; {
79+
nixidy.extrasPackage = mkOption {
80+
type = types.package;
81+
internal = true;
82+
description = "The package containing all the extra files for an environment.";
83+
};
84+
85+
nixidy.environmentPackage = mkOption {
86+
type = types.package;
87+
internal = true;
88+
description = "The package containing all the applications for an environment.";
89+
};
90+
};
91+
92+
config = {
93+
# Build all extra files into its own package
94+
nixidy.extrasPackage = pkgs.stdenv.mkDerivation {
95+
name = "nixidy-extras-${envName}";
96+
97+
phases = ["installPhase"];
98+
99+
installPhase =
100+
''
101+
mkdir -p $out
102+
''
103+
+ (lib.concatStringsSep "\n" (lib.mapAttrsToList (_: file: ''
104+
mkdir -p $out/$(dirname ${file.path})
105+
cat <<EOF > "$out/${file.path}"
106+
${file.text}
107+
EOF
108+
'')
109+
config.nixidy.extraFiles));
110+
};
111+
112+
# Build final environment into a package
113+
nixidy.environmentPackage = let
114+
joined = pkgs.linkFarm "nixidy-apps-joined-${envName}" (lib.mapAttrsToList (_: app: {
115+
name = app.path;
116+
path = mkApp app;
117+
})
118+
apps);
119+
in
120+
pkgs.symlinkJoin {
121+
name = "nixidy-environment-${envName}";
122+
paths = [
123+
joined
124+
config.nixidy.extrasPackage
125+
];
126+
};
127+
};
128+
}

modules/modules.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
22
./applications.nix
33
./nixidy.nix
4+
./environment.nix
45
]

nixidy/nixidy

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,29 @@ function doBuild() {
4747
exit 1
4848
fi
4949

50-
nix build "${FLAKE_ROOT}#nixidyEnvs.${NIX_SYSTEM}.${FLAKE_ENV}.result" "${BUILD_PARAMS[@]}"
50+
nix build "${FLAKE_ROOT}#nixidyEnvs.${NIX_SYSTEM}.${FLAKE_ENV}.environmentPackage" "${BUILD_PARAMS[@]}"
5151
}
5252

5353
function doHelp() {
5454
echo "Usage: $0 [OPTION] COMMAND"
5555
echo
5656
echo "Options"
5757
echo
58-
echo " --no-link Don't create a result symlink (only used in build)."
59-
echo " --out-link PATH Create a custom result symlink (only used in build)."
60-
echo " --json Output info in JSON format (only used in info)."
61-
echo " -h Print this help"
58+
echo " --no-link Don't create a result symlink (only used in build)."
59+
echo " --out-link PATH Create a custom result symlink (only used in build)."
60+
echo " --print-out-paths Print the resulting output paths (only used in build)."
61+
echo " --json Output info in JSON format (only used in info)."
62+
echo " -h Print this help"
6263
echo
6364
echo "Commands"
6465
echo
65-
echo " help Print this help."
66+
echo " help Print this help."
6667
echo
67-
echo " info FLAKE_URI"
68-
echo " Get info about environment."
69-
echo " Example: .#prod"
68+
echo " info FLAKE_URI Get info about environment."
69+
echo " Example: .#prod"
7070
echo
71-
echo " build FLAKE_URI"
72-
echo " Build nixidy environment from flake URI."
73-
echo " Example: .#prod"
71+
echo " build FLAKE_URI Build nixidy environment from flake URI."
72+
echo " Example: .#prod"
7473
}
7574

7675
COMMAND=""
@@ -89,6 +88,9 @@ while [[ $# -gt 0 ]]; do
8988
--no-link)
9089
BUILD_PARAMS+=("--no-link")
9190
;;
91+
--print-out-paths)
92+
BUILD_PARAMS+=("--print-out-paths")
93+
;;
9294
--out-link)
9395
BUILD_PARAMS+=("--out-link" "$1")
9496
shift

0 commit comments

Comments
 (0)