1
0
Login
Code
Issues
Pull requests
Events
Packages
029aa306ea21ac7a03529bfe91d09000c595a30d
Name
Commit
Updated at
README.md
README

You Don't Know CNB YAML

不知道您是否有用疑问,当您监听的事件越来越多:

  • 您的 .cnb.yml 越来越长
  • 您的 .cnb.yml 越来越繁琐
  • 您总是复制一大坨相同的内容

此时您不尽感叹道【我可不想当 YAML 工程师】。

.cnb.yml 代码提示和语法校验

配置在 VSCode 和 Jetbrains 种配置 JSON Schema

YAML 的复用

这里将介绍 YAML 的锚点(Anchors)和别名(Aliases),来帮助我们编写出可复用的 YAML 问题提升 YAML 的可维护性。

在阅读下面文章前,必须提前推荐两个 YAML 在线解析工具(两个工具无区别),帮助我们识别使用锚点后我们的 YAML 是否符合预期:

场景一: 一次 master push 事件分别在 amd 和 arm 中构建

优化前:

master:
  push:
    - name: amd
      runner:
        tags: cnb:arch:amd64
      services:
        - docker
      stages:
        - name: uname
          script: uname -a
        - name: hello world
          script: echo "hello world"
    - name: arm
      runner:
        tags: cnb:arch:arm64:v8
      services:
        - docker
      stages:
        - name: uname
          script: uname -a
        - name: hello world
          script: echo "hello world"

优化后:

# 锚点推荐 . 开头避免被识别成 branch 和 tag 名称
.build_script: &build_script
  - name: uname
    script: uname -a
  - name: hello world
    script: echo "hello world"

master:
  push:
    - name: amd
      runner:
        tags: cnb:arch:amd64
      services:
        - docker
      stages:
        *build_script
    - name: arm
      runner:
        tags: cnb:arch:arm64:v8
      services:
        - docker
      stages:
        *build_script

我们可以将优化有的 YAML 放入到 YAML 在线解析网站种进行查看确认是否符合预期。

场景二: master 分支和 develop 分支需要使用不同的环境变量

优化前:

master:
  push:
    - name: amd
      runner:
        tags: cnb:arch:amd64
      services:
        - docker
      env:
        SSH_USERNAME: username
        SSH_PASSWORD: password
        SSH_IP: production_ip
      stages:
        - name: uname
          script: uname -a
        - name: echo ssh ip
          script: echo $SSH_IP
develop:
  push:
    - name: amd
      runner:
        tags: cnb:arch:amd64
      services:
        - docker
      env:
        SSH_USERNAME: username
        SSH_PASSWORD: password
        SSH_IP: test_ip
      stages:
        - name: uname
          script: uname -a
        - name: echo ssh ip
          script: echo $SSH_IP

优化后:

# 锚点推荐 . 开头避免被识别成 branch 和 tag 名称
.build_script: &build_script
  - name: uname
    script: uname -a
  - name: echo ssh ip
    script: echo $SSH_IP

.common_env: &common_env
  SSH_USERNAME: username
  SSH_PASSWORD: password
  SSH_IP: production_ip


master:
  push:
    - name: amd
      runner:
        tags: cnb:arch:amd64
      services:
        - docker
      env:
        *common_env
      stages:
        *build_script
    - name: amd
      runner:
        tags: cnb:arch:amd64
      services:
        - docker
      env:
        *common_env
      stages:
        <<: *build_script
develop:
  push:
    - name: amd
      runner:
        tags: cnb:arch:amd64
      services:
        - docker
      env:
        <<: *common_env
        SSH_IP: test_ip
      stages:
        *build_script

About

No description, topics, or website provided.

Release
0

Tag
0
Language
Markdown99.5%
gitignore0.5%