Name | Updated at | |
---|---|---|
README.md |
不知道您是否有用疑问,当您监听的事件越来越多:
此时您不尽感叹道【我可不想当 YAML 工程师】。
配置在 VSCode 和 Jetbrains 种配置 JSON Schema
这里将介绍 YAML 的锚点(Anchors)和别名(Aliases),来帮助我们编写出可复用的 YAML 问题提升 YAML 的可维护性。
在阅读下面文章前,必须提前推荐两个 YAML 在线解析工具(两个工具无区别),帮助我们识别使用锚点后我们的 YAML 是否符合预期:
优化前:
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:
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