LoginSignup
3
0

CircleCI + AWS(StepFunctions)でmigrationの成功を待つ

Posted at

経緯

  • CircleCIのupdate-serviceを使って、Fargateにデプロイするのですが、migrationを事前に実施したくなります。

実現方法

環境 内容
AWS migration用のタスク定義 + StepFunctionsのStateを作成 
CircleCI migration用のStepFunctionsを起動し、実行ステータスを監視

migration用のタスク定義

  • 基本的にはfargate(web)のタスク定義と同じです。

migrationを実行するStepFunctionsのstates

  • Railsだと以下のようにコマンドを書いておきます。
"Overrides": {
  "ContainerOverrides": [
    {
      "Name": "app",
      "Command": ["bundle", "exec", "rails", "db:migrate"]
    }
  ]
}

※ migration専用のState+CloudwatchLogのロググループを用意しておくと障害の時に探し易くなります。

CircleCI側で実行ステータスを監視

  • 以下のようなコマンドを用意して、jobの中で呼び出せば、migrationが完了した後に、fargateへのデプロイを実行できます。
commands:
  start_migration:
     parameters:
      state_machine: { type: string }            
    steps:
    - aws-cli/setup
    - run:
        name: Start State machine
        command: |
            EXECUTION_ARN=$(aws stepfunctions start-execution \
              --state-machine-arn arn:aws:states:${AWS_DEFAULT_REGION}:${AWS_ECR_REGISTRY_ID}:stateMachine:<< parameters.state_machine >>\
              | jq -r ".executionArn")

            STATUS="RUNNING"
            while [ "$STATUS" = "RUNNING" ]
            do
              echo "実行中..."
              sleep 10s

              STATUS=$(aws stepfunctions describe-execution --execution-arn ${EXECUTION_ARN} | jq -r ".status")
            done

            echo "STATUS=${STATUS}"
            if [ "${STATUS}" != "SUCCEEDED" ]; then
              exit 1
            fi

参考

3
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
3
0