From a049c9cc6b11018e7fc497876ad0402965d87397 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 22 Jun 2024 16:07:12 +0200 Subject: add CD to fly --- default.nix | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fly.toml | 22 ++++++++------------- justfile | 15 ++++++++++++-- 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/default.nix b/default.nix index b05d12e..4c29731 100644 --- a/default.nix +++ b/default.nix @@ -14,6 +14,27 @@ let runCommandLocal; version = "unstable"; + mkDocker = type: { server, website }: + let + PORT = 80; + in + pkgs.dockerTools.${type} { + name = "registry.fly.io/alanpearce-eu"; + config = { + Cmd = [ "${server}/bin/server" ]; + Env = [ + "PRODUCTION=true" + "LISTEN_ADDRESS=::" + "PORT=${builtins.toString PORT}" + "ROOT=${website}" + ]; + ExposedPorts = { + "${builtins.toString PORT}/tcp" = { }; + }; + }; + }; + mkDockerStream = mkDocker "streamLayeredImage"; + mkDockerImage = mkDocker "buildLayeredImage"; in rec { pre-commit-check = pre-commit-hooks.run { @@ -70,4 +91,49 @@ rec { } '' ${builder}/bin/build -s $src -d $out ''; + server = buildGoApplication { + pname = "server"; + inherit version; + CGO_ENABLED = 0; + src = with lib.fileset; toSource { + root = ./.; + fileset = unions [ + ./go.mod + ./go.sum + ./cmd/server + ./internal + ]; + }; + modules = ./nix/gomod2nix.toml; + patchPhase = '' + ${pkgs.templ}/bin/templ generate + ''; + subPackages = [ "cmd/server" ]; + ldflags = [ + "-s" + "-w" + ]; + }; + docker-stream = mkDockerStream { inherit server website; }; + docker-image = mkDockerImage { inherit server website; }; + + server-amd64-linux = server.overrideAttrs (old: old // { + GOOS = "linux"; + GOARCH = "amd64"; + fixupPhase = '' + if [[ -d $out/bin/linux_amd64 ]] + then + mv $out/bin/linux_amd64/server $out/bin/server + rmdir $out/bin/linux_amd64 + fi + ''; + }); + docker-image-amd64-linux = mkDockerImage { + inherit website; + server = server-amd64-linux; + }; + docker-stream-amd64-linux = mkDockerStream { + inherit website; + server = server-amd64-linux; + }; } diff --git a/fly.toml b/fly.toml index 4fc764b..4488c03 100644 --- a/fly.toml +++ b/fly.toml @@ -1,4 +1,4 @@ -# fly.toml app configuration file generated for homestead on 2023-09-14T11:40:37+02:00 +# fly.toml app configuration file generated for alanpearce-eu on 2024-06-19T15:17:02+02:00 # # See https://fly.io/docs/reference/configuration/ for information about how to use this file. # @@ -10,18 +10,14 @@ primary_region = "ams" image = "registry.fly.io/alanpearce-eu" [env] + BASE_URL = "https://alanpearce.eu" PORT = "80" REDIRECT_OTHER_HOSTNAMES = "true" - BASE_URL = "https://alanpearce.eu" [[files]] guest_path = "/config.toml" local_path = "config.toml" -[metrics] - port = 9091 - path = "/metrics" - [http_service] internal_port = 80 force_https = true @@ -29,18 +25,16 @@ primary_region = "ams" auto_start_machines = true min_machines_running = 3 processes = [ "app" ] + [http_service.concurrency] type = "requests" soft_limit = 15000 + [http_service.http_options] h2_backend = true + [http_service.http_options.response] pristine = true - [[http_service.checks]] - grace_period = "15s" - interval = "30s" - method = "GET" - timeout = "1s" - path = "/health" - [http_service.checks.headers] - Host = "fly-internal" + +[[vm]] + size = "shared-cpu-1x" diff --git a/justfile b/justfile index 8b2922c..08f6b01 100755 --- a/justfile +++ b/justfile @@ -1,5 +1,8 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i "just --justfile" +#!/usr/bin/env nix-shell +#!nix-shell -i "just --justfile" + +docker-registry := "registry.fly.io/alanpearce-eu" +docker-tag := `date +%Y%m%d%H%M%S` default: @just --list --justfile {{ justfile() }} --unsorted @@ -27,6 +30,14 @@ dev: ci: build check-links +cd: + fly auth docker + nix-build -A docker-stream-amd64-linux | sh | gzip --fast | \ + skopeo copy --dest-precompute-digests \ + docker-archive:/dev/stdin \ + docker://{{ docker-registry }}:{{ docker-tag }} + fly deploy --image {{ docker-registry }}:{{ docker-tag }} + deploy-vercel-preview: build vercel pull --environment=preview vercel deploy -- cgit 1.4.1