Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

travis-yaml

> 编程语言
Open source

parses, normalizes, validates and serializes your .travis.yml

171 stars0 likes1 views
WebsiteGitHub

About

parses, normalizes, validates and serializes your .travis.yml

This repository is deprecated and was replaced by travis-yml

Travis Configuration Parser

What is this?

This project is a library for loading Travis CI build configuration.

It can create a configuration both from a normal Ruby Hash or a YAML string. These config objects generally behave like normal primitives (hashes, arrays, etc).

ruby
require 'travis/yaml'

config = Travis::Yaml.parse('language: ruby')
config = Travis::Yaml.parse(language: 'ruby')

config[:language] # ruby
config.language   # ruby

# using parse! instead of parse will print out warnings
Travis::Yaml.parse! deploy: []
# .travis.yml: missing key "language", defaulting to "ruby"
# .travis.yml: value for "deploy" section is empty, dropping

# have it generate the build matrix for you
Travis::Yaml.matrix('rvm: [jruby, 2.0.0]').each do |matrix_entry|
  puts matrix_entry.ruby
end

Why use it?

  • Prevents code execution. Instead of deserializing arbitrary Ruby objects, it only deserializes primitive objects. To go even further, in contrast to SafeYAML, it only deserializes primitive objects that are expected for a certain part of the configuration.
  • Prevents memory leaks. Internally, only expected values are stored in the data structure, discarding any additional data. No user input is converted to symbols (which would allow a memory based DoS attack).
  • Normalization is happening in one place. Travis CI currently does config normalization in many different parts of its infrastructure, making it tedious to determine supported input formats and the resulting internal structure.
  • Explicit structure. Due to the explicit configuration structure, exceptions throughout the system can be greatly reduced, since the configuration will not contain unexpected objects.
  • Forgiving about user input. The parser knows the expected structure and can therefore automatically map some malformed input onto that structure. For instance, node: 1.10 does not get converted to node: 1.1 internally, because travis-yaml knows to expect a string here rather than a float.
  • Built-in support for encrypted values, making it easier to support these in different parts of the system and never leak the decrypted form if configuration is being serialized.
  • Extensive warning and error handling, making it possible to use this library for linting and also to display such warnings when running a build.
  • Performance. Using travis-yaml to load a configuration from a YAML string is slightly faster than using psych directly and significantly faster than using safeyaml.
  • Compatibility. This library is written in a way to not only be fully compatible with the current .travis.yml format, but also with the configuration data in the current Travis database and with the code interacting with the configuration data (for instance in travis-build).
  • Extensibility. The configuration structure is easily extended.
  • Pluggable parser. It is easy to write a new parser. This would be useful for instance for the Travis CI command line client, where one could imagine a parser for the small subset of the YAML format that is commonly in use. This would for instance allow to write a parser and serializer able to preserve comments and indentation when modifying the contents of the .travis.yml.

What is missing?

  • Serialization

External API

Loading a config

ruby
config = Travis::Yaml.parse('language: ruby') # parse from a yaml string
config = Travis::Yaml.parse(language: "ruby") # parse from Ruby object

For Psych/YAML compatibility, parse is also aliased to load.

Convenience

Nodes generally behave like normal Ruby objects. Mappings accept both symbols and strings as keys. Known fields on mappings are exposed as methods.

ruby
puts config.language
puts config[:language]
puts config['language']

Warnings and errors

  • errors are actual parse errors, parent elements should discard elements with errors.
  • warnings are general warnings for an element, with the element still being usable. These do not include warnings for child elements (though an error in a child element becomes a warning for its immediate parent).
  • nested warnings include warnings for the whole tree, they also come with key prefix (array of strings) to identify the position the error occured at.
ruby
Travis::Yaml.parse("foo: bar").nested_warnings.each do |key, warning|
  puts "#{key.join('.')}: #{warning}"
end

# will print nested warnings to stderr, will raise on top level error
Travis::Yaml.parse! "foo: bar"

Secure Variables

Secure variables are stored as Travis::Yaml::SecureString internally. A secure string has at least an encrypted_string or a decrypted_string, or both.

You can use decrypt/encrypt with a block to generate the missing string:

ruby
secret = Travis::Yaml::SecureString.new("foo")

secret.encrypted_string # => "foo"
secret.decrypted_string # => nil
secret.encrypted?       # => true
secret.decrypted?       # => false

secret.decrypt { |string| string.upcase }

secret.encrypted_string # => "foo"
secret.decrypted_string # => "FOO"
secret.encrypted?       # => true
secret.decrypted?       # => true

To avoid having to walk the whole structure manually or hardcoding the values to decrypt, these methods are also exposed on any node:

ruby
config = Travis::Yaml.load 'env: { secure: foo }'
config.decrypted? # => false

config.decrypt { |string| string.upcase }
config.decrypted?                        # => true
config.env.matrix.first.decrypted_string # => "FOO"

This can even be done right with the parse step:

ruby
content = File.read('.travis.yml')
Travis::Yaml.parse! content do |config|
  config.decrypt { |string| string.upcase }
end

Serialization

A travis-yaml document can be serialized to a few other formats via the serialize method:

ruby
pp   config.serialize(:ruby)
puts config.serialize(:json, pretty: true)
Serializer Descriptions Options
ruby Corresponding Ruby objects, secure values will be SecureStrings secure, symbol_keys
legacy Format compatible with Travis CI's old fetch_config service secure, symbol_keys
json Serialize as JSON, parsable via Travis::Yaml.load secure, pretty
yaml Serialize as YAML, parsable via Travis::Yaml.load secure, symbol_keys, indentation, line_width, canonical, avoid_tags

The secure option can be set to :decrypted or :encrypted, enforcing the decrypted or encrypted form of secure strings to be serialized. In some serializations, this might lead to secure strings being mapped to normal strings if set to :decrypted.

Defining Structure

Good starting points for getting into the code are the root node and the language node.

A parsed configuration is very similar to a syntax tree. To create a new node type, you should inherit from one of the abstract types. Internal vocabulary is taken from the YAML spec rather than the Ruby names (ie, sequence vs array, mapping vs hash).

Scalar Values

Most of the time, scalar values are just strings. In fact, if you create a new scalar node class and don't specify and other supported type, it will treat everything as string.

ruby
module Travis::Yaml::Nodes
  class Example  2.0 (part of the stdlib).

Issues· 50 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言