LoginSignup
11
13

More than 5 years have passed since last update.

CircleCIでECSへRailsアプリをデプロイする

Posted at

だいたい基本が固まったので記録。

主に CircleCIのサンプルqiitaの記事を参考にした。

.circleci/config.yml
version: 2
jobs:
  checkout_code:
    docker:
      - image: circleci/ruby:2.4-node
    working_directory: ~/my_app_dir
    steps:
      - checkout
      - save_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - ~/my_app_dir

  bundle_dependencies:
    docker:
      - image: circleci/ruby:2.4-node
      - image: circleci/postgres:9.4.12-alpine
    working_directory: ~/my_app_dir
    steps:
      - restore_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1-bundle-{{ checksum "Gemfile.lock" }}
      - run: bundle install --path vendor/bundle
      - save_cache:
          key: v1-bundle-{{ checksum "Gemfile.lock" }}
          paths:
            - ~/my_app_dir/vendor/bundle
  test:
    docker:
      - image: circleci/ruby:2.4-node
        environment:
          DB_USER: db_user
          DB_PASSWORD: test
          DB_HOST: localhost
          DB_NAME: my_app_test_db
      - image: circleci/postgres:9.4.12-alpine
        environment:
          POSTGRES_USER: db_user
          POSTGRES_DB: my_app_test_db
          POSTGRES_PASSWORD: test
    working_directory: ~/my_app_dir
    steps:
      - restore_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1-bundle-{{ checksum "Gemfile.lock" }}
      - run: bundle --path vendor/bundle
      - run: bundle exec rake db:setup
      - run:
          name: Run tests
          command: bundle exec rake
  deploy:
    docker:
      - image: circleci/python
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: install aws
          command: |
            sudo pip install awscli
            aws --version
      - run:
          name: Install ecs-deploy
          command: |
            curl https://raw.githubusercontent.com/silinternational/ecs-deploy/master/ecs-deploy | sudo tee -a /usr/bin/ecs-deploy
            sudo chmod +x /usr/bin/ecs-deploy
      - run:
          name: Install jq
          command: sudo apt-get install -y jq
      - run:
          name: "Log in to AWS ECR"
          command: eval $(aws ecr get-login --no-include-email --region ap-northeast-1)
      - run:
          name: "Build & Push Docker Image"
          command: |
            docker build -t $AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/my_app_repo:latest -t $AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/my_app_repo:$CIRCLE_SHA1 .
            docker push $AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/my_app_repo:$CIRCLE_SHA1

      - run:
          name: "DB Migrate"
          command: |
            aws ecs run-task \
              --region ap-northeast-1 \
              --cluster my_ecs_cluster \
              --task-definition my_app_task_def \
              --overrides file://ecs/run_task_db_migrate.json
      - run:
          name: "Sevice deploy"
          environment:
          command: |
            ecs-deploy --cluster my_ecs_cluster --service_name my_app_service \
              --region ap-northeast-1 --timeout 240 \
              --image $AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/my_app_repo:$CIRCLE_SHA1

workflows:
  version: 2
  build_and_test_and_deploy:
    jobs:
      - checkout_code
      - bundle_dependencies:
          requires:
            - checkout_code
      - test:
          requires:
            - bundle_dependencies
      - deploy:
          requires:
            - test

以下をアプリに合わせて書き換える。

  • my_app_dir
  • testのところの環境変数まわり
  • リージョンをap-northeast-1としているところ
  • my_app_repo
  • my_ecs_cluster
  • my_app_service
  • my_app_task_def
11
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
13