dofigen 2.8.0

A Dockerfile generator using a simplified description in YAML or JSON format create
Documentation
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
use dofigen_lib::*;
use pretty_assertions_sorted::assert_eq_sorted;

#[test]
fn yaml_to_dockerfile_empty() {
    let yaml = "";

    let dofigen: Dofigen = DofigenContext::new()
        .parse_from_string(yaml)
        .map_err(Error::from)
        .unwrap();

    let mut generation_context = GenerationContext::from(dofigen);
    let dockerfile: String = generation_context.generate_dockerfile().unwrap();

    assert_eq_sorted!(
        dockerfile,
        r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# runtime
FROM scratch AS runtime
USER 1000:1000
"#
    );

    let dockerignore: String = generation_context.generate_dockerignore().unwrap();

    assert_eq_sorted!(
        dockerignore,
        "# This file is generated by Dofigen v0.0.0\n# See https://github.com/lenra-io/dofigen\n\n"
    );
}

#[test]
#[cfg(feature = "permissive")]
fn yaml_to_dockerfile_complex() {
    let yaml = r#"
builders:
  builder:
    fromImage: ekidd/rust-musl-builder
    user: 1000
    add: "."
    run:
    - ls -al
    - cargo build --release
    cache: /usr/local/cargo/registry
arg:
  TARGETPLATFORM: ""
  APP_NAME: template-rust
env:
  fprocess: /app
copy:
  - fromBuilder: builder
    paths: /home/rust/src/target/x86_64-unknown-linux-musl/release/${APP_NAME}
    target: /app
    chmod: "555"
  - fromImage: ghcr.io/openfaas/of-watchdog:0.9.6
    paths: /fwatchdog
    target: /fwatchdog
    chmod: 555
volume: /app/cache
expose: 8080
healthcheck:
  interval: 3s
  cmd: "[ -e /tmp/.lock ] || exit 1"
cmd: "/fwatchdog"
ignores:
  - target
  - test
        "#;

    let dofigen: Dofigen = DofigenContext::new()
        .parse_from_string(yaml)
        .map_err(Error::from)
        .unwrap();
    let mut generation_context = GenerationContext::from(dofigen);
    let dockerfile: String = generation_context.generate_dockerfile().unwrap();

    assert_eq_sorted!(
        dockerfile,
        r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# builder
FROM ekidd/rust-musl-builder AS builder
COPY \
    --chown=1000 \
    --link \
    "." "./"
USER 1000
RUN \
    --mount=type=cache,target=/usr/local/cargo/registry,uid=1000,sharing=locked \
    <<EOF
ls -al
cargo build --release
EOF

# runtime
FROM scratch AS runtime
ARG APP_NAME=template-rust
ARG TARGETPLATFORM
ENV fprocess="/app"
COPY \
    --from=builder \
    --chown=1000:1000 \
    --chmod=555 \
    --link \
    "/home/rust/src/target/x86_64-unknown-linux-musl/release/${APP_NAME}" "/app"
COPY \
    --from=ghcr.io/openfaas/of-watchdog:0.9.6 \
    --chown=1000:1000 \
    --chmod=555 \
    --link \
    "/fwatchdog" "/fwatchdog"
USER 1000:1000
VOLUME /app/cache
EXPOSE 8080
HEALTHCHECK \
    --interval=3s \
    CMD [ -e /tmp/.lock ] || exit 1
CMD ["/fwatchdog"]
"#
    );

    let dockerignore: String = generation_context.generate_dockerignore().unwrap();

    assert_eq_sorted!(
        dockerignore,
        "# This file is generated by Dofigen v0.0.0\n# See https://github.com/lenra-io/dofigen\n\ntarget\ntest\n"
    );

    assert_eq_sorted!(generation_context.get_lint_messages(), vec![]);
}

#[ignore = "Flatten enum variant alias problem: https://github.com/serde-rs/serde/issues/2188"]
#[test]
#[cfg(feature = "permissive")]
fn using_dockerfile_overlap_aliases() {
    use std::collections::HashMap;

    #[cfg(not(feature = "permissive"))]
    let yaml = r#"
builders:
  builder:
    image: 
      path: ekidd/rust-musl-builder
    adds:
      - paths:
        - "*"
    script:
      - cargo build --release
artifacts:
  - builder: builder
    source:
      - /home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust
    destination: /app
"#;

    #[cfg(feature = "permissive")]
    let yaml = r#"
builders:
  builder:
    image: ekidd/rust-musl-builder
    adds:
      - "*"
    script:
      - cargo build --release
artifacts:
- builder: builder
  source: /home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust
  destination: /app
"#;
    let dofigen: Dofigen = DofigenContext::new().parse_from_string(yaml).unwrap();
    assert_eq_sorted!(
        dofigen,
        Dofigen {
            builders: HashMap::from([(
                "builder".into(),
                Stage {
                    from: FromContext::FromImage(ImageName {
                        path: String::from("ekidd/rust-musl-builder"),
                        ..Default::default()
                    }),
                    copy: vec![CopyResource::Copy(Copy {
                        paths: vec![String::from("*")].into(),
                        ..Default::default()
                    })],
                    run: Run {
                        run: vec![String::from("cargo build --release")].into(),
                        ..Default::default()
                    },
                    ..Default::default()
                }
            )]),
            stage: Stage {
                copy: vec![CopyResource::Copy(Copy {
                    from: FromContext::FromBuilder(String::from("builder")),
                    paths: vec![String::from(
                        "/home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust"
                    )],
                    options: CopyOptions {
                        target: Some(String::from("/app")),
                        ..Default::default()
                    },
                    ..Default::default()
                })]
                .into(),
                ..Default::default()
            },
            ..Default::default()
        }
    );
}

#[test]
#[cfg(feature = "permissive")]
fn multiline_run_field() {
    let yaml = r#"
run:
  - |
    if [ "test" = "test" ]; then
      echo "Test"
    fi
"#;

    let dofigen: Dofigen = DofigenContext::new().parse_from_string(yaml).unwrap();
    let mut generation_context = GenerationContext::from(dofigen);
    let dockerfile: String = generation_context.generate_dockerfile().unwrap();

    assert_eq_sorted!(
        dockerfile,
        r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# runtime
FROM scratch AS runtime
USER 1000:1000
RUN <<EOF
if [ "test" = "test" ]; then
  echo "Test"
fi
EOF
"#
    );
}

#[ignore = "Not managed yet by serde: https://serde.rs/field-attrs.html#flatten"]
#[test]
#[cfg(feature = "permissive")]
fn combine_field_and_aliases() {
    #[cfg(not(feature = "permissive"))]
    let yaml = r#"
fromContext: 
  path: scratch
fromImage:
  path: alpine
"#;

    #[cfg(feature = "permissive")]
    let yaml = r#"
fromContext: scratch
fromImage: alpine
"#;
    let result = DofigenContext::new().parse_from_string(yaml);
    assert!(
        result.is_err(),
        "The parsing must fail since from and image are not compatible",
    );
}

#[ignore = "Not managed yet by serde: https://serde.rs/field-attrs.html#flatten"]
#[test]
#[cfg(feature = "permissive")]
fn fail_on_unknow_field() {
    #[cfg(not(feature = "permissive"))]
    let yaml = r#"
fromImage:
  path: alpine
test: Fake value
"#;

    #[cfg(feature = "permissive")]
    let yaml = r#"
fromImage: alpine
test: Fake value
"#;
    let result = DofigenContext::new().parse_from_string(yaml);
    assert!(
        result.is_err(),
        "The parsing must fail since 'test' is not a valid field"
    );

    // Check the error message
    let error = result.unwrap_err();
    let expected =
        "Error while deserializing the document at line 2, column 1: unknown field `test`";
    assert_eq_sorted!(
        &error.to_string().as_str()[..expected.len()],
        expected,
        "Wrong error message"
    );
}

#[test]
#[cfg(feature = "permissive")]
fn manage_plural_aliases() {
    #[cfg(not(feature = "permissive"))]
    let yaml = r#"
builders:
  builder:
    fromImage:
      path: ekidd/rust-musl-builder
    user:
      user: rust
    adds: 
      - paths:
          - "."
    run:
      - cargo build --release
    caches:
      - /usr/local/cargo/registry
envs:
  fprocess: /app
artifacts:
  - fromBuilder: builder
    source: /home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust
    target: /app
  - fromImage: 
      host: ghcr.io
      path: openfaas/of-watchdog
      tag: 0.9.6
    source: /fwatchdog
    target: /fwatchdog
ports:
  - port: 8080
ignore:
  - target
  - test
"#;

    #[cfg(feature = "permissive")]
    let yaml = r#"
builders:
  builder:
    fromImage: ekidd/rust-musl-builder
    user: rust
    adds: 
    - "."
    run:
    - cargo build --release
    caches:
    - /usr/local/cargo/registry
envs:
  fprocess: /app
artifacts:
  - fromBuilder: builder
    source: /home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust
    target: /app
  - fromImage: ghcr.io/openfaas/of-watchdog:0.9.6
    source: /fwatchdog
    target: /fwatchdog
ports:
  - 8080
ignore:
  - target
  - test
"#;

    let result = DofigenContext::new().parse_from_string(yaml);

    assert!(result.is_ok());
}

#[test]
#[cfg(feature = "permissive")]
fn artifact_copy_custom_user() {
    #[cfg(not(feature = "permissive"))]
    let yaml = r#"
builders:
  builder:
    fromImage:
      path: ekidd/rust-musl-builder
    user:
      user: rust
    copy:
      - paths: ["."]
    run:
      - cargo build --release
    cache:
      - /usr/local/cargo/registry
user:
  user: 1001
artifacts:
- fromBuilder: builder
  source: /home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust
  target: /app
run:
  - echo "coucou"
cache:
  - /tmp
"#;

    #[cfg(feature = "permissive")]
    let yaml = r#"
builders:
  builder:
    fromImage: ekidd/rust-musl-builder
    user: 1000
    add: "."
    run: cargo build --release
    cache: /usr/local/cargo/registry
user: 1001
artifacts:
- fromBuilder: builder
  source: /home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust
  target: /app
run: echo "coucou"
cache: /tmp
"#;

    let dofigen: Dofigen = DofigenContext::new().parse_from_string(yaml).unwrap();
    let mut generation_context = GenerationContext::from(dofigen);
    let dockerfile: String = generation_context.generate_dockerfile().unwrap();

    assert_eq_sorted!(
        dockerfile,
        r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# builder
FROM ekidd/rust-musl-builder AS builder
COPY \
    --chown=1000 \
    --link \
    "." "./"
USER 1000
RUN \
    --mount=type=cache,target=/usr/local/cargo/registry,uid=1000,sharing=locked \
    cargo build --release

# runtime
FROM scratch AS runtime
COPY \
    --from=builder \
    --chown=1001 \
    --link \
    "/home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust" "/app"
USER 1001
RUN \
    --mount=type=cache,target=/tmp,uid=1001,sharing=locked \
    echo "coucou"
"#
    );

    assert_eq_sorted!(generation_context.get_lint_messages(), vec![]);
}

#[test]
fn bind_instead_of_copy() {
    #[cfg(not(feature = "permissive"))]
    let yaml = r#"
builders:
  builder:
    fromImage:
      path: clux/muslrust
      tag: stable
    workdir: /app
    bind:
      - source: Cargo.toml
        target: Cargo.toml
      - source: Cargo.lock
        target: Cargo.lock
      - source: src/
        target: src/
    run:
      # Build with musl to work with scratch
      - cargo build --release
      # copy the generated binary outside of the target directory. If not the other stages won't be able to find it since it's in a cache volume
      - mv target/x86_64-unknown-linux-musl/release/dofigen /tmp/
    cache:
      # Cargo cache
      - target: /home/rust/.cargo
      # build cache
      - target: target
workdir: /app
copy:
  - fromBuilder: builder
    paths: 
      - "/tmp/dofigen"
    target: "/bin/"
entrypoint:
  - /bin/dofigen
cmd:
  - --help
context:
  - "/src"
  - "/Cargo.*"
"#;

    #[cfg(feature = "permissive")]
    let yaml = r#"
builders:
  builder: 
    fromImage: clux/muslrust:stable
    workdir: /app
    bind:
      - Cargo.toml
      - Cargo.lock
      - src/ src/
    run:
      # Build with musl to work with scratch
      - cargo build --release
      # copy the generated binary outside of the target directory. If not the other stages won't be able to find it since it's in a cache volume
      - mv target/x86_64-unknown-linux-musl/release/dofigen /tmp/
    cache:
      # Cargo cache
      - /home/rust/.cargo
      # build cache
      - target
workdir: /app
artifacts:
  - fromBuilder: builder
    source: "/tmp/dofigen"
    target: "/bin/"
entrypoint: /bin/dofigen
cmd: --help
context:
  - "/src"
  - "/Cargo.*"
"#;

    let dofigen: Dofigen = DofigenContext::new().parse_from_string(yaml).unwrap();
    let mut generation_context = GenerationContext::from(dofigen);
    let dockerfile: String = generation_context.generate_dockerfile().unwrap();

    assert_eq_sorted!(
        dockerfile,
        r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# builder
FROM clux/muslrust:stable AS builder
WORKDIR /app
RUN \
    --mount=type=bind,target=Cargo.toml,source=Cargo.toml \
    --mount=type=bind,target=Cargo.lock,source=Cargo.lock \
    --mount=type=bind,target=src/,source=src/ \
    --mount=type=cache,target=/home/rust/.cargo,sharing=locked \
    --mount=type=cache,target=target,sharing=locked \
    <<EOF
cargo build --release
mv target/x86_64-unknown-linux-musl/release/dofigen /tmp/
EOF

# runtime
FROM scratch AS runtime
WORKDIR /app
COPY \
    --from=builder \
    --chown=1000:1000 \
    --link \
    "/tmp/dofigen" "/bin/"
USER 1000:1000
ENTRYPOINT ["/bin/dofigen"]
CMD ["--help"]
"#
    );

    assert_eq_sorted!(generation_context.get_lint_messages(), vec![]);
}

#[ignore = "Not managed yet by serde because of multilevel flatten: https://serde.rs/field-attrs.html#flatten"]
#[test]
#[cfg(feature = "permissive")]
fn malformed_user_must_fail() {
    let yaml = r#"
user: wrong.user
"#;

    let result = DofigenContext::new().parse_from_string(yaml);
    assert!(
        result.is_err(),
        "The parsing must fail since user name is malformed",
    );
}

mod copy_file_content {
    use super::*;

    #[test]
    fn simple() {
        let yaml = r#"
fromImage:
  path: alpine
arg:
  NAME: World
copy:
  - content: echo "Hello ${NAME} !"
    target: /entrypoint.sh
entrypoint: [/entrypoint.sh]
"#;

        let dofigen: Dofigen = DofigenContext::new().parse_from_string(yaml).unwrap();
        let mut generation_context = GenerationContext::from(dofigen);
        let dockerfile: String = generation_context.generate_dockerfile().unwrap();

        assert_eq_sorted!(
            dockerfile,
            r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# runtime
FROM alpine AS runtime
ARG NAME=World
COPY \
    --chown=1000:1000 \
    --link \
    <<EOF /entrypoint.sh
echo "Hello ${NAME} !"
EOF
USER 1000:1000
ENTRYPOINT ["/entrypoint.sh"]
"#
        );

        assert_eq_sorted!(generation_context.get_lint_messages(), vec![]);
    }

    #[test]
    fn multiline() {
        let yaml = r#"
fromImage:
  path: alpine
arg:
  NAME: World
copy:
  - content: |
      #!/bin/sh
      echo "Hello ${NAME} !"
      echo "Welcome !"
    target: /entrypoint.sh
entrypoint: [/entrypoint.sh]
"#;

        let dofigen: Dofigen = DofigenContext::new().parse_from_string(yaml).unwrap();
        let mut generation_context = GenerationContext::from(dofigen);
        let dockerfile: String = generation_context.generate_dockerfile().unwrap();

        assert_eq_sorted!(
            dockerfile,
            r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# runtime
FROM alpine AS runtime
ARG NAME=World
COPY \
    --chown=1000:1000 \
    --link \
    <<EOF /entrypoint.sh
#!/bin/sh
echo "Hello ${NAME} !"
echo "Welcome !"

EOF
USER 1000:1000
ENTRYPOINT ["/entrypoint.sh"]
"#
        );

        assert_eq_sorted!(generation_context.get_lint_messages(), vec![]);
    }

    #[test]
    fn no_arg_substitution() {
        let yaml = r#"
fromImage:
  path: alpine
arg:
  NAME: World
copy:
  - content: echo "Hello ${NAME} !"
    substitute: false
    target: /entrypoint.sh
entrypoint: [/entrypoint.sh]
"#;

        let dofigen: Dofigen = DofigenContext::new().parse_from_string(yaml).unwrap();
        let mut generation_context = GenerationContext::from(dofigen);
        let dockerfile: String = generation_context.generate_dockerfile().unwrap();

        assert_eq_sorted!(
            dockerfile,
            r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# runtime
FROM alpine AS runtime
ARG NAME=World
COPY \
    --chown=1000:1000 \
    --link \
    <<"EOF" /entrypoint.sh
echo "Hello ${NAME} !"
EOF
USER 1000:1000
ENTRYPOINT ["/entrypoint.sh"]
"#
        );

        assert_eq_sorted!(generation_context.get_lint_messages(), vec![]);
    }
}

#[test]
fn shells_in_dockerfile() {
    let yaml = r#"
shell: 
  - bash
  - "-c"
run:
  - echo "Hello World !"
"#;
    let dofigen: Dofigen = DofigenContext::new()
        .parse_from_string(yaml)
        .map_err(Error::from)
        .unwrap();
    let mut generation_context = GenerationContext::from(dofigen);
    let dockerfile: String = generation_context.generate_dockerfile().unwrap();

    assert_eq_sorted!(
        dockerfile,
        r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# runtime
FROM scratch AS runtime
USER 1000:1000
SHELL ["bash", "-c"]
RUN echo "Hello World !"
"#
    );
}

#[test]
fn builders_dependencies() {
    let yaml = r#"
builders:
  install:
    fromImage:
      path: oven/bun
    workdir: /app/
    # Copy project
    bind:
      - source: package.json
        target: package.json
    # Install dependencies
    run:
      - bun install --production  --frozen-lockfile --verbose
    cache:
      - target: /root/.bun/install/cache
  build:
    fromImage:
      path: oven/bun
    workdir: /app/apps/workflows
    copy:
      - paths:
          - .
        target: /app/
      - fromBuilder: install
        paths:
          - /app/node_modules
        target: /app/node_modules
    env:
      NODE_ENV: production
    run:
      - bun build --compile --target bun  --sourcemap --format=cjs src/index.ts --outfile=app --external '@libsql/*' --external libsql

  docker:
    fromImage:
      path: oven/bun
    workdir: /app/apps/workflows
    copy:
      - paths:
          - .
        target: /app/
    run:
      - bun run src/build-docker.ts

  libsql:
    fromImage:
      path: oven/bun
    workdir: /app/
    copy:
      - fromBuilder: docker
        paths:
          - /app/apps/build-docker/package.json
        target: /app/package.json
    run:
      - bun install

  ca-certs:
    fromImage: 
      path: debian
      tag: bullseye-slim
    run:
      - apt update && apt install -y ca-certificates && update-ca-certificates

fromImage:
  path: debian
  tag: bullseye-slim
workdir: /app/
copy:
  - fromBuilder: build
    paths:
      - /app/apps/workflows/app
    target: /app/apps/workflows/
    chmod: "555"
  - fromBuilder: libsql
    paths:
      - /app/node_modules
    target: /app/packages/db/node_modules
  - fromBuilder: libsql
    paths:
      - /app/node_modules
    target: /app/node_modules
  - fromBuilder: ca-certs
    paths:
      - /etc/ssl/certs/ca-certificates.crt
    target: /etc/ssl/certs/
expose:
  - port: 3000
entrypoint:
  - /app/apps/workflows/app
"#;
    let dofigen: Dofigen = DofigenContext::new()
        .parse_from_string(yaml)
        .map_err(Error::from)
        .unwrap();

    let mut generation_context = GenerationContext::from(dofigen);
    let dockerfile: String = generation_context.generate_dockerfile().unwrap();

    assert_eq_sorted!(
        dockerfile,
        r#"# syntax=docker/dockerfile:1.19.0
# This file is generated by Dofigen v0.0.0
# See https://github.com/lenra-io/dofigen

# ca-certs
FROM debian:bullseye-slim AS ca-certs
RUN apt update && apt install -y ca-certificates && update-ca-certificates

# docker
FROM oven/bun AS docker
WORKDIR /app/apps/workflows
COPY \
    --link \
    "." "/app/"
RUN bun run src/build-docker.ts

# install
FROM oven/bun AS install
WORKDIR /app/
RUN \
    --mount=type=bind,target=package.json,source=package.json \
    --mount=type=cache,target=/root/.bun/install/cache,sharing=locked \
    bun install --production  --frozen-lockfile --verbose

# build
FROM oven/bun AS build
ENV NODE_ENV="production"
WORKDIR /app/apps/workflows
COPY \
    --link \
    "." "/app/"
COPY \
    --from=install \
    --link \
    "/app/node_modules" "/app/node_modules"
RUN bun build --compile --target bun  --sourcemap --format=cjs src/index.ts --outfile=app --external '@libsql/*' --external libsql

# libsql
FROM oven/bun AS libsql
WORKDIR /app/
COPY \
    --from=docker \
    --link \
    "/app/apps/build-docker/package.json" "/app/package.json"
RUN bun install

# runtime
FROM debian:bullseye-slim AS runtime
WORKDIR /app/
COPY \
    --from=build \
    --chown=1000:1000 \
    --chmod=555 \
    --link \
    "/app/apps/workflows/app" "/app/apps/workflows/"
COPY \
    --from=libsql \
    --chown=1000:1000 \
    --link \
    "/app/node_modules" "/app/packages/db/node_modules"
COPY \
    --from=libsql \
    --chown=1000:1000 \
    --link \
    "/app/node_modules" "/app/node_modules"
COPY \
    --from=ca-certs \
    --chown=1000:1000 \
    --link \
    "/etc/ssl/certs/ca-certificates.crt" "/etc/ssl/certs/"
USER 1000:1000
EXPOSE 3000
ENTRYPOINT ["/app/apps/workflows/app"]
"#
    );
}