Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

effects.cloudflarePages: init #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions effects/cloudflare-pages/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{ lib
, mkEffect
, nodePackages
}:

{ content
, secretName ? throw ''effects.cloudflare-pages: You must provide `secretName`, the name of the secret which holds the "${secretField}" field.''
, secretField ? "token"
, projectName
, accountId
, branch ? null
, secretsMap ? { }
, extraDeployArgs ? [ ]
, ...
}@args:
let
deployArgs = [
"--project-name=${projectName}"
] ++ lib.optionals (branch != null) [
"--branch=${branch}"
] ++ extraDeployArgs;
in
mkEffect (args // {
name = "cloudflare-pages";
inputs = [ nodePackages.wrangler ];
secretsMap = { "cloudflare" = secretName; } // secretsMap;
CLOUDFLARE_ACCOUNT_ID = accountId;
effectScript = ''
echo 1>&2 Running wrangler pages publish...
export CLOUDFLARE_API_TOKEN="$(readSecretString cloudflare .${secretField})"
wrangler pages publish ${content} ${lib.escapeShellArgs deployArgs}
'';
})
104 changes: 104 additions & 0 deletions effects/cloudflare-pages/flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{ config, lib, withSystem, ... }:
let
inherit (lib)
mkOption
types
optionalAttrs
mapAttrs;
cfg = config.hercules-ci.cloudflare-pages;
in
{
options.hercules-ci.cloudflare-pages = mkOption {
type = types.attrsOf (types.submodule {
options = {
content = mkOption {
type = types.functionTo (types.either types.str types.package);
example = lib.literalExpression "{ config, ... }: config.packages.default";
description = ''
A function that returns the site content, also known as the Publish directory.
This includes files such as index.html, style sheets, etc.
You will typically put a derivation here.

The function receives the same arguments as [perSystem](https://flake.parts/options/flake-parts.html#opt-perSystem).
'';
};

secretName = mkOption {
type = types.str;
description = ''
The secret that will be looked up in [secrets.json](https://docs.hercules-ci.com/hercules-ci-agent/secrets-json).
This secret must hold the `secretField` field, with a string value that is a Cloudflare API token.
'';
};

secretField = mkOption {
type = types.str;
default = "token";
description = "The name of the field inside the `secretName` secret which holds the Cloudflare API token.";
};

projectName = mkOption {
type = types.str;
description = "The project name you assigned to the website when initializing it.";
};

accountId = mkOption {
type = types.str;
description = "The Cloudflare account ID which the project is under.";
};

branch = mkOption {
type = types.nullOr (types.functionTo types.str);
default = null;
example = lib.literalExpression ''{ branch, ... }: branch'';
description = ''
The name of the branch you want to deploy to.
The function receives the value of attributes under [herculesCI.repo](https://flake.parts/options/hercules-ci-effects.html#opt-herculesCI.repo).
'';
};

secretsMap = mkOption {
type = types.attrs;
default = { };
description = "Extra secrets to add to the effect.";
};

extraDeployArgs = mkOption {
type = (types.listOf types.str);
default = [ ];
description = "Extra arguments to pass to the wrangler publish invocation.";
};

effect.system = mkOption {
type = types.str;
default = config.defaultEffectSystem;
defaultText = lib.literalMD "config.defaultEffectSystem";
example = "aarch64-linux";
description = "The [system](https://nixos.org/manual/nix/stable/command-ref/conf-file.html#conf-system) on which to run the website deployment on.";
};
};
});
};

config = {
herculesCI = herculesCI: {
onPush.default.outputs.effects = {
cloudflare-pages = mapAttrs
(name: projectCfg: optionalAttrs (projectCfg != { }) (withSystem projectCfg.effect.system ({ hci-effects, ... }:
hci-effects.cloudflarePages {
inherit (projectCfg)
secretName
secretField
projectName
accountId
secretsMap
extraDeployArgs;
branch = projectCfg.branch herculesCI.config.repo;
content = withSystem projectCfg.effect.system projectCfg.content;
}
)))
cfg;
};
};
};
}
4 changes: 3 additions & 1 deletion effects/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ checkVersion

gitWriteBranch = self.modularEffectWithUserModule "gitWriteBranch" ./write-branch/effect-module.nix;

cloudflarePages = callPackage ./cloudflare-pages { };

netlifyDeploy = callPackage ./netlify { };
netlifySetupHook = pkgs.runCommand "hercules-ci-netlify-setup-hook" {} ''
netlifySetupHook = pkgs.runCommand "hercules-ci-netlify-setup-hook" { } ''
mkdir -p $out/nix-support
cp ${./netlify/netlify-setup-hook.sh} $out/nix-support/setup-hook
'';
Expand Down
1 change: 1 addition & 0 deletions flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
./flake-modules/github-pages.nix
./flake-modules/github-releases
./effects/flake-update/flake-module.nix
./effects/cloudflare-pages/flake-module.nix
];
}