LoginSignup
2
0

More than 1 year has passed since last update.

CircleCIでビルドして、AWS EC2にデプロイするために知りたかったこと

Last updated at Posted at 2022-06-14

push時に変更されたファイルによって、処理を分ける方法

path-filteringを利用する。

config.yml

path-filteringで変更されたファイルを判定して、build.ymlのparametersに渡す。

version: 2.1

setup: true

orbs:
  path-filtering: circleci/path-filtering@0.1.3

workflows:
  setup-workflow:
    jobs:
      - path-filtering/filter:
          base-revision: develop # 比較対象のブランチ
          config-path: .circleci/build.yml
          mapping: |
            aaa1/.* changed_aaa true
            aaa2/.* changed_aaa true

build.yml

config.yml で判定された結果をparametersで受け取って、処理を分岐させる。

parameters:
  changed_aaa:
    type: boolean
    default: false

jobs:
  build:
    build_aaa:
      executor:
        name: default
      steps:
        - when:
            condition: <<pipeline.parameters.changed_aaa>>
            steps:
              - run: echo "build"
        - unless:
            condition: <<pipeline.parameters.changed_main>>
            steps:
              - run: echo "not build"

workflows:
  build_and_deploy:
    jobs:
      - build_aaa:
          filters:
            branches:
              only:
                - develop

環境別(本番、ステージング等)の設定値を同じKeyで取得する。

Contextに設定すると同じKeyで取得できて便利。
https://app.circleci.com/settings/organization/github/VJSOL/contexts

AWS EC2のセキュリティグループにCircleCIの実行サーバーのipアドレスを追加、削除

参考にした記事:
CircleCI2.0からEC2にアクセスするときだけ特定のIPを許可したい

セキュリティグループにipを追加

commands:
  add_security_group_ssh_ip:

    description: "ec2のセキュリティグループにcircleciのipを追加。"
    steps:
      - aws-cli/install
      - run:
          name: "authorize-security-group-ingress"
          command: |
            IP=`curl -s ifconfig.me`
            echo "current executor ip:${IP}"
            echo "#!/bin/bash" > ./sg.sh
            echo "aws configure set region ap-northeast-1" >> ./sg.sh
            echo "aws ec2 authorize-security-group-ingress --group-id $EC2_SECURITY_GROUP_ID \
             --ip-permissions IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges='[{CidrIp=${IP}/32,Description="circle ci"}]'" >> ./sg.sh
            bash ./sg.sh

セキュリティグループからipを削除

orbs:
  aws-cli: circleci/aws-cli@1.3.2

commands:
    remove_security_group_ssh_ip:
      description: "ec2のセキュリティグループからcircleciのipを削除。"
      steps:
        - aws-cli/install
        - run:
            name: "revoke-security-group-ingress"
            command: |
              IP=`curl -s ifconfig.me`
              echo "current executor ip:${IP}"
              echo "#!/bin/bash" > ./sg.sh
              echo "aws configure set region ap-northeast-1" >> ./sg.sh
              echo "aws ec2 revoke-security-group-ingress --group-id $EC2_SECURITY_GROUP_ID \
               --ip-permissions IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges='[{CidrIp=${IP}/32,Description="circle ci"}]'" >> ./sg.sh
              bash ./sg.sh

SCPでファイルを送る。

ドキュメント:
CircleCI に SSH キーを登録する

commands:
    deploy_file:
      parameters:
        file_path:
          type: string
        deploy_target_path:
          type: string
        restart_command:
          type: string

      steps:
        - attach_workspace:
            at: ~/workspace
        # add_ssh_keys でcircle ciの管理画面で登録した秘密鍵を呼び出す。
        - add_ssh_keys:
            fingerprints: # circle ciの管理画面「SSH Keys」に設定したkeyのFingerprintを設定
              - "aaaaaaaaaaaaaaaaaaaaaaaaa"
              - "bbbbbbbbbbbbbbbbbbbbbbbbb"

        # SCPでデプロイ対象のサーバーにファイルを送信。
        - run: scp -o StrictHostKeyChecking=no << parameters.file_path >> $SSH_USERNAME@$TARGET_HOST:<<parameters.deploy_target_path>>

        # remoteのサーバーで再起動コマンドの実行
        - run: echo "ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$TARGET_HOST '<<parameters.restart_command>>'" > restart_command.sh;
        - run: bash ./restart_command.sh
2
0
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
2
0