-
Notifications
You must be signed in to change notification settings - Fork 58
CI CD Workflow
Darkk-kami edited this page Jul 21, 2024
·
2 revisions
This Section outlines the setup and configuration of GitHub Actions for the project.
Before setting up GitHub Actions, ensure you have the following:
- A Laravel project hosted on GitHub.
- Access to a server for deployment.
- SSH access set up on the server.
- Required secrets stored in GitHub (APP_KEY, SSH_HOST, SSH_USER, SSH_KEY).
The workflow is defined in a YAML file located in the .github/workflows directory of the repository. Each workflow consists of one or more jobs that can run in parallel or sequentially, and each job consists of a series of steps. This is a sample file created seperately in the dev, staging and production branch that triggers on any change to be automatically tested and deployed
name: Laravel CI <branch-name>
on:
push:
branches:
- <branch-name>
pull_request:
branches:
- <branch-name>
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: sqlite, zip, bcmath
- name: Install PHP dependencies
run: composer install --no-interaction --prefer-dist --optimize-autoloader
- name: Set environment variables for CI
run: |
cp .env.example .env
echo "APP_KEY=${{ secrets.APP_KEY }}" >> .env
echo "DB_CONNECTION=sqlite" >> .env
echo "DB_DATABASE=$(pwd)/database/database.sqlite" >> .env
- name: Ensure SQLite database file exists
run: |
mkdir -p database
touch database/database.sqlite
- name: Run database migrations
run: php artisan migrate --force
- name: Clear configuration cache
run: php artisan config:clear
- name: Generate JWT secret
run: php artisan jwt:secret
- name: Run Laravel tests
run: php artisan test
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
echo "About to Deploy"
cd /var/www/langlearnai-be/<server-enviroment>
eval $(ssh-agent)
ssh-add ~/.ssh/id_ecdsa
git pull origin <branch-name>
# exclude the --no-dev flag for dev deployment
composer install --no-dev
php artisan migrate --force
php artisan jwt:secret
- Trigger Events: The workflow triggers on push events to the branch
- Runs-on: Specifies the type of runner to use, here we use ubuntu-latest.
- Steps:
-
Checkout code: Uses the
actions/checkout@v2
action to pull the latest code from the repository. -
Set up PHP: Uses
shivammathur/setup-php@v2
to set up the PHP environment with the required version and extensions. -
Install PHP dependencies: Runs
composer install
to install the necessary dependencies for the application. -
Set environment variables: Creates a
.env
file from.env.example
and appends the necessary environment variables - Ensure SQLite database file exists: Creates the SQLite database file if it doesnt already exist
- Run Database Migrations: Run php artisan migrate --force to apply database migrations.
- Clear Configuration Cache: Clear any cached configurations to ensure the application runs with the latest configuration.
-
Generate JWT secret: Generates a new JWT secret key and sets it in your
.env
file. (Important if you are including JWT authentication in your Laravel application.)
- Run Laravel tests: This step runs the Laravel test suite using php artisan test, which will execute all the defined tests in the Laravel application to ensure that the code changes do not break any existing functionality.
-
Deploy to server: This step uses the
appleboy/ssh-action@mster
to perform the deployment process. It connects to the server via SSH using the specified credentials stored in GitHub Secrets. Once connected, it navigates to the deployment directory, pulls the latest code from the specified branch, installs dependencies using Composer, and runs database migrations to ensure the latest database schema is applied.
Made with ❤️ by Olat-nji | Ujusophy | tulbadex | Darkk-kami | Otie16 courtesy of @HNG-Internship