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

wip: initial layout #324

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions src/std/templates/microservice-aws-github/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
outputs = {std, ...} @ inputs:
std.growOn {
inherit inputs;
cellsFrom = ./ops;
cellBlocks = with std.blockTypes; [
# Software Delivery Lifecycle (Packaging Layers)
# For deeper context, please consult:
# https://std.divnix.com/patterns/four-packaging-layers.html
(installables "packages" {ci.build = true;})
(runnables "operables")
(containers "oci-images" {ci.publish = true;})
(kubectl "deployments" {ci.apply = true;})
# For rendering the Github Action CI/CD
(nixago "action")
];
};

inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";

inputs = {
std.url = "github:divnix/std";
std.inputs.nixpkgs.follows = "nixpkgs";
std.inputs.n2c.follows = "n2c";
std.inputs.nixago.follows = "nixago";
n2c.url = "github:nlewo/nix2container";
n2c.inputs.nixpkgs.follows = "nixpkgs";
nixago.url = "github:nix-community/nixago";
nixago.inputs.nixpkgs.follows = "nixpkgs";
nixago.inputs.nixago-exts.follows = "";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let
inherit (inputs.std) dmerge;
inherit (inputs.std.inputs) haumea;
inherit (inputs.std.lib.ops) readYAML;

loadYaml = _: _: readYAML;
baseline = with haumea.lib;
load {
src = ./deployments;
loader = [(matchers.regex ''^.+\.(yaml|yml)'' loadYaml)];
};
in {
my-srv-a = dmerge baseline.my-srv-a {
meta.description = "Development Environment for my-srv-a";
deployment = {
spec.template.spec.containers = dmerge.updateOn "name" [
{
name = "my-srv-a";
image = cell.oci-images.my-srv-a.image.name;
}
];
};
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: apps/v1
kind: Deployment
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: v1
kind: Service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let
inherit (inputs) std;
in {
my-srv-a = std.lib.ops.mkStandardOCI {
name = "ghcr.io/myorg/myrepo/my-srv-a";
operable = cell.operables.my-srv-a;
meta.description = "Minimal OCI Image for my-srv-a";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let
inherit (inputs) std;
inherit (inputs.nixpkgs) lib;
in {
my-srv-a = std.lib.ops.mkOperable {
package = cell.packages.my-srv-a;
runtimeScript = lib.getExe cell.packages.my-srv-a;
meta.description = "A thin warpper around my-srv-a binary executable";
};
}
12 changes: 12 additions & 0 deletions src/std/templates/microservice-aws-github/ops/backend/packages.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let
inherit (inputs) nixpkgs std self;

src = std.incl self [
"folder"
"my.file"
];
in {
my-srv-a = {
meta.description = "my-srv-a binary executable";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let
inherit (inputs.std) dmerge;
inherit (inputs.std.inputs) haumea;
inherit (inputs.std.lib.ops) readYAML;

loadYaml = _: _: readYAML;
baseline = with haumea.lib;
load {
src = ./deployments;
loader = [(matchers.regex ''^.+\.(yaml|yml)'' loadYaml)];
};
in {
my-srv-a = dmerge baseline.my-srv-a {
meta.description = "Development Environment for my-srv-a";
deployment = {
spec.template.spec.containers = dmerge.updateOn "name" [
{
name = "my-srv-a";
image = cell.oci-images.my-srv-a.image.name;
}
];
};
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: apps/v1
kind: Deployment
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: v1
kind: Service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let
inherit (inputs) std;
in {
my-srv-a = std.lib.ops.mkStandardOCI {
name = "ghcr.io/myorg/myrepo/my-srv-a";
operable = cell.operables.my-srv-a;
meta.description = "Minimal OCI Image for my-srv-a";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let
inherit (inputs) std;
inherit (inputs.nixpkgs) lib;
in {
my-srv-a = std.lib.ops.mkOperable {
package = cell.packages.my-srv-a;
runtimeScript = lib.getExe cell.packages.my-srv-a;
meta.description = "A thin warpper around my-srv-a binary executable";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let
inherit (inputs) nixpkgs std self;

src = std.incl self [
"folder"
"my.file"
];
in {
my-srv-a = {
meta.description = "my-srv-a binary executable";
};
}
21 changes: 21 additions & 0 deletions src/std/templates/microservice-aws-github/ops/github/action.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let
inherit (inputs.nixpkgs) lib;
inherit (inputs.std.lib) dev;

template = (import ./action/template.nix) lib;
in {
inherit template;
ci = dev.mkNixago {
output = ".github/workflows/ci-cd.yaml";
format = "yaml";
hook.mode = "copy";
data = template {
default_branch = "main";
platform = "aws"; # gc, azure, digitalocean
# set up with nixbuild.net to speed up builds
withNixbuild = false;
# use with persistent discovery; needs to be setup separately
withPersistentDiscovery = false;
};
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
credentials = {
name = "Configure AWS Credentials";
uses = "aws-actions/configure-aws-credentials@main";
"with" = {
"role-to-assume" = "\${{ var.AWS_ROLE_ARN }}";
"aws-region" = "\${{ var.AWS_REGION }}";
};
};
ecr = {
name = "Login to Amazon ECR";
uses = "aws-actions/amazon-ecr-login@v1";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
lib: {
default_branch,
platform,
withNixbuild,
withPersistentDiscovery,
}: let
aws = import ./aws.nix;
installNixAction = {uses = "blaggacao/nix-quick-install-action@detect-nix-flakes-config";};
useNixbuildAction = {
uses = "nixbuild/nixbuild-action@v17";
"with" = {
nixbuild_ssh_key = "\${{ secrets.SSH_KEY }}";
generate_summary_for = "job";
};
};
discoverAction = {
uses = "divnix/std-action/discover@main";
id = "discovery";
};
runAction = {uses = "divnix/std-action/run@main";};
# Jobs
discover = {
outputs.hits = "\${{ steps.discovery.outputs.hits }}";
runs-on = "ubuntu-latest";
steps =
[]
# account is part of ecr url, thus part of `hits` output and needs to pass so we can't mask it
++ lib.optionals (platform == "aws") [(lib.recursiveUpdate aws.credentials {mask-aws-account-id = false;})]
++ lib.optionals (platform == "aws") [aws.ecr]
++ lib.optionals (!withPersistentDiscovery) [installNixAction]
++ lib.optionals withNixbuild [useNixbuildAction]
++ [discoverAction];
};
worker = {
block,
action,
needs ? [],
steps ? [],
}: {
needs = ["discover"] ++ needs;
name = "\${{ matrix.target.jobName }}";
"if" = "fromJSON(needs.discover.outputs.hits).${block}.${action} != '{}'";
strategy = {
fail-fast = false;
matrix.target = "\${{ fromJSON(needs.discover.outputs.hits).${block}.${action} }}";
};
steps =
[]
++ [installNixAction]
++ lib.optionals withNixbuild [useNixbuildAction]
++ [runAction];
};
in {
name = "CI/CD";
on = {
pull_request.branches = [default_branch];
push.branches = [default_branch];
};
permissions = {
id-token = "write";
contents = "read";
};
concurrency = {
group = ''std-''${{ github.workflow }}-''${{ runner.os }}-''${{ github.ref }}'';
"cancel-in-progress" = true;
};
jobs = {
inherit discover;
build = worker {
block = "packages";
action = "build";
};
images = worker {
block = "images";
action = "publish";
needs = ["build"];
steps =
lib.optionals (platform == "aws") [aws.credentials]
lib.optionals (platform == "aws") [aws.ecr];
};
deploy = worker {
block = "deployments";
action = "apply";
needs = ["images"];
steps =
lib.optionals (platform == "aws") [aws.credentials];
};
};
}