• 高级YAML语法

高级YAML语法

锚点和别名

您可以在流水线配置中使用YAML锚点和别名作为变量.

要转换此内容:

pipeline:
  test:
    image: golang:1.18
    commands: go test ./...
  build:
    image: golang:1.18
    commands: build

只需添加一个名为variables的新部分,如下所示:

+variables:
+  - &golang_image 'golang:1.18'
 pipeline:
   test:
-    image: golang:1.18
+    image: *golang_image
     commands: go test ./...
   build:
-    image: golang:1.18
+    image: *golang_image
     commands: build

Map merges and overwrites

variables:
  - &base-plugin-settings
    target: dist
    recursive: false
    try: true
  - &special-setting
    special: true
  - &some-plugin codeberg.org/6543/docker-images/print_env

pipeline:
  develop:
    image: *some-plugin
    settings:
      <<: [*base-plugin-settings, *special-setting] # merge two maps into an empty map
    when:
      branch: develop

  main:
    image: *some-plugin
    settings:
      <<: *base-plugin-settings # merge one map and ...
      try: false # ... overwrite original value
      ongoing: false # ... adding a new value
    when:
      branch: main

Sequence merges

variables:
  pre_cmds: &pre_cmds
   - echo start
   - whoami
  post_cmds: &post_cmds
   - echo stop
  hello_cmd: &hello_cmd
   - echo hello

pipeline:
  step1:
    image: debian
    commands:
     - <<: *pre_cmds # prepend a sequence
     - echo exec step now do dedicated things
     - <<: *post_cmds # append a sequence
  step2:
    image: debian
    commands:
     - <<: [*pre_cmds, *hello_cmd] # prepend two sequences
     - echo echo from second step
     - <<: *post_cmds