cicdtest5 #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy to AWS EC2 | ||
|
Check failure on line 1 in .github/workflows/deploy.yml
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| - production | ||
| workflow_dispatch: | ||
| env: | ||
| AWS_REGION: ap-northeast-2 | ||
| ECR_REPOSITORY: my-app | ||
| EC2_HOST: ${{ secrets.EC2_HOST }} | ||
| EC2_USER: ubuntu | ||
| jobs: | ||
| deploy: | ||
| name: Build and Deploy to EC2 | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@v4 | ||
| with: | ||
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
| aws-region: ${{ env.AWS_REGION }} | ||
| - name: Login to Amazon ECR | ||
| id: login-ecr | ||
| uses: aws-actions/amazon-ecr-login@v2 | ||
| - name: Build, tag, and push image to Amazon ECR | ||
| id: build-image | ||
| env: | ||
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
| IMAGE_TAG: ${{ github.sha }} | ||
| run: | | ||
| docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | ||
| docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest | ||
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | ||
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest | ||
| echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT | ||
| - name: Deploy to EC2 | ||
| env: | ||
| PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | ||
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
| IMAGE_URI: ${{ steps.build-image.outputs.image }} | ||
| AWS_REGION: ${{ env.AWS_REGION }} | ||
| run: | | ||
| echo "$PRIVATE_KEY" > private_key.pem | ||
| chmod 600 private_key.pem | ||
| ssh -o StrictHostKeyChecking=no \ | ||
| -i private_key.pem \ | ||
| $EC2_USER@$EC2_HOST \ | ||
| "AWS_REGION=$AWS_REGION ECR_REGISTRY=$ECR_REGISTRY IMAGE_URI=$IMAGE_URI bash -s" << 'EOF' | ||
| aws ecr get-login-password --region $AWS_REGION | \ | ||
| docker login --username AWS --password-stdin $ECR_REGISTRY | ||
| docker stop my-app 2>/dev/null || true | ||
| docker rm my-app 2>/dev/null || true | ||
| docker pull $IMAGE_URI | ||
| docker run -d \ | ||
| --name my-app \ | ||
| --restart unless-stopped \ | ||
| -p 80:8000 \ | ||
| $IMAGE_URI | ||
| docker image prune -af | ||
| EOF | ||
| rm -f private_key.pem | ||
| - name: Verify deployment | ||
| run: | | ||
| echo "🚀 Deployment completed!" | ||
| echo "🔍 Checking application health..." | ||
| sleep 15 | ||
| for i in {1..5}; do | ||
| if curl -f http://${{ env.EC2_HOST }}/health; then | ||
| echo "✅ Application is healthy!" | ||
| exit 0 | ||
| fi | ||
| echo "⏳ Attempt $i/5 failed, retrying..." | ||
| sleep 10 | ||
| done | ||
| echo "❌ Health check failed" | ||
| exit 1 | ||