💭

YAML入門

2025/04/06に公開

YAML入門

コメントの記載方法

  • 先頭行に"# "。

YAMLのデータ型について

データ型 備考
scalar 数字や文字列、真偽値などの基本的な値。
sequence 配列・リストのこと。要素の集合。
dictionary(mapping) キーと値のコレクション。
  • リスト
# リストの記載例
fruits:
  - apple
  - banana
  - grape
  - kiwi
---
 

* キーと値のペアの記載例
```yaml
name: ジョン
age: 21
  • ネストされたリスト
    リストの中にオブジェクトを含めることもできる。
people:
  - name: ジョン
    age: 21
  - name: アリス
    age: 23
  • 特殊文字について
特殊文字 扱い方
{ } [ ] & * # ? | - < > = ! @ : ` , 引用符または二重引用符で囲む
  • 階層構造の表現方法
person:
  name: 人物1
  age: 30

注意点

- インデントはスペースで行い、タブは使用しないこと。
- キーは一意でなければならず、同じキーが複数回出現することはできない。
- 値には文字列、数値、リスト、オブジェクトなどが使用できる。

YAMLの記載例

# サーバー設定
server:
  host: localhost
  port: 8080

# データベース設定
database:
  type: mysql
  host: db.example.com
  port: 3306
  username: user
  password: pass

# サポートされている言語
supported_languages:
  - 日本語
  - 英語

この例のYAMLをJSONにした場合

{
  "server": {
    "host": "localhost",
    "port": 8080
  },
  "database": {
    "type": "mysql",
    "host": "db.example.com",
    "port": 3306,
    "username": "user",
    "password": "pass"
  },
  "supported_languages": [
    "日本語",
    "英語"
  ]
}

# アプリケーション設定
app:
  name: MyApp
  version: 1.0.0
  environment: production

# サーバー設定
server:
  host: localhost
  port: 8080
  ssl: true
  timeout: 30  # 秒

# データベース設定
database:
  type: postgresql
  host: db.example.com
  port: 5432
  username: db_user
  password: db_pass
  database_name: my_database
  options:
    sslmode: require
    connection_timeout: 10

# ユーザー情報
users:
  - id: 1
    name: 人物1
    email: jhon.one@example.com
    roles:
      - admin
      - user
  - id: 2
    name: 人物2
    email: jhon.two@example.com
    roles:
      - user

# ログ設定
logging:
  level: info
  file: /var/log/myapp.log
  rotation:
    enabled: true
    max_size: 10MB
    max_age: 30  # 日数

この例のYAMLをJSONにした場合

{
  "app": {
    "name": "MyApp",
    "version": "1.0.0",
    "environment": "production"
  },
  "server": {
    "host": "localhost",
    "port": 8080,
    "ssl": true,
    "timeout": 30
  },
  "database": {
    "type": "postgresql",
    "host": "db.example.com",
    "port": 5432,
    "username": "db_user",
    "password": "db_pass",
    "database_name": "my_database",
    "options": {
      "sslmode": "require",
      "connection_timeout": 10
    }
  },
  "users": [
    {
      "id": 1,
      "name": "人物1",
      "email": "john.one@example.com",
      "roles": [
        "admin",
        "user"
      ]
    },
    {
      "id": 2,
      "name": "人物2",
      "email": "john.two@example.com",
      "roles": [
        "user"
      ]
    }
  ],
  "logging": {
    "level": "info",
    "file": "/var/log/myapp.log",
    "rotation": {
      "enabled": true,
      "max_size": "10MB",
      "max_age": 30
    }
  }
}

libraries:
  - library:
      name: 市立図書館
      location: 東京都
      books:
        - title: プログラミング入門
          author: 山田 太郎
          published_year: 2020
          genres:
            - 教育
            - プログラミング
          available_copies: 5
        - title: データサイエンスの基礎
          author: 佐藤 花子
          published_year: 2021
          genres:
            - 教育
            - データサイエンス
          available_copies: 3
        - title: 歴史の探求
          author: 鈴木 一郎
          published_year: 2019
          genres:
            - 歴史
            - ノンフィクション
          available_copies: 2
  - library:
      name: 都立図書館
      location: 東京都
      books:
        - title: AIの未来
          author: 田中 次郎
          published_year: 2022
          genres:
            - 科学
            - テクノロジー
          available_copies: 4
        - title: 日本の歴史
          author: 中村 美咲
          published_year: 2018
          genres:
            - 歴史
            - 教育
          available_copies: 6
        - title: 環境問題の解決
          author: 鈴木 三郎
          published_year: 2020
          genres:
            - 環境
            - 社会
          available_copies: 1

この例のYAMLをJSONにした場合

{
  "libraries": [
    {
      "library": {
        "name": "市立図書館",
        "location": "東京都",
        "books": [
          {
            "title": "プログラミング入門",
            "author": "山田 太郎",
            "published_year": 2020,
            "genres": ["教育", "プログラミング"],
            "available_copies": 5
          },
          {
            "title": "データサイエンスの基礎",
            "author": "佐藤 花子",
            "published_year": 2021,
            "genres": ["教育", "データサイエンス"],
            "available_copies": 3
          },
          {
            "title": "歴史の探求",
            "author": "鈴木 一郎",
            "published_year": 2019,
            "genres": ["歴史", "ノンフィクション"],
            "available_copies": 2
          }
        ]
      }
    },
    {
      "library": {
        "name": "都立図書館",
        "location": "東京都",
        "books": [
          {
            "title": "AIの未来",
            "author": "田中 次郎",
            "published_year": 2022,
            "genres": ["科学", "テクノロジー"],
            "available_copies": 4
          },
          {
            "title": "日本の歴史",
            "author": "中村 美咲",
            "published_year": 2018,
            "genres": ["歴史", "教育"],
            "available_copies": 6
          },
          {
            "title": "環境問題の解決",
            "author": "鈴木 三郎",
            "published_year": 2020,
            "genres": ["環境", "社会"],
            "available_copies": 1
          }
        ]
      }
    }
  ]
}

DockerHub

github、github actions、dockerの連携例

name: Java CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: '11'
    - name: Build with Gradle
      run: ./gradlew build
FROM openjdk:11-jre-slim
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- name: Build and push Docker image
  uses: docker/build-push-action@v2
  with:
    context: .
    push: true
    tags: user/repo:latest

Discussion