Skip to content

Merge pull request #35 from next-engineer/dev #21

Merge pull request #35 from next-engineer/dev

Merge pull request #35 from next-engineer/dev #21

Workflow file for this run

name: Deploy to Amazon ECR & EC2
on:
push:
branches:
- main
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: intune-chat-repo
CONTAINER_NAME: intune-chat
EC2_INSTANCE_IDS: "i-08100c71ecc821618,i-07ee03b6f1f176e41"
DOCKER_RUN_ARGS: >-
-p 8081:8081
--restart unless-stopped
--log-driver=awslogs
--log-opt awslogs-region=ap-northeast-2
--log-opt awslogs-group=intune-chat-logs
--log-opt awslogs-stream=$(hostname)
S3_BUCKET: intune-deploy-bucket
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- id: login-ecr
uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a
# 1) 이미지 빌드 및 푸시
- id: build-image
run: |
bash scripts/build_and_push.sh \
"${{ steps.login-ecr.outputs.registry }}" \
"${{ env.ECR_REPOSITORY }}" \
"${{ github.sha }}"
# 2) 로그 그룹 보장 (30일 보관)
- name: Ensure CW log group with 30-day retention
run: |
aws logs create-log-group \
--log-group-name intune-chat-logs \
--region ${{ env.AWS_REGION }} 2>/dev/null || true
aws logs put-retention-policy \
--log-group-name intune-chat-logs \
--retention-in-days 30 \
--region ${{ env.AWS_REGION }}
# 3) 스크립트 업로드
- id: upload-script
run: |
aws s3 cp scripts/deploy_container.sh \
"s3://${{ env.S3_BUCKET }}/deploy_container.sh" \
--region "${{ env.AWS_REGION }}"
# 4) SSM으로 EC2에 배포
- id: ssm-deploy
run: |
IMAGE_URI=${{ steps.build-image.outputs.image }}
TARGETS_JSON=$(jq -n --arg ids "${{ env.EC2_INSTANCE_IDS }}" '
{Key:"InstanceIds", Values: ($ids | split(","))}
' | jq -s .)
aws ssm send-command \
--document-name "AWS-RunShellScript" \
--comment "Deploy new container" \
--targets "$TARGETS_JSON" \
--parameters '{
"commands": [
"aws s3 cp s3://${{ env.S3_BUCKET }}/deploy_container.sh /home/ec2-user/deploy_container.sh --region ap-northeast-2",
"chmod +x /home/ec2-user/deploy_container.sh",
"bash /home/ec2-user/deploy_container.sh ap-northeast-2 ${{ steps.login-ecr.outputs.registry }} '"$IMAGE_URI"' intune-chat \"${{ env.DOCKER_RUN_ARGS }}\""
]
}' \
--region "${{ env.AWS_REGION }}" \
--output json > send-command.json
COMMAND_ID=$(jq -r '.Command.CommandId' send-command.json)
echo "command-id=$COMMAND_ID" >> "$GITHUB_OUTPUT"
# 5) SSM 결과 대기
- run: |
bash scripts/wait_ssm.sh \
"${{ env.AWS_REGION }}" \
"${{ steps.ssm-deploy.outputs.command-id }}"