Deploying with GitHub Actions
Automate build, test, and SSH deployment steps in a GitHub Actions workflow.
Deploying with GitHub Actions is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is GitHub Actions?
GitHub Actions is a CI/CD platform built into GitHub. Workflows defined in .github/workflows/*.yml automatically run on push, pull request, or schedule events.
Basic Workflow Structure
A workflow consists of one or more jobs, each with steps.
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.3"
- name: Install dependencies
run: composer install --no-dev --optimize-autoloader
- name: Run tests
run: vendor/bin/phpunitSSH Deployment Step
Deploy to a server via SSH after tests pass.
- name: Deploy to server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /srv/myapp
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
sudo systemctl reload php8.3-fpmGitHub Secrets
Store sensitive values (SSH keys, API tokens, database passwords) as encrypted repository secrets in Settings → Secrets and Variables.
Caching Dependencies
Cache Composer packages between runs to speed up workflows.
- name: Cache Composer packages
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-composer-${{ hashFiles("composer.lock") }}Running Tests
Include a MySQL or PostgreSQL service container for integration tests.
services:
mysql:
image: mysql:8
env:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: secret
ports: [3306:3306]Environment Variables in Actions
Pass secrets as environment variables to workflow steps.
- name: Run tests
env:
DB_HOST: 127.0.0.1
DB_DATABASE: test
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: vendor/bin/phpunitCode Quality Gates
Block deployments if code quality checks fail.
- name: PHPStan static analysis
run: vendor/bin/phpstan analyse src --level=6
- name: PHP_CodeSniffer
run: vendor/bin/phpcs src --standard=PSR12Matrix Testing
Test against multiple PHP versions simultaneously.
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php: ["8.2", "8.3"]
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}Deployment Strategies
Common approaches: direct git pull (simple), rsync (fast file sync), blue-green (zero downtime), Deployer PHP (automated zero-downtime). Choose based on downtime tolerance.
Summary
Define workflows in .github/workflows/*.yml. Run tests on every push, deploy on main branch merges. Cache dependencies, store secrets in GitHub Secrets, and add quality gates before deployment.
Quick Check
Where are sensitive values like SSH keys stored for GitHub Actions?
Frequently asked questions
Is the “Deploying with GitHub Actions” lesson free?
Yes — the full text of “Deploying with GitHub Actions” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Deploying with GitHub Actions”?
Automate build, test, and SSH deployment steps in a GitHub Actions workflow. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Deploying with GitHub Actions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nginx and PHP-FPM Configuration
- Environment Variables and .env Files
- Deploying with GitHub Actions
- Dockerizing a PHP Application