Time Saving Yaml Tricks

DRY: Don’t Repeat Yourself If you have a section that’s being repeated multiple times, you can create an anchor and then reference it. You use &name to create an anchor, and then *name to reference it. One example of it is defining environment variables for different services in docker-compose.yaml. env_vars: &env ENVIRONMENT: production LOGLEVEL: info AWS_REGION_NAME: us-east-1 services: api: environment: *env ... ... backend: ... ... environment: *env Reading environment variables While yaml doesn’t read environment variables, but if you pair it with parser, you can read the environment variables. The syntax depends on the parser you are using. The most common places, where I have a need for reading environment variables (or context variables) in yaml file is, cicd pipeline namely github action or circleci and docker-compose. ...

February 12, 2025 · 2 min · 293 words · Mohit Sharma