@zukhruf/nx 0.8.30 → 0.8.32

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