Skip to content

Deploy configuration

Naomichi Yamakita edited this page Aug 18, 2024 · 88 revisions

You need to place config/deploy.yml in the repository to be deployed. In the configuration file, deploy settings related to services and Run task are configured.

  • If you want to know what parameters can be defined in the configuration file, check the JSON Schema.

Sample configuration

Auto deploy

To deploy automatically using GitHub Actions/Webhook, you need to set auto_deploy.

# config/deploy.yml
auto_deploy:
  # Specifying a branch supports full or partial matching. Specify '*' if you want all branches to be covered.
  # If you include an asterisk, the string must be enclosed in quotes.
  - branch: 'feature/*'

    # Defines the order in which deployments are executed.
    # The name to be specified for `resources` must be defined in `config/deploy.yml`.
    steps:
      - cluster: staging-app
        type: run_task
        resources:
          - db_migrate
      - cluster: staging-app
        type: service
        resources:
          - backend

        # If true, services are updated sequentially without waiting for the service replacement to complete (only for `type: service`).
        async_wait: true

      - cluster: staging-app
        type: scheduled_task
        resources:
          # Scheduled tasks are specified by separating the scheduling rule and target with a colon.
          - schedule_rule:target

Service

# config/deploy.yml
clusters:
  - name: production
    services:
      # Service name
      backend:
        containers:
          - name: nginx
            build:
              context: ..
              dockerfile: docker/nginx/Dockerfile

              # Build arguments (Optional)
              args:
                MYSQL_HOST: ***.***.***.***
                MYSQL_DATABASE: ***
                MYSQL_USER: ***

                # KMS-encrypted values can be compounded at build time.
                # (This feature has been deprecated. use `secret_args`.)
                MYSQL_PASSWORD: ${***}

              secret_args:
                # Secret parameters used at build time can also be obtained from the Parameter Store or the Secrets Manager.
                # https://github.com/metaps/genova/wiki/Encryption-of-environment-variables
                MYSQL_PASSWORD: {ARN or NAME}

          - name: rails
            build:
              context: ..
              dockerfile: docker/rails/Dockerfile
        path: ./deploy/production.yml

        # (Optional)
        desired_count: 1

        # (Optional)
        force_new_deployment: true

        # (Optional)
        minimum_healthy_percent: 100

        # (Optional)
        maximum_percent: 200

        # (Optional)
        health_check_grace_period_seconds: 300

        # (Optional)
        latest_submodule: true

Scheduled task

# config/deploy.yml
clusters:
  - name: production
    # Scheduled task configuration
    scheduled_tasks:
      - rule: sitemap_refresh
        expression: "cron(* * * * ? *)"

        # (Optional)
        enabled: true

        # (Optional)
        description: Greeting
        targets:
          - name: production
            # (Optional)
            depend_service: production

            containers:
              - name: container
                build:
                  context: ..
                  dockerfile: docker/rails/Dockerfile

            # Task definition path
            path: ./deploy/production.yml

            # "EC2" or "FARGATE" (Optional)
            launch_type: EC2

            # Required if network mode is awsvpc
            network_configuration:
              awsvpc_configuration:
                subnets:
                  - subnet-***

            # (Optional)
                security_groups:
                  - sg-***

            # (Optional)
               assign_public_ip: DISABLED

            # (Optional)
            desired_count: 1

            # (Optional)
            description: Greeting

            # Override task role (Optional)
            task_role: EcsTaskExecutionRole

            # CloudWatch Events IAM role (Optional)
            cloudwatch_event_iam_role: ecsEventsRole

            # (Optional)
            container_overrides:
            - name: app
              command:
                - "rake"
                - "sitemap:refresh"
              environment:
                - FOO: bar

            # (Optional)
            latest_submodule: true

Run task

# config/deploy.yml
clusters:
  # Cluster name
  - name: production
    run_tasks:
      # Task name
      greeting:
        containers:
          # Container name must match name of task definition.
          # https://github.com/metaps/genova/wiki/Task-definition
          - name: rails
            build:
              # Image build relative path 
              context: ..

              # Relative path of Dockerfile from context (Optional)
              dockerfile: docker/rails/Dockerfile
        # Task definition path
        path: ./deploy/production.yml

        # "EC2" or "FARGATE" (Optional)
        launch_type: EC2

        # (Optional)
        desired_count: 1

        # (Optional)
        group: greeting

        # Required if network mode is awsvpc
        network_configuration:
          awsvpc_configuration:
            subnets:
              - subnet-***

        # (Optional)
            security_groups:
              - sg-***

        # (Optional)
           assign_public_ip: DISABLED

     # (Optional)
        container_overrides:
          - name: rails
            command:
              - "echo"
              - "Hello!"

        # (Optional)
        latest_submodule: true

Tips

Override task definitions

When using a common task definition for multiple services or Scheduled tasks, you may want to change some parameters (e.g., memory size allocated to containers) depending on the task to be executed. In such cases, the task_overrides parameter can be used to override the task definition.

# config/deploy.yml
clusters:
  - name: production
    run_tasks:
      greeting:
        containers:
          - name: rails
            build:
              context: ..
        path: ./deploy/production.yml
        task_overrides:
          container_definitions:
            - name: rails
              memory: 2048

This setup uses the . /deploy/production.yml file, but overwrites the rails container with 2GiB of memory.

Reduce YAML code

In YAML, anchors and aliases can be used to reduce the number of lines of code.

# config/deploy.yml
clusters:
  scheduled_tasks:
    - rule: foo
      expression: "cron(0 0 * * ? *)"
      targets:
        - name: foo
          launch_type: FARGATE
          network_configuration: &network_configuration
            awsvpc_configuration:
              subnets:
                - ***
              security_groups:
                - ***
          ...
    - rule: bar
      expression: "cron(0 0 * * ? *)"
      targets:
        - name: bar
          launch_type: FARGATE
          network_configuration:
            <<: *network_configuration
          ...