gitea: add actions runner and CI/CD deploy workflow
This will avoid me having to run "deploy" myself on my laptop. All I will need to do is push a commit and it will self-deploy.
This commit is contained in:
60
.gitea/workflows/deploy.yml
Normal file
60
.gitea/workflows/deploy.yml
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
name: Build and Deploy
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: nix
|
||||||
|
env:
|
||||||
|
GIT_SSH_COMMAND: "ssh -i /run/agenix/ci-deploy-key -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/etc/ci-known-hosts"
|
||||||
|
steps:
|
||||||
|
- uses: https://github.com/actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Unlock git-crypt
|
||||||
|
run: |
|
||||||
|
git-crypt unlock /run/agenix/git-crypt-key-server-config
|
||||||
|
|
||||||
|
- name: Build NixOS configuration
|
||||||
|
run: |
|
||||||
|
nix build .#nixosConfigurations.muffin.config.system.build.toplevel -L
|
||||||
|
|
||||||
|
- name: Deploy via deploy-rs
|
||||||
|
run: |
|
||||||
|
eval $(ssh-agent -s)
|
||||||
|
ssh-add /run/agenix/ci-deploy-key
|
||||||
|
nix run github:serokell/deploy-rs -- .#muffin --skip-checks --ssh-opts="-o StrictHostKeyChecking=yes -o UserKnownHostsFile=/etc/ci-known-hosts"
|
||||||
|
|
||||||
|
- name: Health check
|
||||||
|
run: |
|
||||||
|
sleep 10
|
||||||
|
ssh -i /run/agenix/ci-deploy-key -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/etc/ci-known-hosts root@server-public \
|
||||||
|
"systemctl is-active gitea && systemctl is-active caddy && systemctl is-active continuwuity && systemctl is-active coturn"
|
||||||
|
|
||||||
|
- name: Notify success
|
||||||
|
if: success()
|
||||||
|
run: |
|
||||||
|
TOPIC=$(cat /run/agenix/ntfy-alerts-topic | tr -d '[:space:]')
|
||||||
|
TOKEN=$(cat /run/agenix/ntfy-alerts-token | tr -d '[:space:]')
|
||||||
|
curl -sf -o /dev/null -X POST \
|
||||||
|
"https://ntfy.sigkill.computer/$TOPIC" \
|
||||||
|
-H "Authorization: Bearer $TOKEN" \
|
||||||
|
-H "Title: [muffin] Deploy succeeded" \
|
||||||
|
-H "Priority: default" \
|
||||||
|
-H "Tags: white_check_mark" \
|
||||||
|
-d "server-config deployed from commit ${GITHUB_SHA::8}"
|
||||||
|
|
||||||
|
- name: Notify failure
|
||||||
|
if: failure()
|
||||||
|
run: |
|
||||||
|
TOPIC=$(cat /run/agenix/ntfy-alerts-topic 2>/dev/null | tr -d '[:space:]')
|
||||||
|
TOKEN=$(cat /run/agenix/ntfy-alerts-token 2>/dev/null | tr -d '[:space:]')
|
||||||
|
curl -sf -o /dev/null -X POST \
|
||||||
|
"https://ntfy.sigkill.computer/$TOPIC" \
|
||||||
|
-H "Authorization: Bearer $TOKEN" \
|
||||||
|
-H "Title: [muffin] Deploy FAILED" \
|
||||||
|
-H "Priority: urgent" \
|
||||||
|
-H "Tags: rotating_light" \
|
||||||
|
-d "server-config deploy failed at commit ${GITHUB_SHA::8}" || true
|
||||||
@@ -99,7 +99,11 @@ Each service file in `services/` follows this structure:
|
|||||||
- **git-crypt**: `secrets/` directory and `usb-secrets/usb-secrets-key*` are encrypted (see `.gitattributes`)
|
- **git-crypt**: `secrets/` directory and `usb-secrets/usb-secrets-key*` are encrypted (see `.gitattributes`)
|
||||||
- **agenix**: secrets declared in `modules/age-secrets.nix`, decrypted at runtime to `/run/agenix/`
|
- **agenix**: secrets declared in `modules/age-secrets.nix`, decrypted at runtime to `/run/agenix/`
|
||||||
- **Identity**: USB drive at `/mnt/usb-secrets/usb-secrets-key`
|
- **Identity**: USB drive at `/mnt/usb-secrets/usb-secrets-key`
|
||||||
- **Encrypting new secrets**: The agenix encryption key is in `usb-secrets/usb-secrets-key` (SSH private key, git-crypt encrypted). To create a new secret: derive the age public key with `ssh-keygen -y -f usb-secrets/usb-secrets-key | ssh-to-age`, then encrypt with `age -r <public-key> -o secrets/<name>.age`.
|
- **Encrypting new secrets**: The agenix identity is an SSH private key at `usb-secrets/usb-secrets-key` (git-crypt encrypted). To encrypt a new secret, use the SSH public key directly with `age -R`:
|
||||||
|
```bash
|
||||||
|
age -R <(ssh-keygen -y -f usb-secrets/usb-secrets-key) -o secrets/<name>.age /path/to/plaintext
|
||||||
|
```
|
||||||
|
- **DO NOT use `ssh-to-age`**. Using `ssh-to-age` to derive a native age public key and then encrypting with `age -r age1...` produces `X25519` recipient stanzas. The SSH private key identity on the server can only decrypt `ssh-ed25519` stanzas. This mismatch causes `age: error: no identity matched any of the recipients` at deploy time. Always use `age -R` with the SSH public key directly.
|
||||||
- Never read or commit plaintext secrets. Never log secret values.
|
- Never read or commit plaintext secrets. Never log secret values.
|
||||||
|
|
||||||
### Important Patterns
|
### Important Patterns
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
./services/caddy.nix
|
./services/caddy.nix
|
||||||
./services/immich.nix
|
./services/immich.nix
|
||||||
./services/gitea.nix
|
./services/gitea.nix
|
||||||
|
./services/gitea-actions-runner.nix
|
||||||
./services/minecraft.nix
|
./services/minecraft.nix
|
||||||
|
|
||||||
./services/wg.nix
|
./services/wg.nix
|
||||||
@@ -73,6 +74,18 @@
|
|||||||
./services/mollysocket.nix
|
./services/mollysocket.nix
|
||||||
];
|
];
|
||||||
|
|
||||||
|
# Hosts entries for CI/CD deploy targets
|
||||||
|
networking.hosts."192.168.1.50" = [ "server-public" ];
|
||||||
|
networking.hosts."192.168.1.223" = [ "desktop" ];
|
||||||
|
|
||||||
|
# SSH known_hosts for CI runner (pinned host keys)
|
||||||
|
environment.etc."ci-known-hosts".text = ''
|
||||||
|
server-public ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFMjgaMnE+zS7tL+m5E7gh9Q9U1zurLdmU0qcmEmaucu
|
||||||
|
192.168.1.50 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFMjgaMnE+zS7tL+m5E7gh9Q9U1zurLdmU0qcmEmaucu
|
||||||
|
git.sigkill.computer ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFMjgaMnE+zS7tL+m5E7gh9Q9U1zurLdmU0qcmEmaucu
|
||||||
|
git.gardling.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFMjgaMnE+zS7tL+m5E7gh9Q9U1zurLdmU0qcmEmaucu
|
||||||
|
'';
|
||||||
|
|
||||||
services.kmscon.enable = true;
|
services.kmscon.enable = true;
|
||||||
|
|
||||||
systemd.targets = {
|
systemd.targets = {
|
||||||
@@ -249,6 +262,14 @@
|
|||||||
|
|
||||||
users.groups.${service_configs.media_group} = { };
|
users.groups.${service_configs.media_group} = { };
|
||||||
|
|
||||||
|
users.users.gitea-runner = {
|
||||||
|
isSystemUser = true;
|
||||||
|
group = "gitea-runner";
|
||||||
|
home = "/var/lib/gitea-runner";
|
||||||
|
description = "Gitea Actions CI runner";
|
||||||
|
};
|
||||||
|
users.groups.gitea-runner = { };
|
||||||
|
|
||||||
users.users.${username} = {
|
users.users.${username} = {
|
||||||
isNormalUser = true;
|
isNormalUser = true;
|
||||||
extraGroups = [
|
extraGroups = [
|
||||||
|
|||||||
@@ -68,19 +68,19 @@
|
|||||||
group = "root";
|
group = "root";
|
||||||
};
|
};
|
||||||
|
|
||||||
# ntfy-alerts secrets
|
# ntfy-alerts secrets (group-readable for CI runner notifications)
|
||||||
ntfy-alerts-topic = {
|
ntfy-alerts-topic = {
|
||||||
file = ../secrets/ntfy-alerts-topic.age;
|
file = ../secrets/ntfy-alerts-topic.age;
|
||||||
mode = "0400";
|
mode = "0440";
|
||||||
owner = "root";
|
owner = "root";
|
||||||
group = "root";
|
group = "gitea-runner";
|
||||||
};
|
};
|
||||||
|
|
||||||
ntfy-alerts-token = {
|
ntfy-alerts-token = {
|
||||||
file = ../secrets/ntfy-alerts-token.age;
|
file = ../secrets/ntfy-alerts-token.age;
|
||||||
mode = "0400";
|
mode = "0440";
|
||||||
owner = "root";
|
owner = "root";
|
||||||
group = "root";
|
group = "gitea-runner";
|
||||||
};
|
};
|
||||||
|
|
||||||
# Firefox Sync server secrets (SYNC_MASTER_SECRET)
|
# Firefox Sync server secrets (SYNC_MASTER_SECRET)
|
||||||
@@ -128,5 +128,36 @@
|
|||||||
group = "continuwuity";
|
group = "continuwuity";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# CI deploy SSH key
|
||||||
|
ci-deploy-key = {
|
||||||
|
file = ../secrets/ci-deploy-key.age;
|
||||||
|
mode = "0400";
|
||||||
|
owner = "gitea-runner";
|
||||||
|
group = "gitea-runner";
|
||||||
|
};
|
||||||
|
|
||||||
|
# Git-crypt symmetric key for dotfiles repo
|
||||||
|
git-crypt-key-dotfiles = {
|
||||||
|
file = ../secrets/git-crypt-key-dotfiles.age;
|
||||||
|
mode = "0400";
|
||||||
|
owner = "root";
|
||||||
|
group = "root";
|
||||||
|
};
|
||||||
|
|
||||||
|
# Git-crypt symmetric key for server-config repo
|
||||||
|
git-crypt-key-server-config = {
|
||||||
|
file = ../secrets/git-crypt-key-server-config.age;
|
||||||
|
mode = "0400";
|
||||||
|
owner = "gitea-runner";
|
||||||
|
group = "gitea-runner";
|
||||||
|
};
|
||||||
|
|
||||||
|
# Gitea Actions runner registration token
|
||||||
|
gitea-runner-token = {
|
||||||
|
file = ../secrets/gitea-runner-token.age;
|
||||||
|
mode = "0400";
|
||||||
|
owner = "gitea-runner";
|
||||||
|
group = "gitea-runner";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
# ZFS cache directory - persisting the directory instead of the file
|
# ZFS cache directory - persisting the directory instead of the file
|
||||||
# avoids "device busy" errors when ZFS atomically updates the cache
|
# avoids "device busy" errors when ZFS atomically updates the cache
|
||||||
"/etc/zfs"
|
"/etc/zfs"
|
||||||
|
"/var/lib/gitea-runner"
|
||||||
];
|
];
|
||||||
|
|
||||||
files = [
|
files = [
|
||||||
|
|||||||
BIN
secrets/ci-deploy-key.age
Normal file
BIN
secrets/ci-deploy-key.age
Normal file
Binary file not shown.
Binary file not shown.
BIN
secrets/git-crypt-key-dotfiles.age
Normal file
BIN
secrets/git-crypt-key-dotfiles.age
Normal file
Binary file not shown.
BIN
secrets/git-crypt-key-server-config.age
Normal file
BIN
secrets/git-crypt-key-server-config.age
Normal file
Binary file not shown.
BIN
secrets/gitea-runner-token.age
Normal file
BIN
secrets/gitea-runner-token.age
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
46
services/gitea-actions-runner.nix
Normal file
46
services/gitea-actions-runner.nix
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
config,
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
service_configs,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
{
|
||||||
|
services.gitea-actions-runner.instances.muffin = {
|
||||||
|
enable = true;
|
||||||
|
name = "muffin";
|
||||||
|
url = config.services.gitea.settings.server.ROOT_URL;
|
||||||
|
tokenFile = config.age.secrets.gitea-runner-token.path;
|
||||||
|
labels = [ "nix:host" ];
|
||||||
|
hostPackages = with pkgs; [
|
||||||
|
bash
|
||||||
|
coreutils
|
||||||
|
curl
|
||||||
|
gawk
|
||||||
|
git
|
||||||
|
git-crypt
|
||||||
|
gnugrep
|
||||||
|
gnused
|
||||||
|
jq
|
||||||
|
nix
|
||||||
|
nodejs
|
||||||
|
openssh
|
||||||
|
];
|
||||||
|
settings = {
|
||||||
|
runner = {
|
||||||
|
capacity = 1;
|
||||||
|
timeout = "3h";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
# Override DynamicUser to use our static gitea-runner user
|
||||||
|
systemd.services."gitea-runner-muffin" = {
|
||||||
|
serviceConfig = {
|
||||||
|
DynamicUser = lib.mkForce false;
|
||||||
|
User = "gitea-runner";
|
||||||
|
Group = "gitea-runner";
|
||||||
|
};
|
||||||
|
environment.GIT_SSH_COMMAND = "ssh -i /run/agenix/ci-deploy-key -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/etc/ci-known-hosts";
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -37,6 +37,7 @@
|
|||||||
};
|
};
|
||||||
# only I shall use gitea
|
# only I shall use gitea
|
||||||
service.DISABLE_REGISTRATION = true;
|
service.DISABLE_REGISTRATION = true;
|
||||||
|
actions.ENABLED = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -31,5 +31,8 @@
|
|||||||
|
|
||||||
# used for deploying configs to server
|
# used for deploying configs to server
|
||||||
users.users.root.openssh.authorizedKeys.keys =
|
users.users.root.openssh.authorizedKeys.keys =
|
||||||
config.users.users.${username}.openssh.authorizedKeys.keys;
|
config.users.users.${username}.openssh.authorizedKeys.keys
|
||||||
|
++ [
|
||||||
|
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIC5ZYN6idL/w/mUIfPOH1i+Q/SQXuzAMQUEuWpipx1Pc ci-deploy@muffin"
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
60
tests/gitea-runner.nix
Normal file
60
tests/gitea-runner.nix
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
{
|
||||||
|
config,
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
pkgs.testers.runNixOSTest {
|
||||||
|
name = "gitea-runner";
|
||||||
|
nodes.machine =
|
||||||
|
{ pkgs, ... }:
|
||||||
|
{
|
||||||
|
services.gitea = {
|
||||||
|
enable = true;
|
||||||
|
database.type = "sqlite3";
|
||||||
|
settings = {
|
||||||
|
server = {
|
||||||
|
HTTP_PORT = 3000;
|
||||||
|
ROOT_URL = "http://localhost:3000";
|
||||||
|
DOMAIN = "localhost";
|
||||||
|
};
|
||||||
|
actions.ENABLED = true;
|
||||||
|
service.DISABLE_REGISTRATION = true;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
specialisation.runner = {
|
||||||
|
inheritParentConfig = true;
|
||||||
|
configuration.services.gitea-actions-runner.instances.test = {
|
||||||
|
enable = true;
|
||||||
|
name = "ci";
|
||||||
|
url = "http://localhost:3000";
|
||||||
|
labels = [ "native:host" ];
|
||||||
|
tokenFile = "/var/lib/gitea/runner_token";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
testScript = ''
|
||||||
|
start_all()
|
||||||
|
|
||||||
|
machine.wait_for_unit("gitea.service")
|
||||||
|
machine.wait_for_open_port(3000)
|
||||||
|
|
||||||
|
# Generate runner token
|
||||||
|
machine.succeed(
|
||||||
|
"su -l gitea -s /bin/sh -c '${pkgs.gitea}/bin/gitea actions generate-runner-token --work-path /var/lib/gitea' | tail -1 | sed 's/^/TOKEN=/' > /var/lib/gitea/runner_token"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Switch to runner specialisation
|
||||||
|
machine.succeed(
|
||||||
|
"/run/current-system/specialisation/runner/bin/switch-to-configuration test"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Start the runner (specialisation switch doesn't auto-start new services)
|
||||||
|
machine.succeed("systemctl start gitea-runner-test.service")
|
||||||
|
machine.wait_for_unit("gitea-runner-test.service")
|
||||||
|
machine.succeed("sleep 5")
|
||||||
|
machine.succeed("test -f /var/lib/gitea-runner/test/.runner")
|
||||||
|
'';
|
||||||
|
}
|
||||||
@@ -27,4 +27,7 @@ in
|
|||||||
|
|
||||||
# torrent audit test
|
# torrent audit test
|
||||||
torrentAuditTest = handleTest ./torrent-audit.nix;
|
torrentAuditTest = handleTest ./torrent-audit.nix;
|
||||||
|
|
||||||
|
# gitea runner test
|
||||||
|
giteaRunnerTest = handleTest ./gitea-runner.nix;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user