#4 マルチAZ対応していないインフラを構築する #40
This file contains 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: Laravel | |
on: | |
pull_request: | |
branches: [ "main" ] | |
push: | |
branches: [ "main" ] | |
env: | |
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} | |
TASK_DEFINITION: ${{ vars.TASK_DEFINITION }} | |
ECS_CLUSTER: ${{ vars.ECS_CLUSTER }} | |
ECS_SERVICE: ${{ vars.ECS_SERVICE }} | |
PJ_PREFIX: ${{ vars.PJ_PREFIX }} | |
AWS_REGION: ${{ vars.AWS_REGION }} | |
permissions: | |
contents: read | |
jobs: | |
phpunit: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ./src | |
services: | |
mysql: | |
image: mysql:8.0 | |
env: | |
MYSQL_USER: user | |
MYSQL_PASSWORD: password | |
MYSQL_DATABASE: laravel | |
MYSQL_ROOT_PASSWORD: password | |
DB_PORT: ${{ job.services.mysql.ports[3306] }} | |
ports: | |
- 33306:3306 | |
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 | |
steps: | |
- uses: actions/checkout@v1 | |
with: | |
fetch-depth: 1 | |
- name: Verify MySQL connection | |
run: | | |
mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -u user -ppassword -e "SHOW DATABASES" | |
- name: Install dependencies | |
run: | | |
php --version | |
composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist | |
chmod -R 777 storage bootstrap/cache | |
- name: Boot Laravel application | |
run: | | |
php -r "file_exists('.env') || copy('.env.example', '.env');" | |
php artisan key:generate | |
php artisan --version | |
- name: Setup Node.js | |
uses: actions/setup-node@v2 | |
with: | |
node-version: '18' | |
- name: Install Node.js dependencies | |
run: npm install | |
- name: Build Vite assets | |
run: npm run build | |
- name: Execute PHPUnit tests | |
env: | |
APP_ENV: testing | |
DB_CONNECTION: mysql | |
DB_HOST: 127.0.0.1 | |
DB_DATABASE: laravel | |
DB_PORT: ${{ job.services.mysql.ports['3306'] }} | |
DB_USER: user | |
DB_PASSWORD: password | |
run: | | |
php artisan migrate | |
./vendor/bin/phpunit ./tests | |
building: | |
if: github.event_name == 'push' | |
needs: phpunit | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Build, tag, and push image to Amazon ECR | |
id: build-and-push-image | |
run: | | |
# Nginx | |
docker build --platform=linux/amd64 \ | |
--build-arg AWS_ACCOUNT_ID=${{ env.AWS_ACCOUNT_ID }} \ | |
--build-arg PJPrefix=${{ env.PJ_PREFIX }} \ | |
-t ${{ env.ECR_REGISTRY }}:latest \ | |
-f ./docker/build/nginx/Dockerfile . | |
docker push ${{ env.ECR_REGISTRY }}:latest | |
- name: Deploy Amazon ECS task definition | |
run: | |
aws ecs update-service --cluster ${{ env.ECS_CLUSTER }} --service ${{ env.ECS_SERVICE }} --task-definition ${{ env.TASK_DEFINITION }} --force-new-deployment |