2022-02-18 19:02:45 +00:00
|
|
|
{ pkgs, ... }:
|
|
|
|
let script = ''
|
|
|
|
cd /root/nixos-config
|
|
|
|
|
|
|
|
${pkgs.gitMinimal}/bin/git checkout main
|
|
|
|
${pkgs.gitMinimal}/bin/git fetch origin
|
|
|
|
${pkgs.gitMinimal}/bin/git reset --hard origin/main
|
|
|
|
|
2022-02-18 19:13:36 +00:00
|
|
|
TOKEN="$(${pkgs.coreutils}/bin/cat /run/secrets/services/hydra/gitea_token)"
|
2022-02-18 19:02:45 +00:00
|
|
|
|
|
|
|
deploy_finished() {
|
|
|
|
MAIN_SHA=$(${pkgs.curl}/bin/curl -X GET \
|
2022-02-18 19:15:22 +00:00
|
|
|
"https://git.chir.rs/api/v1/repos/darkkirb/nixos-config/branches/main?token=$TOKEN" \
|
2022-02-18 19:02:45 +00:00
|
|
|
-H "accept: application/json" | ${pkgs.jq}/bin/jq -r '.commit.id')
|
|
|
|
STAGING_SHA=$(${pkgs.curl}/bin/curl -X GET \
|
2022-02-18 19:15:22 +00:00
|
|
|
"https://git.chir.rs/api/v1/repos/darkkirb/nixos-config/branches/staging?token=$TOKEN" \
|
2022-02-18 19:02:45 +00:00
|
|
|
-H "accept: application/json" | ${pkgs.jq}/bin/jq -r '.commit.id')
|
|
|
|
|
|
|
|
if [[ "$MAIN_SHA" == "$STAGING_SHA" ]]; then
|
|
|
|
${pkgs.coreutils}/bin/echo "No changes to deploy"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
COMMIT_STATUS=$(${pkgs.curl}/bin/curl -X 'GET' \
|
|
|
|
"https://git.chir.rs/api/v1/repos/darkkirb/nixos-config/commits/$MAIN_SHA/status?token=$TOKEN" \
|
|
|
|
-H 'accept: application/json' | ${pkgs.jq}/bin/jq -r '.state')
|
|
|
|
|
|
|
|
if [[ "$COMMIT_STATUS" != "success" ]]; then
|
|
|
|
${pkgs.coreutils}/bin/echo "Commit status is not success"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2022-02-18 19:25:57 +00:00
|
|
|
${pkgs.gitMinimal}/bin/git checkout staging
|
|
|
|
${pkgs.gitMinimal}/bin/git reset --hard $MAIN_SHA
|
2022-02-18 19:42:38 +00:00
|
|
|
${pkgs.gitMinimal}/bin/git push origin staging --force
|
2022-02-18 19:25:57 +00:00
|
|
|
${pkgs.gitMinimal}/bin/git checkout main
|
2022-02-18 19:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deploy_finished
|
|
|
|
|
2022-02-28 14:25:41 +00:00
|
|
|
PATH="${pkgs.gitMinimal}/bin:$PATH" ${pkgs.nixFlakes}/bin/nix flake update
|
2022-02-18 19:02:45 +00:00
|
|
|
|
|
|
|
${pkgs.gitMinimal}/bin/git commit -am "Automatic nixpkgs update"
|
|
|
|
${pkgs.gitMinimal}/bin/git push origin main
|
|
|
|
''; in
|
|
|
|
{
|
|
|
|
systemd.services.autodeploy = {
|
|
|
|
enable = true;
|
|
|
|
description = "Automatically deploy updates";
|
|
|
|
inherit script;
|
|
|
|
};
|
|
|
|
systemd.timers.autodeploy = {
|
|
|
|
enable = true;
|
|
|
|
description = "Automatically deploy updates";
|
|
|
|
requires = [ "autodeploy.service" ];
|
2022-02-18 19:07:25 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-02-18 19:02:45 +00:00
|
|
|
timerConfig = {
|
|
|
|
OnUnitActiveSec = 3600;
|
|
|
|
OnBootSec = 3600;
|
|
|
|
};
|
|
|
|
};
|
2022-02-18 19:13:36 +00:00
|
|
|
sops.secrets."services/hydra/gitea_token" = { };
|
2022-02-18 19:02:45 +00:00
|
|
|
}
|