|
| 1 | +name: Deploy Monitoring Stack |
| 2 | + |
| 3 | +# workflow_call: docker-publish.yml의 운영 환경 배포(release) 완료 후 자동 호출 |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + workflow_call: |
| 7 | + inputs: |
| 8 | + instance_id: |
| 9 | + type: string |
| 10 | + required: false |
| 11 | + |
| 12 | +jobs: |
| 13 | + deploy: |
| 14 | + name: Deploy to monitoring EC2 |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + id-token: write |
| 18 | + contents: read |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Configure AWS credentials |
| 22 | + uses: aws-actions/configure-aws-credentials@v4 |
| 23 | + with: |
| 24 | + role-to-assume: ${{ secrets.AWS_ROLE_ARN }} |
| 25 | + aws-region: ap-northeast-2 |
| 26 | + |
| 27 | + - name: Resolve instance ID |
| 28 | + id: instance |
| 29 | + run: | |
| 30 | + INSTANCE_ID="${{ inputs.instance_id }}" |
| 31 | + if [ -z "$INSTANCE_ID" ] || [ "$INSTANCE_ID" = "null" ]; then |
| 32 | + INSTANCE_ID=$(aws ec2 describe-instances \ |
| 33 | + --filters "Name=tag:Name,Values=bannote-prod-monitoring" "Name=instance-state-name,Values=running" \ |
| 34 | + --query "Reservations[0].Instances[0].InstanceId" \ |
| 35 | + --output text) |
| 36 | + fi |
| 37 | + echo "id=$INSTANCE_ID" >> $GITHUB_OUTPUT |
| 38 | +
|
| 39 | + - name: Pull latest code |
| 40 | + id: pull |
| 41 | + run: | |
| 42 | + COMMAND_ID=$(aws ssm send-command \ |
| 43 | + --instance-id ${{ steps.instance.outputs.id }} \ |
| 44 | + --document-name "AWS-RunShellScript" \ |
| 45 | + --parameters '{"commands":["sudo su - ec2-user -c \"cd /home/ec2-user/gsc-slack-app && git pull origin main\""]}' \ |
| 46 | + --query "Command.CommandId" \ |
| 47 | + --output text) |
| 48 | + echo "command_id=$COMMAND_ID" >> $GITHUB_OUTPUT |
| 49 | +
|
| 50 | + - name: Wait and get pull result |
| 51 | + run: | |
| 52 | + aws ssm wait command-executed \ |
| 53 | + --command-id ${{ steps.pull.outputs.command_id }} \ |
| 54 | + --instance-id ${{ steps.instance.outputs.id }} || true |
| 55 | + RESULT=$(aws ssm get-command-invocation \ |
| 56 | + --command-id ${{ steps.pull.outputs.command_id }} \ |
| 57 | + --instance-id ${{ steps.instance.outputs.id }} --output json) |
| 58 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); print(r['StandardOutputContent']); print(r['StandardErrorContent'])" |
| 59 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); exit(0 if r['Status']=='Success' else 1)" |
| 60 | +
|
| 61 | + - name: Write .env file |
| 62 | + id: env |
| 63 | + run: | |
| 64 | + COMMAND_ID=$(aws ssm send-command \ |
| 65 | + --instance-id ${{ steps.instance.outputs.id }} \ |
| 66 | + --document-name "AWS-RunShellScript" \ |
| 67 | + --parameters '{"commands":["GRAFANA_PASSWORD=$(aws secretsmanager get-secret-value --secret-id ${{ secrets.SECRETS_ARN }} --region ap-northeast-2 --query SecretString --output text | python3 -c \"import sys,json; print(json.load(sys.stdin)[\\\"GRAFANA_PASSWORD\\\"])\") && printf \"GRAFANA_PASSWORD=%s\\nPROMETHEUS_CONFIG=./monitoring/prometheus/prometheus.prod.yml\\n\" \"$GRAFANA_PASSWORD\" > /home/ec2-user/gsc-slack-app/.env && echo done"]}' \ |
| 68 | + --query "Command.CommandId" \ |
| 69 | + --output text) |
| 70 | + echo "command_id=$COMMAND_ID" >> $GITHUB_OUTPUT |
| 71 | +
|
| 72 | + - name: Wait and get env result |
| 73 | + run: | |
| 74 | + aws ssm wait command-executed \ |
| 75 | + --command-id ${{ steps.env.outputs.command_id }} \ |
| 76 | + --instance-id ${{ steps.instance.outputs.id }} || true |
| 77 | + RESULT=$(aws ssm get-command-invocation \ |
| 78 | + --command-id ${{ steps.env.outputs.command_id }} \ |
| 79 | + --instance-id ${{ steps.instance.outputs.id }} --output json) |
| 80 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); print(r['StandardOutputContent']); print(r['StandardErrorContent'])" |
| 81 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); exit(0 if r['Status']=='Success' else 1)" |
| 82 | +
|
| 83 | + - name: Ensure docker compose plugin |
| 84 | + id: setup |
| 85 | + run: | |
| 86 | + COMMAND_ID=$(aws ssm send-command \ |
| 87 | + --instance-id ${{ steps.instance.outputs.id }} \ |
| 88 | + --document-name "AWS-RunShellScript" \ |
| 89 | + --parameters '{"commands":["mkdir -p /usr/libexec/docker/cli-plugins && ln -sf /usr/local/bin/docker-compose /usr/libexec/docker/cli-plugins/docker-compose && docker compose version"]}' \ |
| 90 | + --query "Command.CommandId" \ |
| 91 | + --output text) |
| 92 | + echo "command_id=$COMMAND_ID" >> $GITHUB_OUTPUT |
| 93 | +
|
| 94 | + - name: Wait and get setup result |
| 95 | + run: | |
| 96 | + aws ssm wait command-executed \ |
| 97 | + --command-id ${{ steps.setup.outputs.command_id }} \ |
| 98 | + --instance-id ${{ steps.instance.outputs.id }} || true |
| 99 | + RESULT=$(aws ssm get-command-invocation \ |
| 100 | + --command-id ${{ steps.setup.outputs.command_id }} \ |
| 101 | + --instance-id ${{ steps.instance.outputs.id }} --output json) |
| 102 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); print(r['StandardOutputContent']); print(r['StandardErrorContent'])" |
| 103 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); exit(0 if r['Status']=='Success' else 1)" |
| 104 | +
|
| 105 | + - name: Deploy monitoring stack |
| 106 | + id: deploy |
| 107 | + run: | |
| 108 | + COMMAND_ID=$(aws ssm send-command \ |
| 109 | + --instance-id ${{ steps.instance.outputs.id }} \ |
| 110 | + --document-name "AWS-RunShellScript" \ |
| 111 | + --parameters '{"commands":["sudo su - ec2-user -c \"cd /home/ec2-user/gsc-slack-app && make monitoring\""]}' \ |
| 112 | + --query "Command.CommandId" \ |
| 113 | + --output text) |
| 114 | + echo "command_id=$COMMAND_ID" >> $GITHUB_OUTPUT |
| 115 | +
|
| 116 | + - name: Wait and get deploy result |
| 117 | + run: | |
| 118 | + aws ssm wait command-executed \ |
| 119 | + --command-id ${{ steps.deploy.outputs.command_id }} \ |
| 120 | + --instance-id ${{ steps.instance.outputs.id }} || true |
| 121 | + RESULT=$(aws ssm get-command-invocation \ |
| 122 | + --command-id ${{ steps.deploy.outputs.command_id }} \ |
| 123 | + --instance-id ${{ steps.instance.outputs.id }} --output json) |
| 124 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); print(r['StandardOutputContent']); print(r['StandardErrorContent'])" |
| 125 | + echo "$RESULT" | python3 -c "import sys,json; r=json.load(sys.stdin); exit(0 if r['Status']=='Success' else 1)" |
0 commit comments