@zukhruf/nx 0.8.30 → 0.8.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +378 -19
- package/dist/generators/add-keys.d.ts +7 -0
- package/dist/generators/add-keys.d.ts.map +1 -0
- package/dist/generators/add-keys.js +14 -0
- package/dist/generators/add-keys.js.map +1 -0
- package/dist/generators/add-lines.d.ts +8 -0
- package/dist/generators/add-lines.d.ts.map +1 -0
- package/dist/generators/add-lines.js +21 -0
- package/dist/generators/add-lines.js.map +1 -0
- package/dist/generators/app-directory.d.ts +17 -0
- package/dist/generators/app-directory.d.ts.map +1 -0
- package/dist/generators/app-directory.js +34 -0
- package/dist/generators/app-directory.js.map +1 -0
- package/dist/generators/dokploy/files.d.ts +21 -0
- package/dist/generators/dokploy/files.d.ts.map +1 -0
- package/dist/generators/dokploy/files.js +755 -0
- package/dist/generators/dokploy/files.js.map +1 -0
- package/dist/generators/dokploy/generator.d.ts +75 -0
- package/dist/generators/dokploy/generator.d.ts.map +1 -0
- package/dist/generators/dokploy/generator.js +139 -0
- package/dist/generators/dokploy/generator.js.map +1 -0
- package/dist/generators/dokploy/providers.d.ts +15 -0
- package/dist/generators/dokploy/providers.d.ts.map +1 -0
- package/dist/generators/dokploy/providers.js +114 -0
- package/dist/generators/dokploy/providers.js.map +1 -0
- package/dist/generators/init/files.js +3 -3
- package/dist/generators/init/files.js.map +1 -1
- package/dist/generators/init/generator.d.ts.map +1 -1
- package/dist/generators/init/generator.js +1 -16
- package/dist/generators/init/generator.js.map +1 -1
- package/dist/generators/init/nx-json.d.ts +16 -3
- package/dist/generators/init/nx-json.d.ts.map +1 -1
- package/dist/generators/init/nx-json.js +20 -5
- package/dist/generators/init/nx-json.js.map +1 -1
- package/dist/generators/init/root-package-json.d.ts +6 -0
- package/dist/generators/init/root-package-json.d.ts.map +1 -1
- package/dist/generators/init/root-package-json.js +15 -12
- package/dist/generators/init/root-package-json.js.map +1 -1
- package/dist/generators/node-app/files.d.ts +43 -0
- package/dist/generators/node-app/files.d.ts.map +1 -0
- package/dist/generators/node-app/files.js +522 -0
- package/dist/generators/node-app/files.js.map +1 -0
- package/dist/generators/node-app/generator.d.ts +17 -0
- package/dist/generators/node-app/generator.d.ts.map +1 -0
- package/dist/generators/node-app/generator.js +319 -0
- package/dist/generators/node-app/generator.js.map +1 -0
- package/dist/generators/nx-plugin.d.ts +16 -0
- package/dist/generators/nx-plugin.d.ts.map +1 -0
- package/dist/generators/nx-plugin.js +27 -0
- package/dist/generators/nx-plugin.js.map +1 -0
- package/dist/generators/pair/files.d.ts +4 -0
- package/dist/generators/pair/files.d.ts.map +1 -0
- package/dist/generators/pair/files.js +73 -0
- package/dist/generators/pair/files.js.map +1 -0
- package/dist/generators/pair/generator.d.ts +19 -0
- package/dist/generators/pair/generator.d.ts.map +1 -0
- package/dist/generators/pair/generator.js +70 -0
- package/dist/generators/pair/generator.js.map +1 -0
- package/dist/generators/prisma/files.d.ts +20 -0
- package/dist/generators/prisma/files.d.ts.map +1 -0
- package/dist/generators/prisma/files.js +183 -0
- package/dist/generators/prisma/files.js.map +1 -0
- package/dist/generators/prisma/generator.d.ts +17 -0
- package/dist/generators/prisma/generator.d.ts.map +1 -0
- package/dist/generators/prisma/generator.js +258 -0
- package/dist/generators/prisma/generator.js.map +1 -0
- package/dist/generators/react-app/files.d.ts +7 -0
- package/dist/generators/react-app/files.d.ts.map +1 -0
- package/dist/generators/react-app/files.js +188 -0
- package/dist/generators/react-app/files.js.map +1 -0
- package/dist/generators/react-app/generator.d.ts +12 -0
- package/dist/generators/react-app/generator.d.ts.map +1 -0
- package/dist/generators/react-app/generator.js +242 -0
- package/dist/generators/react-app/generator.js.map +1 -0
- package/dist/generators/react.d.ts +29 -0
- package/dist/generators/react.d.ts.map +1 -0
- package/dist/generators/react.js +64 -0
- package/dist/generators/react.js.map +1 -0
- package/dist/generators/root-dependencies.d.ts +21 -0
- package/dist/generators/root-dependencies.d.ts.map +1 -0
- package/dist/generators/root-dependencies.js +56 -0
- package/dist/generators/root-dependencies.js.map +1 -0
- package/dist/generators/ui-package/files.d.ts +37 -0
- package/dist/generators/ui-package/files.d.ts.map +1 -0
- package/dist/generators/ui-package/files.js +118 -0
- package/dist/generators/ui-package/files.js.map +1 -0
- package/dist/generators/ui-package/generator.d.ts +14 -0
- package/dist/generators/ui-package/generator.d.ts.map +1 -0
- package/dist/generators/ui-package/generator.js +111 -0
- package/dist/generators/ui-package/generator.js.map +1 -0
- package/dist/generators/versions.d.ts +49 -1
- package/dist/generators/versions.d.ts.map +1 -1
- package/dist/generators/versions.js +61 -1
- package/dist/generators/versions.js.map +1 -1
- package/generators.json +30 -0
- package/package.json +24 -5
- package/src/generators/dokploy/schema.json +48 -0
- package/src/generators/node-app/schema.json +22 -0
- package/src/generators/pair/schema.json +18 -0
- package/src/generators/prisma/schema.json +21 -0
- package/src/generators/react-app/schema.json +17 -0
- package/src/generators/ui-package/schema.json +22 -0
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
import { dokployProvider, hcloudProvider, randomProvider, } from "./providers.js";
|
|
2
|
+
/** The root of the OpenTofu project. */
|
|
3
|
+
export const infraRoot = 'infra';
|
|
4
|
+
function providersOf(stack) {
|
|
5
|
+
return [
|
|
6
|
+
dokployProvider,
|
|
7
|
+
hcloudProvider,
|
|
8
|
+
...(stack.database ? [randomProvider] : []),
|
|
9
|
+
];
|
|
10
|
+
}
|
|
11
|
+
const image = (stack, name) => `ghcr.io/${stack.owner}/${stack.name}-${name}:latest`;
|
|
12
|
+
const hostDescription = (stack) => stack.host.kind === 'dns'
|
|
13
|
+
? stack.host.host
|
|
14
|
+
: `${stack.name}.<server ipv4>.sslip.io`;
|
|
15
|
+
export const gitignore = `# State is committed ENCRYPTED (git is the state store) at
|
|
16
|
+
# src/terraform.tfstate. Only unencrypted backups and lock files are ignored.
|
|
17
|
+
#
|
|
18
|
+
# CRITICAL: *.backup files are PLAINTEXT and must NEVER be committed.
|
|
19
|
+
*.backup
|
|
20
|
+
.terraform.tfstate.lock.info
|
|
21
|
+
|
|
22
|
+
# Provider plugins / module cache
|
|
23
|
+
.terraform/
|
|
24
|
+
|
|
25
|
+
# Saved plans are regenerated by preview and freshness-guarded.
|
|
26
|
+
tfplan
|
|
27
|
+
|
|
28
|
+
# tfvars can carry secrets
|
|
29
|
+
*.tfvars
|
|
30
|
+
*.tfvars.json
|
|
31
|
+
|
|
32
|
+
# Crash logs
|
|
33
|
+
crash.log
|
|
34
|
+
crash.*.log
|
|
35
|
+
|
|
36
|
+
# Local overrides
|
|
37
|
+
override.tf
|
|
38
|
+
override.tf.json
|
|
39
|
+
*_override.tf
|
|
40
|
+
*_override.tf.json
|
|
41
|
+
`;
|
|
42
|
+
// Prettier reads the root ignore files only, so the provider caches that
|
|
43
|
+
// tofu init writes, the saved plan and the state are named here.
|
|
44
|
+
export const prettierignore = `.terraform
|
|
45
|
+
tfplan
|
|
46
|
+
${infraRoot}/src/terraform.tfstate
|
|
47
|
+
`;
|
|
48
|
+
export function envExample(stack) {
|
|
49
|
+
const record = stack.host.kind === 'dns'
|
|
50
|
+
? ` and manages the\n# ${stack.host.host} record`
|
|
51
|
+
: '';
|
|
52
|
+
const registry = stack.registry
|
|
53
|
+
? `
|
|
54
|
+
# GitHub classic token of ${stack.registry.username} with read:packages only. The
|
|
55
|
+
# Dokploy server logs in to GHCR with it and pulls the private images. The stack
|
|
56
|
+
# reads it as var.ghcr_pull_token.
|
|
57
|
+
GHCR_PULL_TOKEN=
|
|
58
|
+
TF_VAR_ghcr_pull_token=\${GHCR_PULL_TOKEN}
|
|
59
|
+
`
|
|
60
|
+
: '';
|
|
61
|
+
return `# Operator secrets for the ${stack.name} OpenTofu stack.
|
|
62
|
+
#
|
|
63
|
+
# Copy to infra/.env (gitignored) and fill in, with unquoted values. Nx loads
|
|
64
|
+
# this file for every target of the project, and the @zukhruf/nx/opentofu
|
|
65
|
+
# targets (init, preview, show, output) read it. CI reads the same names from
|
|
66
|
+
# repository secrets; applies run only in CI (.github/workflows/deploy.yml).
|
|
67
|
+
|
|
68
|
+
# Dokploy panel API key (sent as x-api-key). The stack reads it as
|
|
69
|
+
# var.dokploy_api_key; Nx expands the reference below.
|
|
70
|
+
DOKPLOY_API_KEY=
|
|
71
|
+
TF_VAR_dokploy_api_key=\${DOKPLOY_API_KEY}
|
|
72
|
+
|
|
73
|
+
# Hetzner Cloud API token: reads the ${stack.server} server${record}. The hcloud
|
|
74
|
+
# provider reads HCLOUD_TOKEN.
|
|
75
|
+
HCLOUD_TOKEN=
|
|
76
|
+
${registry}
|
|
77
|
+
# State and plan ENCRYPTION passphrase (-> TF_VAR_tf_encryption_passphrase).
|
|
78
|
+
# The state is committed to git encrypted; this passphrase is its key.
|
|
79
|
+
# Min 16 chars. BACK IT UP: losing it makes the committed state unrecoverable.
|
|
80
|
+
TF_ENCRYPTION_PASSPHRASE=
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
const logging = ` logging:
|
|
84
|
+
driver: json-file
|
|
85
|
+
options:
|
|
86
|
+
max-size: 10m
|
|
87
|
+
max-file: '3'
|
|
88
|
+
`;
|
|
89
|
+
export function composeYml(stack) {
|
|
90
|
+
const { name, app, database } = stack;
|
|
91
|
+
const pull = stack.registry
|
|
92
|
+
? `# The images are private GHCR packages: OpenTofu stores a Dokploy registry,
|
|
93
|
+
# which logs the server in to GHCR, and the compose pulls with that login.`
|
|
94
|
+
: `# The images are public GHCR packages, so the server needs no registry login.
|
|
95
|
+
# They hold the compiled app, never a secret; those come from the environment.`;
|
|
96
|
+
const postgres = database
|
|
97
|
+
? ` postgres:
|
|
98
|
+
image: postgres:18-alpine
|
|
99
|
+
${logging} environment:
|
|
100
|
+
POSTGRES_DB: ${database.role}
|
|
101
|
+
POSTGRES_USER: ${database.role}
|
|
102
|
+
POSTGRES_PASSWORD: \${POSTGRES_PASSWORD}
|
|
103
|
+
# Postgres 18 keeps its data in a folder of its major version under
|
|
104
|
+
# /var/lib/postgresql (PGDATA=/var/lib/postgresql/18/docker), so the volume
|
|
105
|
+
# holds the parent folder and a major upgrade finds the old data beside it.
|
|
106
|
+
volumes:
|
|
107
|
+
- pgdata:/var/lib/postgresql
|
|
108
|
+
healthcheck:
|
|
109
|
+
test: ['CMD-SHELL', 'pg_isready -h 127.0.0.1 -U ${database.role} -d ${database.role}']
|
|
110
|
+
interval: 5s
|
|
111
|
+
timeout: 5s
|
|
112
|
+
retries: 10
|
|
113
|
+
restart: unless-stopped
|
|
114
|
+
|
|
115
|
+
# One-shot: applies the Prisma migrations of ${database.root} before the
|
|
116
|
+
# backend starts (${database.root}/Dockerfile.migration).
|
|
117
|
+
migrate:
|
|
118
|
+
image: ${image(stack, 'migrate')}
|
|
119
|
+
pull_policy: always
|
|
120
|
+
environment:
|
|
121
|
+
CONNECTION_STRING: \${CONNECTION_STRING}
|
|
122
|
+
depends_on:
|
|
123
|
+
postgres:
|
|
124
|
+
condition: service_healthy
|
|
125
|
+
restart: 'no'
|
|
126
|
+
|
|
127
|
+
`
|
|
128
|
+
: '';
|
|
129
|
+
const backendDatabase = database
|
|
130
|
+
? ` environment:
|
|
131
|
+
CONNECTION_STRING: \${CONNECTION_STRING}
|
|
132
|
+
depends_on:
|
|
133
|
+
postgres:
|
|
134
|
+
condition: service_healthy
|
|
135
|
+
migrate:
|
|
136
|
+
condition: service_completed_successfully
|
|
137
|
+
`
|
|
138
|
+
: '';
|
|
139
|
+
const networks = database
|
|
140
|
+
? ` networks:
|
|
141
|
+
default: {}
|
|
142
|
+
dokploy-network: {}
|
|
143
|
+
`
|
|
144
|
+
: ` networks:
|
|
145
|
+
dokploy-network: {}
|
|
146
|
+
`;
|
|
147
|
+
const volumes = database
|
|
148
|
+
? `
|
|
149
|
+
volumes:
|
|
150
|
+
pgdata: {}
|
|
151
|
+
`
|
|
152
|
+
: '';
|
|
153
|
+
return `# ${name} Dokploy compose template (raw), production only: the ${app.name}
|
|
154
|
+
# image${database ? ', Postgres and the one-shot migrate service' : ''}.
|
|
155
|
+
#
|
|
156
|
+
# OpenTofu owns this compose and its environment (infra/src/environments/
|
|
157
|
+
# production). Do not edit them in the Dokploy panel. Its placeholders resolve
|
|
158
|
+
# from that environment; write a literal dollar sign as $$.
|
|
159
|
+
#
|
|
160
|
+
${pull}
|
|
161
|
+
|
|
162
|
+
services:
|
|
163
|
+
${postgres} ${app.name}:
|
|
164
|
+
image: ${image(stack, app.name)}
|
|
165
|
+
# A compose deploy reuses the cached image unless told to pull.
|
|
166
|
+
pull_policy: always
|
|
167
|
+
restart: unless-stopped
|
|
168
|
+
${logging}${backendDatabase}${networks} labels:
|
|
169
|
+
- 'traefik.enable=true'
|
|
170
|
+
- 'traefik.docker.network=dokploy-network'
|
|
171
|
+
- 'traefik.http.routers.${name}-web.rule=Host(\`\${PUBLIC_HOST}\`)'
|
|
172
|
+
- 'traefik.http.routers.${name}-web.entrypoints=web'
|
|
173
|
+
- 'traefik.http.routers.${name}-web.middlewares=redirect-to-https@file'
|
|
174
|
+
- 'traefik.http.routers.${name}-web.service=${name}'
|
|
175
|
+
- 'traefik.http.routers.${name}-websecure.rule=Host(\`\${PUBLIC_HOST}\`)'
|
|
176
|
+
- 'traefik.http.routers.${name}-websecure.entrypoints=websecure'
|
|
177
|
+
- 'traefik.http.routers.${name}-websecure.tls.certresolver=letsencrypt'
|
|
178
|
+
- 'traefik.http.routers.${name}-websecure.service=${name}'
|
|
179
|
+
- 'traefik.http.services.${name}.loadbalancer.server.port=${app.port}'
|
|
180
|
+
|
|
181
|
+
networks:
|
|
182
|
+
dokploy-network:
|
|
183
|
+
external: true
|
|
184
|
+
${volumes}`;
|
|
185
|
+
}
|
|
186
|
+
export function versionsTf(stack) {
|
|
187
|
+
const providers = providersOf(stack)
|
|
188
|
+
.map((provider) => ` ${provider.name} = {
|
|
189
|
+
source = "${provider.source}"
|
|
190
|
+
version = "${provider.version}"
|
|
191
|
+
}`)
|
|
192
|
+
.join('\n');
|
|
193
|
+
return `terraform {
|
|
194
|
+
required_version = ">= 1.12.3"
|
|
195
|
+
|
|
196
|
+
required_providers {
|
|
197
|
+
${providers}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
`;
|
|
201
|
+
}
|
|
202
|
+
/** The lock blocks, in the order of their source addresses, as \`tofu init\` writes them. */
|
|
203
|
+
export function lockHcl(stack) {
|
|
204
|
+
const blocks = providersOf(stack)
|
|
205
|
+
.map((provider) => provider.lock)
|
|
206
|
+
.toSorted((a, b) => a.localeCompare(b));
|
|
207
|
+
return `# This file is maintained automatically by "tofu init".
|
|
208
|
+
# Manual edits may be lost in future updates.
|
|
209
|
+
|
|
210
|
+
${blocks.join('\n\n')}
|
|
211
|
+
`;
|
|
212
|
+
}
|
|
213
|
+
export const backendTf = `terraform {
|
|
214
|
+
# Git is the state store: the encrypted state is committed at
|
|
215
|
+
# infra/src/terraform.tfstate, and the apply workflow commits it back.
|
|
216
|
+
backend "local" {
|
|
217
|
+
path = "../../terraform.tfstate"
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
`;
|
|
221
|
+
export function encryptionTf(stack) {
|
|
222
|
+
const { name } = stack;
|
|
223
|
+
return `variable "tf_encryption_passphrase" {
|
|
224
|
+
description = "Passphrase for the Git-tracked encrypted state and saved plans."
|
|
225
|
+
type = string
|
|
226
|
+
sensitive = true
|
|
227
|
+
|
|
228
|
+
# CI renders an unset secret as "".
|
|
229
|
+
validation {
|
|
230
|
+
condition = length(var.tf_encryption_passphrase) > 0
|
|
231
|
+
error_message = "The passphrase is empty: set TF_ENCRYPTION_PASSPHRASE."
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
terraform {
|
|
236
|
+
encryption {
|
|
237
|
+
key_provider "pbkdf2" "${name}" {
|
|
238
|
+
passphrase = var.tf_encryption_passphrase
|
|
239
|
+
}
|
|
240
|
+
method "aes_gcm" "${name}" {
|
|
241
|
+
keys = key_provider.pbkdf2.${name}
|
|
242
|
+
}
|
|
243
|
+
state {
|
|
244
|
+
method = method.aes_gcm.${name}
|
|
245
|
+
enforced = true
|
|
246
|
+
}
|
|
247
|
+
plan {
|
|
248
|
+
method = method.aes_gcm.${name}
|
|
249
|
+
enforced = true
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
`;
|
|
254
|
+
}
|
|
255
|
+
export function providersTf(stack) {
|
|
256
|
+
const host = stack.host.kind === 'dns'
|
|
257
|
+
? `"${stack.host.host}"`
|
|
258
|
+
: `"${stack.name}.\${data.hcloud_server.dokploy.ipv4_address}.sslip.io"`;
|
|
259
|
+
const hostComment = stack.host.kind === 'dns'
|
|
260
|
+
? `# host has an A record in the ${stack.host.zone} zone (dns.tf).`
|
|
261
|
+
: `# sslip.io resolves <name>.<ipv4>.sslip.io to <ipv4>, so host needs no DNS
|
|
262
|
+
# record.`;
|
|
263
|
+
return `# The Dokploy API of the ${stack.server} server.
|
|
264
|
+
${hostComment}
|
|
265
|
+
locals {
|
|
266
|
+
dokploy_host = "${stack.dokployHost}"
|
|
267
|
+
host = ${host}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
provider "dokploy" {
|
|
271
|
+
host = local.dokploy_host
|
|
272
|
+
api_key = var.dokploy_api_key
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
# Authenticates with HCLOUD_TOKEN.
|
|
276
|
+
provider "hcloud" {}
|
|
277
|
+
`;
|
|
278
|
+
}
|
|
279
|
+
export function dnsTf(stack) {
|
|
280
|
+
const server = `data "hcloud_server" "dokploy" {
|
|
281
|
+
name = "${stack.server}"
|
|
282
|
+
}
|
|
283
|
+
`;
|
|
284
|
+
if (stack.host.kind === 'sslip')
|
|
285
|
+
return server;
|
|
286
|
+
return `${server}
|
|
287
|
+
# One record in the ${stack.host.zone} zone: the stack owns its record, never the zone.
|
|
288
|
+
resource "hcloud_zone_rrset" "${stack.name}" {
|
|
289
|
+
zone = "${stack.host.zone}"
|
|
290
|
+
name = "${stack.host.record}"
|
|
291
|
+
type = "A"
|
|
292
|
+
ttl = 60
|
|
293
|
+
|
|
294
|
+
records = [{
|
|
295
|
+
value = data.hcloud_server.dokploy.ipv4_address
|
|
296
|
+
comment = "${stack.name} on Dokploy"
|
|
297
|
+
}]
|
|
298
|
+
|
|
299
|
+
change_protection = true
|
|
300
|
+
|
|
301
|
+
lifecycle {
|
|
302
|
+
prevent_destroy = true
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
`;
|
|
306
|
+
}
|
|
307
|
+
export function mainTf(stack) {
|
|
308
|
+
const { name, app, database } = stack;
|
|
309
|
+
const password = database
|
|
310
|
+
? `
|
|
311
|
+
resource "random_password" "postgres" {
|
|
312
|
+
length = 32
|
|
313
|
+
special = false
|
|
314
|
+
}
|
|
315
|
+
`
|
|
316
|
+
: '';
|
|
317
|
+
const databaseEnv = database
|
|
318
|
+
? `
|
|
319
|
+
POSTGRES_PASSWORD=\${random_password.postgres.result}
|
|
320
|
+
CONNECTION_STRING=postgresql://${database.role}:\${random_password.postgres.result}@postgres:5432/${database.role}`
|
|
321
|
+
: '';
|
|
322
|
+
return `resource "dokploy_project" "${name}" {
|
|
323
|
+
name = "${name}"
|
|
324
|
+
# Required in practice: the provider writes the API's description into state
|
|
325
|
+
# even when it is unset, and the attribute is not computed. Keep it stable:
|
|
326
|
+
# the environment's project_id requires replacement.
|
|
327
|
+
description = "${name} at ${hostDescription(stack)} - managed by OpenTofu"
|
|
328
|
+
|
|
329
|
+
lifecycle {
|
|
330
|
+
prevent_destroy = true
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
# Dokploy creates this protected default environment with every project and
|
|
335
|
+
# refuses to create another named "production"; provider 0.8 adopts it on
|
|
336
|
+
# create, so a single apply provisions the stack.
|
|
337
|
+
resource "dokploy_environment" "production" {
|
|
338
|
+
name = "production"
|
|
339
|
+
description = "Production environment"
|
|
340
|
+
project_id = dokploy_project.${name}.id
|
|
341
|
+
|
|
342
|
+
lifecycle {
|
|
343
|
+
prevent_destroy = true
|
|
344
|
+
ignore_changes = [name, description]
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
${password}
|
|
348
|
+
resource "dokploy_compose" "${app.name}" {
|
|
349
|
+
environment_id = dokploy_environment.production.id
|
|
350
|
+
name = "${app.name}"
|
|
351
|
+
|
|
352
|
+
source_type = "raw"
|
|
353
|
+
compose_file_content = file("\${path.module}/../../../compose.dokploy.yml")
|
|
354
|
+
|
|
355
|
+
env = <<-EOT
|
|
356
|
+
PUBLIC_HOST=\${local.host}${databaseEnv}
|
|
357
|
+
EOT
|
|
358
|
+
|
|
359
|
+
# Deploys go through the API (compose.deploy), never the refresh-token
|
|
360
|
+
# webhook, so auto_deploy stays off. deploy_on_create stays off so creating
|
|
361
|
+
# the compose never launches a deployment that no job observes.
|
|
362
|
+
auto_deploy = false
|
|
363
|
+
deploy_on_create = false
|
|
364
|
+
|
|
365
|
+
lifecycle {
|
|
366
|
+
prevent_destroy = true
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
# Updating dokploy_compose only stores the compose. The stack does not deploy:
|
|
371
|
+
# the release job of the deploy workflow runs zukhruf's dokploy-deploy and
|
|
372
|
+
# wait-for-health actions after a new image, after an apply that changed the
|
|
373
|
+
# stack (tofu-apply's \`changed\`), and after any infra change since the last
|
|
374
|
+
# successful deploy, so a failed rollout is retried by the next run.
|
|
375
|
+
`;
|
|
376
|
+
}
|
|
377
|
+
export function registryTf(stack, registry) {
|
|
378
|
+
return `# Dokploy runs \`docker login\` on the server when it stores a registry, and a
|
|
379
|
+
# compose deploy pulls with that login, so the server pulls the private images.
|
|
380
|
+
# The login is the server's docker login for ghcr.io: each stack on the server
|
|
381
|
+
# that stores a GHCR registry replaces it. The URL is a bare host: Dokploy
|
|
382
|
+
# refuses one with a scheme ("Registry URL must be a valid hostname or
|
|
383
|
+
# hostname:port").
|
|
384
|
+
resource "dokploy_registry" "ghcr" {
|
|
385
|
+
registry_name = "${stack.name}-ghcr"
|
|
386
|
+
registry_type = "cloud"
|
|
387
|
+
registry_url = "ghcr.io"
|
|
388
|
+
image_prefix = "ghcr.io"
|
|
389
|
+
username = "${registry.username}"
|
|
390
|
+
password = var.ghcr_pull_token
|
|
391
|
+
}
|
|
392
|
+
`;
|
|
393
|
+
}
|
|
394
|
+
export function outputsTf(stack) {
|
|
395
|
+
return `output "dokploy_api" {
|
|
396
|
+
description = "Dokploy API base URL; the release job of the deploy workflow calls compose.deploy there."
|
|
397
|
+
value = local.dokploy_host
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
output "compose_id" {
|
|
401
|
+
description = "Dokploy compose ID of the stack, for API calls such as compose.one and compose.deploy."
|
|
402
|
+
value = dokploy_compose.${stack.app.name}.id
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
output "host" {
|
|
406
|
+
description = "The public host of the ${stack.app.name}; the release job waits for https://<host>/api/healthz."
|
|
407
|
+
value = local.host
|
|
408
|
+
}
|
|
409
|
+
`;
|
|
410
|
+
}
|
|
411
|
+
export function variablesTf(stack) {
|
|
412
|
+
const registry = stack.registry
|
|
413
|
+
? `
|
|
414
|
+
variable "ghcr_pull_token" {
|
|
415
|
+
description = "GitHub classic token with read:packages, supplied through TF_VAR_ghcr_pull_token."
|
|
416
|
+
type = string
|
|
417
|
+
sensitive = true
|
|
418
|
+
|
|
419
|
+
validation {
|
|
420
|
+
condition = length(var.ghcr_pull_token) > 0
|
|
421
|
+
error_message = "The GHCR pull token is empty: set the GHCR_PULL_TOKEN secret."
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
`
|
|
425
|
+
: '';
|
|
426
|
+
return `# CI renders an unset secret as "", so each variable refuses an empty value.
|
|
427
|
+
variable "dokploy_api_key" {
|
|
428
|
+
description = "Dokploy API key supplied through TF_VAR_dokploy_api_key."
|
|
429
|
+
type = string
|
|
430
|
+
sensitive = true
|
|
431
|
+
|
|
432
|
+
validation {
|
|
433
|
+
condition = length(var.dokploy_api_key) > 0
|
|
434
|
+
error_message = "The Dokploy API key is empty: set DOKPLOY_API_KEY."
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
${registry}`;
|
|
438
|
+
}
|
|
439
|
+
export function ciYml(stack) {
|
|
440
|
+
return `name: CI
|
|
441
|
+
|
|
442
|
+
on:
|
|
443
|
+
pull_request:
|
|
444
|
+
branches: [main]
|
|
445
|
+
push:
|
|
446
|
+
branches: [main]
|
|
447
|
+
|
|
448
|
+
# A new push to a pull request replaces its run; every commit on main keeps
|
|
449
|
+
# its own run.
|
|
450
|
+
concurrency:
|
|
451
|
+
group: \${{ github.workflow }}-\${{ github.event_name == 'pull_request' && github.ref || github.sha }}
|
|
452
|
+
cancel-in-progress: \${{ github.event_name == 'pull_request' }}
|
|
453
|
+
|
|
454
|
+
permissions:
|
|
455
|
+
# nx-set-shas reads this workflow's runs to find the last green commit.
|
|
456
|
+
actions: read
|
|
457
|
+
contents: read
|
|
458
|
+
|
|
459
|
+
jobs:
|
|
460
|
+
ci:
|
|
461
|
+
runs-on: ubuntu-latest
|
|
462
|
+
steps:
|
|
463
|
+
- name: Checkout
|
|
464
|
+
uses: actions/checkout@v7
|
|
465
|
+
with:
|
|
466
|
+
fetch-depth: 0
|
|
467
|
+
|
|
468
|
+
- name: Set up the workspace
|
|
469
|
+
uses: JanuaryLabs/zukhruf/.github/actions/setup-workspace@main
|
|
470
|
+
|
|
471
|
+
# ${stack.projectName}:typecheck runs tofu fmt and validate.
|
|
472
|
+
- name: Set up OpenTofu
|
|
473
|
+
uses: opentofu/setup-opentofu@v2
|
|
474
|
+
with:
|
|
475
|
+
tofu_version: latest
|
|
476
|
+
tofu_wrapper: false
|
|
477
|
+
|
|
478
|
+
- name: Find what changed since the last green commit
|
|
479
|
+
id: shas
|
|
480
|
+
uses: nrwl/nx-set-shas@v5
|
|
481
|
+
|
|
482
|
+
- parallel:
|
|
483
|
+
- name: Check Nx workspace sync
|
|
484
|
+
run: npx nx sync:check
|
|
485
|
+
|
|
486
|
+
- name: Check formatting
|
|
487
|
+
run: npx prettier --check .
|
|
488
|
+
|
|
489
|
+
# With no green commit yet, nx-set-shas falls back to HEAD~1 and affected
|
|
490
|
+
# would see only the last commit, so the first green run checks it all.
|
|
491
|
+
- name: Lint, type check, test and build what changed
|
|
492
|
+
env:
|
|
493
|
+
NO_PREVIOUS_BUILD: \${{ steps.shas.outputs.noPreviousBuild }}
|
|
494
|
+
run: |
|
|
495
|
+
if [ "$NO_PREVIOUS_BUILD" = "true" ]; then
|
|
496
|
+
npx nx run-many -t lint typecheck test build
|
|
497
|
+
else
|
|
498
|
+
npx nx affected -t lint typecheck test build
|
|
499
|
+
fi
|
|
500
|
+
`;
|
|
501
|
+
}
|
|
502
|
+
export function deployYml(stack) {
|
|
503
|
+
const { name, app, database, registry } = stack;
|
|
504
|
+
const images = [
|
|
505
|
+
{ image: `${name}-${app.name}`, dockerfile: `${app.root}/Dockerfile` },
|
|
506
|
+
...(database
|
|
507
|
+
? [
|
|
508
|
+
{
|
|
509
|
+
image: `${name}-migrate`,
|
|
510
|
+
dockerfile: `${database.root}/Dockerfile.migration`,
|
|
511
|
+
},
|
|
512
|
+
]
|
|
513
|
+
: []),
|
|
514
|
+
];
|
|
515
|
+
const buildMatrix = images
|
|
516
|
+
.map(({ image, dockerfile }) => ` - image: ${image}\n dockerfile: ${dockerfile}`)
|
|
517
|
+
.join('\n');
|
|
518
|
+
const promoteMatrix = images
|
|
519
|
+
.map(({ image }) => ` - image: ${image}`)
|
|
520
|
+
.join('\n');
|
|
521
|
+
const imageCheck = app.imageProjects
|
|
522
|
+
.map((project) => `has ${project}`)
|
|
523
|
+
.join(' || ');
|
|
524
|
+
const tfEnv = registry
|
|
525
|
+
? `
|
|
526
|
+
# The Dokploy registry logs the server in to GHCR with this token.
|
|
527
|
+
TF_ENV: >-
|
|
528
|
+
{"TF_VAR_ghcr_pull_token": \${{ toJSON(secrets.GHCR_PULL_TOKEN) }}}`
|
|
529
|
+
: '';
|
|
530
|
+
const packageAccess = registry
|
|
531
|
+
? `
|
|
532
|
+
# The packages are private: when a package is not linked to this repo, its
|
|
533
|
+
# settings must give this repo's Actions write access (Manage Actions
|
|
534
|
+
# access), or the push is refused.`
|
|
535
|
+
: '';
|
|
536
|
+
return `# Ships every green commit on main, in order: the images, then the infra
|
|
537
|
+
# stack, then the rollout. Nothing here is run by hand: a push to main drives
|
|
538
|
+
# CI, and CI's success drives this.
|
|
539
|
+
name: Deploy
|
|
540
|
+
|
|
541
|
+
on:
|
|
542
|
+
workflow_run:
|
|
543
|
+
workflows: [CI]
|
|
544
|
+
types: [completed]
|
|
545
|
+
branches: [main]
|
|
546
|
+
|
|
547
|
+
# One rollout at a time, queued, never cancelled.
|
|
548
|
+
concurrency:
|
|
549
|
+
group: deploy
|
|
550
|
+
cancel-in-progress: false
|
|
551
|
+
|
|
552
|
+
permissions:
|
|
553
|
+
# nx-set-shas reads this workflow's runs to find the last successful deploy.
|
|
554
|
+
actions: read
|
|
555
|
+
contents: read
|
|
556
|
+
|
|
557
|
+
env:
|
|
558
|
+
SHA: \${{ github.event.workflow_run.head_sha }}
|
|
559
|
+
|
|
560
|
+
jobs:
|
|
561
|
+
gate:
|
|
562
|
+
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push'
|
|
563
|
+
runs-on: ubuntu-latest
|
|
564
|
+
outputs:
|
|
565
|
+
current: \${{ steps.head.outputs.current }}
|
|
566
|
+
image: \${{ steps.affected.outputs.image }}
|
|
567
|
+
infra: \${{ steps.affected.outputs.infra }}
|
|
568
|
+
steps:
|
|
569
|
+
- name: Checkout main
|
|
570
|
+
uses: actions/checkout@v7
|
|
571
|
+
with:
|
|
572
|
+
fetch-depth: 0
|
|
573
|
+
|
|
574
|
+
# A newer commit has its own CI run and deploy; shipping this one now
|
|
575
|
+
# would roll main back.
|
|
576
|
+
- name: Ship only the commit CI tested
|
|
577
|
+
id: head
|
|
578
|
+
run: |
|
|
579
|
+
if [ "$(git rev-parse HEAD)" = "$SHA" ]; then
|
|
580
|
+
echo "current=true" >> "$GITHUB_OUTPUT"
|
|
581
|
+
else
|
|
582
|
+
echo "main moved past $SHA; its own deploy ships the newer commit."
|
|
583
|
+
echo "current=false" >> "$GITHUB_OUTPUT"
|
|
584
|
+
fi
|
|
585
|
+
|
|
586
|
+
- name: Set up the workspace
|
|
587
|
+
if: steps.head.outputs.current == 'true'
|
|
588
|
+
uses: JanuaryLabs/zukhruf/.github/actions/setup-workspace@main
|
|
589
|
+
|
|
590
|
+
- name: Find the last successful deploy
|
|
591
|
+
if: steps.head.outputs.current == 'true'
|
|
592
|
+
id: shas
|
|
593
|
+
uses: nrwl/nx-set-shas@v5
|
|
594
|
+
with:
|
|
595
|
+
workflow-id: deploy.yml
|
|
596
|
+
last-successful-event: workflow_run
|
|
597
|
+
|
|
598
|
+
# The images hold the projects that their Dockerfiles build.
|
|
599
|
+
- name: Decide what ships
|
|
600
|
+
if: steps.head.outputs.current == 'true'
|
|
601
|
+
id: affected
|
|
602
|
+
env:
|
|
603
|
+
NO_PREVIOUS_BUILD: \${{ steps.shas.outputs.noPreviousBuild }}
|
|
604
|
+
run: |
|
|
605
|
+
if [ "$NO_PREVIOUS_BUILD" = "true" ]; then
|
|
606
|
+
echo "No successful deploy yet: shipping everything."
|
|
607
|
+
{
|
|
608
|
+
echo "image=true"
|
|
609
|
+
echo "infra=true"
|
|
610
|
+
} >> "$GITHUB_OUTPUT"
|
|
611
|
+
exit 0
|
|
612
|
+
fi
|
|
613
|
+
projects="$(npx nx show projects --affected --json)"
|
|
614
|
+
echo "Affected since the last deploy: $projects"
|
|
615
|
+
has() { printf '%s' "$projects" | jq -e --arg p "$1" 'index($p) != null' >/dev/null; }
|
|
616
|
+
if ${imageCheck}; then
|
|
617
|
+
echo "image=true" >> "$GITHUB_OUTPUT"
|
|
618
|
+
else
|
|
619
|
+
echo "image=false" >> "$GITHUB_OUTPUT"
|
|
620
|
+
fi
|
|
621
|
+
# The base is the last successful deploy, so an infra change whose
|
|
622
|
+
# rollout failed stays affected until a rollout succeeds.
|
|
623
|
+
if has ${stack.projectName}; then
|
|
624
|
+
echo "infra=true" >> "$GITHUB_OUTPUT"
|
|
625
|
+
else
|
|
626
|
+
echo "infra=false" >> "$GITHUB_OUTPUT"
|
|
627
|
+
fi
|
|
628
|
+
|
|
629
|
+
# Each image is pushed under the commit first; promote moves :latest only
|
|
630
|
+
# after every image of the commit exists.
|
|
631
|
+
image:
|
|
632
|
+
needs: gate
|
|
633
|
+
if: needs.gate.outputs.current == 'true' && needs.gate.outputs.image == 'true'
|
|
634
|
+
runs-on: ubuntu-latest${packageAccess}
|
|
635
|
+
permissions:
|
|
636
|
+
contents: read
|
|
637
|
+
packages: write
|
|
638
|
+
strategy:
|
|
639
|
+
fail-fast: false
|
|
640
|
+
matrix:
|
|
641
|
+
include:
|
|
642
|
+
${buildMatrix}
|
|
643
|
+
steps:
|
|
644
|
+
- uses: actions/checkout@v7
|
|
645
|
+
with:
|
|
646
|
+
ref: \${{ env.SHA }}
|
|
647
|
+
|
|
648
|
+
- name: Lowercase the image owner
|
|
649
|
+
run: echo "OWNER_LC=\${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_ENV"
|
|
650
|
+
|
|
651
|
+
- uses: docker/setup-buildx-action@v4
|
|
652
|
+
|
|
653
|
+
- uses: docker/login-action@v4
|
|
654
|
+
with:
|
|
655
|
+
registry: ghcr.io
|
|
656
|
+
username: \${{ github.actor }}
|
|
657
|
+
password: \${{ secrets.GITHUB_TOKEN }}
|
|
658
|
+
|
|
659
|
+
- name: Build \${{ matrix.image }}
|
|
660
|
+
uses: docker/build-push-action@v7
|
|
661
|
+
timeout-minutes: 25
|
|
662
|
+
env:
|
|
663
|
+
DOCKER_BUILD_RECORD_UPLOAD: 'false'
|
|
664
|
+
with:
|
|
665
|
+
context: .
|
|
666
|
+
file: \${{ matrix.dockerfile }}
|
|
667
|
+
platforms: linux/amd64
|
|
668
|
+
push: true
|
|
669
|
+
build-args: |
|
|
670
|
+
BUILD_SHA=\${{ env.SHA }}
|
|
671
|
+
tags: ghcr.io/\${{ env.OWNER_LC }}/\${{ matrix.image }}:\${{ env.SHA }}
|
|
672
|
+
cache-from: type=registry,ref=ghcr.io/\${{ env.OWNER_LC }}/\${{ matrix.image }}:buildcache
|
|
673
|
+
cache-to: type=registry,ref=ghcr.io/\${{ env.OWNER_LC }}/\${{ matrix.image }}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true
|
|
674
|
+
|
|
675
|
+
# The compose runs :latest; the immutable :sha stays for rollbacks.
|
|
676
|
+
promote:
|
|
677
|
+
needs: image
|
|
678
|
+
runs-on: ubuntu-latest
|
|
679
|
+
permissions:
|
|
680
|
+
contents: read
|
|
681
|
+
packages: write
|
|
682
|
+
strategy:
|
|
683
|
+
fail-fast: false
|
|
684
|
+
matrix:
|
|
685
|
+
include:
|
|
686
|
+
${promoteMatrix}
|
|
687
|
+
steps:
|
|
688
|
+
- name: Lowercase the image owner
|
|
689
|
+
run: echo "OWNER_LC=\${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_ENV"
|
|
690
|
+
|
|
691
|
+
- uses: docker/setup-buildx-action@v4
|
|
692
|
+
|
|
693
|
+
- uses: docker/login-action@v4
|
|
694
|
+
with:
|
|
695
|
+
registry: ghcr.io
|
|
696
|
+
username: \${{ github.actor }}
|
|
697
|
+
password: \${{ secrets.GITHUB_TOKEN }}
|
|
698
|
+
|
|
699
|
+
- name: Promote \${{ matrix.image }} to latest
|
|
700
|
+
env:
|
|
701
|
+
IMAGE: \${{ matrix.image }}
|
|
702
|
+
run: |
|
|
703
|
+
docker buildx imagetools create \\
|
|
704
|
+
--tag "ghcr.io/\${OWNER_LC}/\${IMAGE}:latest" \\
|
|
705
|
+
"ghcr.io/\${OWNER_LC}/\${IMAGE}:\${SHA}"
|
|
706
|
+
|
|
707
|
+
# After new images, or with none: a failed build stops the stack, so a
|
|
708
|
+
# compose that needs the new images never rolls out with the old ones.
|
|
709
|
+
infra:
|
|
710
|
+
needs: [gate, image, promote]
|
|
711
|
+
if: >-
|
|
712
|
+
always() && needs.gate.result == 'success' && needs.gate.outputs.current == 'true'
|
|
713
|
+
&& (needs.image.result == 'success' || needs.image.result == 'skipped')
|
|
714
|
+
&& (needs.promote.result == 'success' || needs.promote.result == 'skipped')
|
|
715
|
+
permissions:
|
|
716
|
+
contents: write
|
|
717
|
+
uses: JanuaryLabs/zukhruf/.github/workflows/tofu-apply.yml@main
|
|
718
|
+
with:
|
|
719
|
+
stack: ${infraRoot}
|
|
720
|
+
environment: production
|
|
721
|
+
state_file: ${infraRoot}/src/terraform.tfstate
|
|
722
|
+
secrets:
|
|
723
|
+
TF_ENCRYPTION_PASSPHRASE: \${{ secrets.TF_ENCRYPTION_PASSPHRASE }}
|
|
724
|
+
DOKPLOY_API_KEY: \${{ secrets.DOKPLOY_API_KEY }}
|
|
725
|
+
HCLOUD_TOKEN: \${{ secrets.HCLOUD_TOKEN }}${tfEnv}
|
|
726
|
+
|
|
727
|
+
# Dokploy runs a stored compose only on compose.deploy, so a rollout follows
|
|
728
|
+
# new images, an apply that changed the stack, and any infra change since the
|
|
729
|
+
# last successful deploy (a retry of a failed rollout).
|
|
730
|
+
release:
|
|
731
|
+
needs: [gate, promote, infra]
|
|
732
|
+
if: >-
|
|
733
|
+
always() && needs.infra.result == 'success'
|
|
734
|
+
&& (needs.promote.result == 'success' || needs.infra.outputs.changed == 'true' || needs.gate.outputs.infra == 'true')
|
|
735
|
+
runs-on: ubuntu-latest
|
|
736
|
+
steps:
|
|
737
|
+
# Queues the rollout through the Dokploy API and waits until Dokploy
|
|
738
|
+
# reports the deployment done.
|
|
739
|
+
- name: Roll out the compose
|
|
740
|
+
uses: JanuaryLabs/zukhruf/.github/actions/dokploy-deploy@main
|
|
741
|
+
with:
|
|
742
|
+
api-url: \${{ fromJSON(needs.infra.outputs.outputs).dokploy_api }}
|
|
743
|
+
api-key: \${{ secrets.DOKPLOY_API_KEY }}
|
|
744
|
+
compose-id: \${{ fromJSON(needs.infra.outputs.outputs).compose_id }}
|
|
745
|
+
|
|
746
|
+
# A new image must report this commit. A rollout without one restarts the
|
|
747
|
+
# old image, which reports the commit it was built from.
|
|
748
|
+
- name: Wait until the ${app.name} answers
|
|
749
|
+
uses: JanuaryLabs/zukhruf/.github/actions/wait-for-health@main
|
|
750
|
+
with:
|
|
751
|
+
url: https://\${{ fromJSON(needs.infra.outputs.outputs).host }}/api/healthz
|
|
752
|
+
sha: \${{ needs.promote.result == 'success' && env.SHA || '' }}
|
|
753
|
+
`;
|
|
754
|
+
}
|
|
755
|
+
//# sourceMappingURL=files.js.map
|