summary refs log tree commit diff
path: root/modules/auto-redeploy.nix
blob: 4b2a640033cb36cf02153ee0b638c2c85d5612e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{ config, pkgs, lib, secrets, ... }:
{
  systemd.timers = {
    "auto-redeploy" = {
      wantedBy = [ "timers.target" ];
      after = [ "network.target" ];
      timerConfig = {
        #every 30 seconds since boot
        OnBootSec = "10s";
        OnUnitActiveSec = "30s";
        Unit = "auto-redeploy.service";
      };
    };
  };
  systemd.services = {
    "auto-redeploy" = {
        #wantedBy = [ "multi-user.target" ];
        #after = [ "network.target" ];
        serviceConfig = {
            ExecStart = pkgs.writeShellScript "auto-redeploy" ''
              ${pkgs.curl}/bin/curl -S -H "Content-Type: application/json" -d '{"username": "${config.networking.hostName} - redeploy", "content": "System started redeploy."}' ${secrets.webhooks.discord.deploy}
              cd /Spacebar-Open-Architecture
              #store current commit hash
              echo "Current commit: $(${pkgs.git}/bin/git rev-parse HEAD)"
              currentCommit=$(${pkgs.git}/bin/git rev-parse HEAD)
              ${pkgs.git}/bin/git fetch --all
              #check if there are any new commits
              echo "Checking for new commits..."
              echo "Local: $(${pkgs.git}/bin/git rev-parse HEAD)"
              echo "Remote: $(${pkgs.git}/bin/git rev-parse @{u})"
              if [ $(${pkgs.git}/bin/git rev-parse HEAD) = $(${pkgs.git}/bin/git rev-parse @{u}) ]; then
                echo "Already up-to-date"
                exit 0
              fi
              #pull new commits
              ${pkgs.git}/bin/git pull
              #send commit log to discord
              ${pkgs.curl}/bin/curl -S -H "Content-Type: application/json" -d '{"username": "${config.networking.hostName} - redeploy", "content": "```$(git log --pretty=format:"%h - %s" $currentCommit..HEAD)```"}' ${secrets.webhooks.discord.deploy}
              #call ./update.sh and store output (including stderr) in a file
              ./update.sh 2>&1 | tee /tmp/update.log
              #send the output to discord as a file
              ${pkgs.curl}/bin/curl -S -F "file=@/tmp/update.log" -F "filename=update.log" -F "content=System finished redeploy." -H "Content-Type: multipart/form-data" ${secrets.webhooks.discord.deploy}
              '';
            #Restart = "always";
            #RestartSec = 60;
            User = "root";
        };
    };
  };
}