Skip to content

Commit 12d2d3c

Browse files
committed
wip: add gh action template
1 parent 61cb926 commit 12d2d3c

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let
2+
inherit (inputs.nixpkgs) lib;
3+
inherit (inputs.std.lib) dev;
4+
5+
renderFile = (import ./action/template.nix) lib args;
6+
in {
7+
ci = dev.mkNixago {
8+
output = ".github/workflows/ci-cd.yaml";
9+
data = renderFile {
10+
default_branch = "main";
11+
platform = "aws"; # gc, azure, digitalocean
12+
# set up with nixbuild.net to speed up builds
13+
withNixbuild = false;
14+
# use with persistent discovery; needs to be setup separately
15+
withPersistentDiscovery = false;
16+
};
17+
};
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
credentials = {
3+
name = "Configure AWS Credentials";
4+
uses = "aws-actions/configure-aws-credentials@main";
5+
"with" = {
6+
"role-to-assume" = "\${{ var.AWS_ROLE_ARN }}";
7+
"aws-region" = "\${{ var.AWS_REGION }}";
8+
};
9+
};
10+
ecr = {
11+
name = "Login to Amazon ECR";
12+
uses = "aws-actions/amazon-ecr-login@v1";
13+
};
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
lib: {
2+
default_branch,
3+
platform,
4+
withNixbuild,
5+
withPersistentDiscovery,
6+
}: let
7+
aws = import ./aws.nix;
8+
installNixAction = {uses = "blaggacao/nix-quick-install-action@detect-nix-flakes-config";};
9+
useNixbuildAction = {
10+
uses = "nixbuild/nixbuild-action@v17";
11+
"with" = {
12+
nixbuild_ssh_key = "\${{ secrets.SSH_KEY }}";
13+
generate_summary_for = "job";
14+
};
15+
};
16+
discoverAction = {
17+
uses = "divnix/std-action/discover@main";
18+
id = "discovery";
19+
};
20+
runAction = {uses = "divnix/std-action/run@main";};
21+
# Jobs
22+
discover = {
23+
outputs.hits = "\${{ steps.discovery.outputs.hits }}";
24+
runs-on = "ubuntu-latest";
25+
steps =
26+
[]
27+
# account is part of ecr url, thus part of `hits` output and needs to pass so we can't mask it
28+
++ lib.optionals (platform == "aws") [lib.recursiveUpdate aws.credentials {mask-aws-account-id = false;}]
29+
++ lib.optionals (platform == "aws") [aws.ecr]
30+
++ lib.optionals (!withPersistentDiscovery) [installNixAction]
31+
++ lib.optionals withNixbuild [useNixbuildAction]
32+
++ [discoverAction];
33+
};
34+
worker = {
35+
block,
36+
action,
37+
needs ? [],
38+
steps ? [],
39+
}: {
40+
needs = ["discover"] ++ needs;
41+
name = "\${{ matrix.target.jobName }}";
42+
"if" = "fromJSON(needs.discover.outputs.hits).${block}.${action} != '{}'";
43+
strategy = {
44+
fail-fast = false;
45+
matrix.target = "\${{ fromJSON(needs.discover.outputs.hits).${block}.${action} }}";
46+
};
47+
steps =
48+
[]
49+
++ [installNixAction]
50+
++ lib.optionals withNixbuild [useNixbuildAction]
51+
++ [runAction];
52+
};
53+
in {
54+
name = "CI/CD";
55+
on = {
56+
pull_request.branches = [default_branch];
57+
push.branches = [default_branch];
58+
};
59+
permissions = {
60+
id-token = "write";
61+
contents = "read";
62+
};
63+
concurrency = {
64+
group = ''std-''${{ github.workflow }}-''${{ runner.os }}-''${{ github.ref }}'';
65+
"cancel-in-progress" = true;
66+
};
67+
jobs = {
68+
inherit discover;
69+
build = worker {
70+
block = "packages";
71+
action = "build";
72+
};
73+
images = worker {
74+
block = "images";
75+
action = "publish";
76+
needs = ["build"];
77+
steps =
78+
lib.optionals (platform == "aws") [aws.credentials]
79+
lib.optionals (platform == "aws") [aws.ecr];
80+
};
81+
deploy = worker {
82+
block = "deployments";
83+
action = "apply";
84+
needs = ["images"];
85+
steps =
86+
lib.optionals (platform == "aws") [aws.credentials];
87+
};
88+
};
89+
}

0 commit comments

Comments
 (0)