From ddd916456ce4c41d642e9ec0e58ff26f418f6646 Mon Sep 17 00:00:00 2001 From: neojw1505 <70019047+neojw1505@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:56:49 +0800 Subject: [PATCH 1/6] chore(ci): update Laravel CI/CD workflow --- .github/workflows/backend.yaml | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml index 606310b..04acdd3 100644 --- a/.github/workflows/backend.yaml +++ b/.github/workflows/backend.yaml @@ -3,15 +3,17 @@ name: Laravel Backend CI on: push: branches: - - dev_wfh - - main + - ci-cd-backend + # - dev_wfh + # - main paths: - 'backend-api/**' pull_request: branches: - - dev_wfh - - main + - ci-cd-backend + # - dev_wfh + # - main paths: - 'backend-api/**' @@ -33,23 +35,17 @@ jobs: run: | echo "${{ secrets.ENV_CONTENT }}" > .env echo "${{ secrets.ENV_TESTING_CONTENT }}" > .env.testing - - name: Test print content of env (REMOVE in PROD) - run: | - cat .env + - name: Install Dependencies working-directory: backend-api run: composer install --no-ansi --no-interaction --no-progress --prefer-dist - - name: Clear Config Cache - working-directory: backend-api - run: php artisan config:clear - - name: Run Laravel Tests working-directory: backend-api run: php artisan test deploy: - needs: laravel-tests # Deploy only if tests pass + needs: laravel-tests # Only run if tests pass runs-on: ubuntu-latest steps: From 0377d9a33c5a4685a4324e639b1a8a7b3b36e2ae Mon Sep 17 00:00:00 2001 From: neojw1505 <70019047+neojw1505@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:57:47 +0800 Subject: [PATCH 2/6] test: validate on push trigger backend CI/CD --- backend-api/test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 backend-api/test.txt diff --git a/backend-api/test.txt b/backend-api/test.txt deleted file mode 100644 index e69de29..0000000 From 2358bc34684c8e6bbe04a46d3f314d873939c89b Mon Sep 17 00:00:00 2001 From: neojw1505 <70019047+neojw1505@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:13:16 +0800 Subject: [PATCH 3/6] Update backend.yaml --- .github/workflows/backend.yaml | 96 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml index 04acdd3..860b0ed 100644 --- a/.github/workflows/backend.yaml +++ b/.github/workflows/backend.yaml @@ -7,7 +7,7 @@ on: # - dev_wfh # - main paths: - - 'backend-api/**' + - "backend-api/**" pull_request: branches: @@ -15,68 +15,68 @@ on: # - dev_wfh # - main paths: - - 'backend-api/**' + - "backend-api/**" jobs: + # run unit test cases laravel-tests: runs-on: ubuntu-latest steps: - - name: Checkout Code - uses: actions/checkout@v4 + - name: Checkout Code + uses: actions/checkout@v4 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.2" - - name: Create .env and .env.testing Files from Secrets - working-directory: backend-api - run: | - echo "${{ secrets.ENV_CONTENT }}" > .env - echo "${{ secrets.ENV_TESTING_CONTENT }}" > .env.testing + - name: Create .env and .env.testing Files from Secrets + working-directory: backend-api + run: | + echo "${{ secrets.ENV_CONTENT }}" > .env + echo "${{ secrets.ENV_TESTING_CONTENT }}" > .env.testing - - name: Install Dependencies - working-directory: backend-api - run: composer install --no-ansi --no-interaction --no-progress --prefer-dist + - name: Install Dependencies + working-directory: backend-api + run: composer install --no-ansi --no-interaction --no-progress --prefer-dist - - name: Run Laravel Tests - working-directory: backend-api - run: php artisan test + - name: Run Laravel Tests + working-directory: backend-api + run: php artisan test --parallel + # copy files to EC2 instances after passing test cases deploy: - needs: laravel-tests # Only run if tests pass + needs: laravel-tests # Only run if tests pass runs-on: ubuntu-latest steps: - - name: Checkout Code - uses: actions/checkout@v4 + - name: Checkout Code + uses: actions/checkout@v4 - - name: Set up SSH Access - env: - EC2_KEY: ${{ secrets.EC2_KEY }} - run: | - mkdir -p ~/.ssh - echo "$EC2_KEY" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa + - name: Set up SSH Access + run: | + mkdir -p ~/.ssh + echo "${{ secrets.EC2_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa - - name: Copy Files to EC2 - run: | - scp -r ./backend-api ec2-user@54.251.20.155:/var/www/html/spm-project/backend-api + - name: Copy Files to EC2 + run: | + scp -o StrictHostKeyChecking=no -r ./backend-api ec2-user@54.251.20.155:/var/www/html/spm-project/backend-api - - name: Run Laravel Commands on EC2 - run: | - ssh -o StrictHostKeyChecking=no ec2-user@54.251.20.155 << 'EOF' - cd /var/www/html/spm-project/backend-api - if [ -f composer.lock ]; then - composer install --no-dev --optimize-autoloader - fi - php artisan migrate --force - php artisan config:clear - php artisan cache:clear - php artisan route:clear - sudo chmod -R 775 storage bootstrap/cache - sudo chown -R apache:apache /var/www/html/spm-project/backend-api - export APP_ENV=production - sudo systemctl restart httpd - EOF + - name: Run Laravel Commands on EC2 + run: | + ssh -o StrictHostKeyChecking=no ec2-user@54.251.20.155 << 'EOF' + cd /var/www/html/spm-project/backend-api + if [ -f composer.lock ]; then + composer install --no-dev --optimize-autoloader + fi + php artisan migrate --force + php artisan config:clear + php artisan cache:clear + php artisan route:clear + sudo chmod -R 775 storage bootstrap/cache + sudo chown -R apache:apache /var/www/html/spm-project/backend-api + export APP_ENV=production + sudo systemctl restart httpd + EOF From cc28a50d474752f415650516ef7307a5525e7e4d Mon Sep 17 00:00:00 2001 From: neojw1505 <70019047+neojw1505@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:13:54 +0800 Subject: [PATCH 4/6] Create test.txt --- backend-api/test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 backend-api/test.txt diff --git a/backend-api/test.txt b/backend-api/test.txt new file mode 100644 index 0000000..e69de29 From b45246853f75358efc127f4da6754883763d330b Mon Sep 17 00:00:00 2001 From: neojw1505 <70019047+neojw1505@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:16:28 +0800 Subject: [PATCH 5/6] Update backend.yaml --- .github/workflows/backend.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml index 860b0ed..0612094 100644 --- a/.github/workflows/backend.yaml +++ b/.github/workflows/backend.yaml @@ -43,7 +43,7 @@ jobs: - name: Run Laravel Tests working-directory: backend-api - run: php artisan test --parallel + run: php artisan test # copy files to EC2 instances after passing test cases deploy: From 67a96fc8e97b9f4af08b815e95a1d81180ee748a Mon Sep 17 00:00:00 2001 From: neojw1505 <70019047+neojw1505@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:17:14 +0800 Subject: [PATCH 6/6] Update test.txt --- backend-api/test.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/backend-api/test.txt b/backend-api/test.txt index e69de29..30d74d2 100644 --- a/backend-api/test.txt +++ b/backend-api/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file