diff --git a/.github/workflows/deploy-monitoring.yml b/.github/workflows/deploy-monitoring.yml new file mode 100644 index 0000000..a4a6107 --- /dev/null +++ b/.github/workflows/deploy-monitoring.yml @@ -0,0 +1,58 @@ +name: Deploy Monitoring To EC2 +on: + push: + branches: + - main + - deploy +# paths: +# - "docker-compose.yml" +# - "prometheus/**" +# - "grafana/**" +# - ".github/workflows/deploy-monitoring.yml" +# - "deploy/**" + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: GitHub Repository 가져오기 + uses: actions/checkout@v4 + + - name: AWS Resource에 접근할 수 있게 AWS credentials 설정 + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-region: ap-northeast-2 + aws-access-key-id: ${{secrets.AWS_ACCESS_KEY}} + aws-secret-access-key: ${{secrets.AWS_SECRET_KEY}} + + - name: .env 파일 생성 + run: | + cat > .env << EOF + RUN_UID=${{secrets.RUN_UID}} + RUN_GID=${{secrets.RUN_GID}} + GRAFANA_ADMIN_USER=${{secrets.GRAFANA_ADMIN_USER}} + GRAFANA_ADMIN_PASSWORD=${{secrets.GRAFANA_ADMIN_PASSWORD}} + EOF + + - name: 압축하기 + run: | + cp deploy/monitoring/appspec.yml ./appspec.yml + cp -r deploy/monitoring/scripts ./scripts + tar -czvf $GITHUB_SHA.tar.gz \ + appspec.yml \ + .env \ + scripts \ + docker-compose.yml \ + prometheus \ + grafana + + - name: S3에 프로젝트 폴더 업로드 하기 + run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.tar.gz s3://mopl-deploy-4/monitoring/$GITHUB_SHA.tar.gz + + - name: CodeDeploy 배포 트리거 + run: | + aws deploy create-deployment \ + --application-name mopl-monitoring \ + --deployment-config-name CodeDeployDefault.AllAtOnce \ + --deployment-group-name mopl-deploy-group-monitoring \ + --s3-location bucket=mopl-deploy-4,key=monitoring/$GITHUB_SHA.tar.gz,bundleType=tgz diff --git a/README.md b/README.md index ef946f8..005f556 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# monitoring \ No newline at end of file +# monitoring + diff --git a/deploy/monitoring/appspec.yml b/deploy/monitoring/appspec.yml new file mode 100644 index 0000000..4083f55 --- /dev/null +++ b/deploy/monitoring/appspec.yml @@ -0,0 +1,17 @@ +version: 0.0 +os: linux +files: + - source: / + destination: /home/ubuntu/monitoring +file_exists_behavior: OVERWRITE + +permissions: + - object: / + owner: ubuntu + group: ubuntu + +hooks: + ApplicationStart: + - location: scripts/starts.sh + timeout: 120 + runas: ubuntu diff --git a/deploy/monitoring/scripts/starts.sh b/deploy/monitoring/scripts/starts.sh new file mode 100644 index 0000000..d3672f7 --- /dev/null +++ b/deploy/monitoring/scripts/starts.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +echo "--------------- monitoring 시작 -----------------" + +cd /home/ubuntu/monitoring + +# 기존 컨테이너 중지 및 삭제 +docker compose down --remove-orphans 2>/dev/null || true + +# data 디렉토리 생성 (소유권 설정) +mkdir -p data/prometheus data/grafana + +# 환경 변수에서 UID/GID 읽기 +source .env + +# prometheus 데이터 디렉토리 권한 설정 +chown -R ${RUN_UID}:${RUN_GID} data/prometheus + +# grafana 데이터 디렉토리 권한 설정 (grafana는 472:472 고정) +chown -R 472:472 data/grafana + +# docker-compose로 컨테이너 시작 +docker compose up -d + +echo "--------------- monitoring 끝 ------------------" + +# 컨테이너 상태 확인 +sleep 5 +docker ps | grep monitoring