1+ name : Deploy to AWS ECS
2+
3+ on :
4+ push :
5+ branches : [main, production]
6+ pull_request :
7+ branches : [main]
8+
9+ env :
10+ AWS_REGION : us-east-1
11+ ECR_REPOSITORY : laravel-blog
12+ ECS_SERVICE : laravel-blog-service
13+ ECS_CLUSTER : laravel-blog-cluster
14+ ECS_TASK_DEFINITION : laravel-blog-task
15+ CONTAINER_NAME : laravel-app
16+
17+ jobs :
18+ test :
19+ runs-on : ubuntu-latest
20+
21+ services :
22+ mysql :
23+ image : mysql:8.0
24+ env :
25+ MYSQL_ROOT_PASSWORD : password
26+ MYSQL_DATABASE : laravel_test
27+ options : >-
28+ --health-cmd="mysqladmin ping"
29+ --health-interval=10s
30+ --health-timeout=5s
31+ --health-retries=3
32+ ports :
33+ - 3306:3306
34+
35+ redis :
36+ image : redis
37+ options : >-
38+ --health-cmd "redis-cli ping"
39+ --health-interval 10s
40+ --health-timeout 5s
41+ --health-retries 5
42+ ports :
43+ - 6379:6379
44+
45+ steps :
46+ - name : Checkout code
47+ uses : actions/checkout@v4
48+
49+ - name : Setup PHP
50+ uses : shivammathur/setup-php@v2
51+ with :
52+ php-version : ' 8.2'
53+ extensions : mbstring, dom, fileinfo, mysql, redis
54+
55+ - name : Install dependencies
56+ run : |
57+ composer install --prefer-dist --no-progress --no-suggest
58+ npm install
59+ npm run build
60+
61+ - name : Setup environment
62+ run : |
63+ cp .env.example .env
64+ php artisan key:generate
65+ php artisan config:cache
66+
67+ - name : Run tests
68+ env :
69+ DB_CONNECTION : mysql
70+ DB_HOST : 127.0.0.1
71+ DB_PORT : 3306
72+ DB_DATABASE : laravel_test
73+ DB_USERNAME : root
74+ DB_PASSWORD : password
75+ REDIS_HOST : 127.0.0.1
76+ REDIS_PORT : 6379
77+ run : |
78+ php artisan migrate --force
79+ php artisan test
80+
81+ deploy :
82+ name : Deploy to ECS
83+ runs-on : ubuntu-latest
84+ needs : test
85+ if : github.ref == 'refs/heads/main' || github.ref == 'refs/heads/production'
86+
87+ steps :
88+ - name : Checkout
89+ uses : actions/checkout@v4
90+
91+ - name : Configure AWS credentials
92+ uses : aws-actions/configure-aws-credentials@v4
93+ with :
94+ aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
95+ aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
96+ aws-region : ${{ env.AWS_REGION }}
97+
98+ - name : Login to Amazon ECR
99+ id : login-ecr
100+ uses : aws-actions/amazon-ecr-login@v2
101+
102+ - name : Build, tag, and push image to Amazon ECR
103+ id : build-image
104+ env :
105+ ECR_REGISTRY : ${{ steps.login-ecr.outputs.registry }}
106+ IMAGE_TAG : ${{ github.sha }}
107+ run : |
108+ # Build Docker image for production
109+ docker build -f docker/php/Dockerfile.prod -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
110+ docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
111+ echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
112+
113+ - name : Fill in the new image ID in the Amazon ECS task definition
114+ id : task-def
115+ uses : aws-actions/amazon-ecs-render-task-definition@v1
116+ with :
117+ task-definition : aws/task-definition.json
118+ container-name : ${{ env.CONTAINER_NAME }}
119+ image : ${{ steps.build-image.outputs.image }}
120+
121+ - name : Deploy Amazon ECS task definition
122+ uses : aws-actions/amazon-ecs-deploy-task-definition@v1
123+ with :
124+ task-definition : ${{ steps.task-def.outputs.task-definition }}
125+ service : ${{ env.ECS_SERVICE }}
126+ cluster : ${{ env.ECS_CLUSTER }}
127+ wait-for-service-stability : true
0 commit comments