about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-10-07 11:56:36 -0700
committerFranck Cuny <franck@fcuny.net>2022-10-07 11:56:36 -0700
commit0b7e1ae5aea05cdc5069b3edb15bd2165a2e1411 (patch)
treed883ca82a5a6a3361f36549eb703a19d9d9d4fa0
parentbuild: drop the configuration for drone (diff)
downloadfcuny.net-0b7e1ae5aea05cdc5069b3edb15bd2165a2e1411.tar.gz
ref(build): build and deploy with nix
Refactored the build of the docker image to be done with nix: the flake
knows how to build the docker image, using caddy as a HTTP server. It
generates a small image, with the configuration for caddy and the site
generated by hugo (`nix build`).

Deleted the Dockerfile since the creation is done with nix.

Got rid of the deployment script since this is also done via the
flake (`nix run .#deploy`).
-rw-r--r--Caddyfile14
-rw-r--r--Dockerfile7
-rw-r--r--config.toml2
-rw-r--r--flake.nix80
-rw-r--r--fly.toml1
-rwxr-xr-xscripts/deploy.sh14
6 files changed, 68 insertions, 50 deletions
diff --git a/Caddyfile b/Caddyfile
new file mode 100644
index 0000000..7456ef5
--- /dev/null
+++ b/Caddyfile
@@ -0,0 +1,14 @@
+{
+        http_port 8080
+        auto_https off
+}
+
+http://fcunynet.fly.dev {
+        redir https://fcuny.net
+}
+
+http://fcuny.net {
+        root * {$SITE_ROOT}
+        encode gzip
+        file_server
+}
diff --git a/Dockerfile b/Dockerfile
deleted file mode 100644
index 64c0f97..0000000
--- a/Dockerfile
+++ /dev/null
@@ -1,7 +0,0 @@
-FROM klakegg/hugo:0.91.2-ext-alpine-onbuild AS hugo
-
-FROM pierrezemb/gostatic
-
-COPY --from=hugo /target /srv/http/
-
-CMD ["-port", "8080" , "-https-promote"]
diff --git a/config.toml b/config.toml
index 8cb0c62..ace03c8 100644
--- a/config.toml
+++ b/config.toml
@@ -2,7 +2,7 @@ baseURL = "https://fcuny.net/"
 languageCode = "en-us"
 title = "Franck's rambling"
 publishDir = "docs"
-enableGitInfo = true
+enableGitInfo = false
 
 [params]
   homeText = "A collection of posts"
diff --git a/flake.nix b/flake.nix
index 79e6953..a752a6e 100644
--- a/flake.nix
+++ b/flake.nix
@@ -8,39 +8,63 @@
 
   outputs = { self, nixpkgs, flake-utils }:
     flake-utils.lib.eachDefaultSystem (system:
-      let pkgs = nixpkgs.legacyPackages.${system};
-      in {
-        defaultPackage = with pkgs;
-          stdenv.mkDerivation {
-            pname = "fcuny.net";
-            version = self.lastModifiedDate;
-            src = ./.;
-            buildInputs = [ hugo git ];
-            buildPhase = ''
-              mkdir -p $out
-              hugo --minify --destination $out
-            '';
-            dontInstall = true;
+      let
+        pkgs = nixpkgs.legacyPackages.${system};
+        caddyfile = ./Caddyfile;
+      in
+      {
+        packages = {
+          site = with pkgs;
+            stdenv.mkDerivation {
+              pname = "fcuny.net";
+              version = self.lastModifiedDate;
+              src = ./.;
+              buildInputs = [ hugo git ];
+              buildPhase = ''
+                mkdir -p $out
+                hugo --minify --destination $out
+              '';
+              dontInstall = true;
+            };
+          container = pkgs.dockerTools.buildLayeredImage {
+            name = self.packages."${system}".site.pname;
+            tag = self.packages."${system}".site.version;
+            config = {
+              Cmd = [ "${pkgs.caddy}/bin/caddy" "run" "--adapter" "caddyfile" "--config" "${caddyfile}" ];
+              Env = [
+                "SITE_ROOT=${self.packages."${system}".site}"
+              ];
+            };
           };
-
-        defaultApp = pkgs.writers.writeBashBin "run-hugo" ''
-          set -e
-          set -o pipefail
-          export PATH=${pkgs.lib.makeBinPath [ pkgs.hugo pkgs.git ]}
-          hugo server -D
-        '';
-
-        apps = {
-          deploy = pkgs.pkgs.writeShellScriptBin "run-deploy" ''
+          deploy = pkgs.writeShellScriptBin "deploy" ''
             set -euxo pipefail
-            export PATH=${
-              pkgs.lib.makeBinPath [ pkgs.hugo pkgs.git pkgs.jq pkgs.flyctl ]
-            }:$PATH
-            bash ./scripts/deploy.sh
+            export PATH="${pkgs.lib.makeBinPath [(pkgs.docker.override { clientOnly = true; }) pkgs.flyctl]}:$PATH"
+            archive=${self.packages.x86_64-linux.container}
+            # load archive, drop all output except last line case of warnings), print image name
+            image=$(docker load < $archive | tail -n1 | awk '{ print $3; }')
+            flyctl deploy --image $image --local-only
           '';
+          hugo = pkgs.writeShellScriptBin "hugo" ''
+            set -euo pipefail
+            export PATH=${pkgs.lib.makeBinPath [ pkgs.hugo pkgs.git ]}
+            hugo server -D
+          '';
+        };
+
+        apps = {
+          deploy = {
+            type = "app";
+            program = "${self.packages."${system}".deploy}/bin/deploy";
+          };
+          default = {
+            type = "app";
+            program = "${self.packages."${system}".hugo}/bin/hugo";
+          };
         };
 
+        defaultPackage = self.packages."${system}".container;
+
         devShell =
-          pkgs.mkShell { buildInputs = with pkgs; [ hugo flyctl git jq ]; };
+          pkgs.mkShell { buildInputs = with pkgs; [ hugo flyctl git ]; };
       });
 }
diff --git a/fly.toml b/fly.toml
index 46468c1..d9a891f 100644
--- a/fly.toml
+++ b/fly.toml
@@ -28,6 +28,7 @@ kill_timeout = 5
   [[services.ports]]
     handlers = ["http"]
     port = 80
+    force_https = true
 
   [[services.ports]]
     handlers = ["tls", "http"]
diff --git a/scripts/deploy.sh b/scripts/deploy.sh
deleted file mode 100755
index 69ad82a..0000000
--- a/scripts/deploy.sh
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/usr/bin/env bash
-
-git diff --exit-code
-git diff --staged --exit-code
-
-flyctl deploy
-
-VERSION=$(flyctl info -j |jq -r '.App | "fcuny.net/v\(.Version)"')
-
-git tag -a --message ${VERSION} ${VERSION}
-git push origin --all
-git push origin --tags
-
-flyctl agent stop