GitHub Actions Pipelines
Complete Magento Build Pipeline
# .github/workflows/build.yml
name: Magento Build
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
env:
PHP_VERSION: '8.1'
COMPOSER_CACHE_DIR: ~/.composer/cache
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHP_VERSION }}
extensions: mbstring, intl, gd, xsl, redis, pdo_mysql
coverage: none
- name: Cache Composer
uses: actions/cache@v3
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install --prefer-dist --no-progress --no-interaction
- name: Static Analysis
run: |
vendor/bin/phpstan analyse --level=6 app/code/Vendor/
vendor/bin/phpcs --standard=PSR12 app/code/Vendor/
- name: Compile Code
run: bin/magento setup:di:compile
- name: Deploy Static Content
run: bin/magento setup:static-content:deploy -f --theme=Vendor/Theme
- name: Create Build Artifact
run: |
tar -czf magento-build.tar.gz \
app/code/Vendor/ \
pub/static/ \
generated/ \
var/di/
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: magento-build
path: magento-build.tar.gz
retention-days: 7
Reusable Workflow
# .github/workflows/reusable-build.yml
name: Reusable Build
on:
workflow_call:
inputs:
php-version:
required: false
type: string
default: '8.1'
secrets:
COMPOSER_TOKEN:
required: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.php-version }}
- run: composer install --prefer-dist
Key Takeaway
GitHub Actions uses YAML workflows with jobs and steps. Cache dependencies, compile code, deploy static content, and upload artifacts for deployment.
GitLab CI Pipelines
GitLab CI Configuration
# .gitlab-ci.yml
stages:
- build
- test
- package
- deploy
variables:
PHP_VERSION: '8.1'
COMPOSER_CACHE_DIR: $CI_PROJECT_DIR/.composer-cache
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- .composer-cache/
- vendor/
build:
stage: build
image: php:${PHP_VERSION}-cli
script:
- composer install --prefer-dist --no-progress
- bin/magento setup:di:compile
- bin/magento setup:static-content:deploy -f
artifacts:
paths:
- pub/static/
- generated/
- var/di/
expire_in: 1 day
rules:
- if: $CI_COMMIT_BRANCH == "main"
- if: $CI_COMMIT_BRANCH == "develop"
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
test:
stage: test
image: php:${PHP_VERSION}-cli
services:
- mysql:8.0
- redis:6
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: magento_test
script:
- composer install --prefer-dist
- bin/magento setup:install --db-name=$MYSQL_DATABASE --db-user=root --db-password=$MYSQL_ROOT_PASSWORD
- vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist
coverage: '/Lines:\s*(\d+\.?\d*)%/'
package:
stage: package
image: alpine:latest
script:
- tar -czf magento-package.tar.gz app/code/Vendor/ pub/static/ generated/
artifacts:
paths:
- magento-package.tar.gz
expire_in: 7 days
only:
- main
deploy-staging:
stage: deploy
image: alpine:latest
script:
- scp magento-package.tar.gz deployer@staging:/var/www/
- ssh deployer@staging "cd /var/www && tar -xzf magento-package.tar.gz && bin/magento cache:flush"
environment:
name: staging
url: https://staging.example.com
only:
- main
Key Takeaway
GitLab CI uses stages, jobs, services, and artifacts. Cache between jobs, use services for databases, and artifacts to pass build outputs between stages.
Jenkins Pipelines
Jenkinsfile for Magento
// Jenkinsfile
pipeline {
agent any
environment {
PHP_VERSION = '8.1'
COMPOSER_HOME = "${WORKSPACE}/.composer"
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Setup PHP') {
steps {
sh 'phpenv local ${PHP_VERSION}'
sh 'composer install --prefer-dist --no-interaction'
}
}
stage('Static Analysis') {
parallel {
stage('PHPStan') {
steps {
sh 'vendor/bin/phpstan analyse --level=6 app/code/Vendor/'
}
}
stage('PHPCS') {
steps {
sh 'vendor/bin/phpcs --standard=PSR12 app/code/Vendor/'
}
}
}
}
stage('Build') {
steps {
sh 'bin/magento setup:di:compile'
sh 'bin/magento setup:static-content:deploy -f'
}
}
stage('Test') {
steps {
sh 'vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist'
}
post {
always {
junit 'test-results.xml'
}
}
}
stage('Package') {
steps {
sh 'tar -czf magento-build.tar.gz app/code/Vendor/ pub/static/ generated/'
archiveArtifacts artifacts: 'magento-build.tar.gz'
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
sshagent(['deploy-key']) {
sh 'scp magento-build.tar.gz deployer@production:/var/www/'
sh 'ssh deployer@production "cd /var/www && tar -xzf magento-build.tar.gz"'
}
}
}
}
post {
always {
cleanWs()
}
failure {
mail to: 'team@example.com',
subject: "Build Failed: ${currentBuild.fullDisplayName}",
body: "Check: ${env.BUILD_URL}"
}
}
}
Key Takeaway
Jenkins uses Groovy-based Jenkinsfile with declarative or scripted syntax. Supports parallel stages, when conditions, post actions, and artifact archiving.
Pipeline Optimization
Caching Strategies
# GitHub Actions cache
- uses: actions/cache@v3
with:
path: |
~/.composer/cache
vendor/
node_modules/
key: ${{ runner.os }}-deps-${{ hashFiles('**/composer.lock', '**/package-lock.json') }}
restore-keys: ${{ runner.os }}-deps-
Dependency Caching
# GitLab CI cache
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- vendor/
- node_modules/
- .composer-cache/
policy: pull-push
Pipeline Artifacts
# Pass build outputs between jobs
artifacts:
paths:
- pub/static/
- generated/
- var/di/
expire_in: 1 day
when: on_success
Conditional Execution
# Only run on specific branches
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: always
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: always
- when: never
Build Matrix
# Test multiple PHP versions
strategy:
matrix:
php-version: ['8.1', '8.2', '8.3']
test-suite: [unit, integration]
fail-fast: false
Performance Optimization
# Shallow clone
- uses: actions/checkout@v3
with:
fetch-depth: 1
# Parallel jobs
jobs:
lint:
runs-on: ubuntu-latest
unit:
runs-on: ubuntu-latest
integration:
runs-on: ubuntu-latest
# All run in parallel
Key Takeaway
Optimize pipelines with dependency caching, artifact passing, conditional execution, matrix builds, shallow clones, and parallel jobs.
Quiz
1. What does actions/cache do in GitHub Actions?
2. What is the purpose of artifacts in CI?
3. What does fail-fast: false do in matrix builds?
4. Why use shallow clone in CI?
5. What is the benefit of reusable workflows?
Flashcards
Question
What are pipeline stages?
Click to reveal answer
Answer
Sequential phases in a build: build -> test -> package -> deploy
Question
What do artifacts do?
Click to reveal answer
Answer
Pass build outputs between pipeline jobs for deployment
Question
How to cache dependencies?
Click to reveal answer
Answer
Use actions/cache (GitHub) or cache key/paths (GitLab) for vendor/ and composer cache
Question
What is a matrix build?
Click to reveal answer
Answer
Test multiple configurations (PHP versions, test suites) in parallel
Question
What is a reusable workflow?
Click to reveal answer
Answer
Shared pipeline logic called from multiple workflows to avoid duplication
Question
Why use shallow clone?
Click to reveal answer
Answer
Speeds up checkout by downloading only latest commit
Question
What does post: always do in Jenkins?
Click to reveal answer
Answer
Runs steps regardless of build success or failure
Question
What is pipeline as code?
Click to reveal answer
Answer
Defining CI/CD pipelines in version-controlled YAML/Groovy files
Revision Notes
Key Takeaways
- 1. GitHub Actions, GitLab CI, Jenkins are the three main CI/CD tools for Magento builds
- 2. Use caching, artifacts, matrix builds, and parallel jobs for optimization
- 3. Pipeline stages: build, test, package, deploy
Interview Tips
- • Compare GitHub Actions vs GitLab CI vs Jenkins
- • Explain artifact management and caching strategies
- • Discuss pipeline optimization techniques
- • Describe conditional execution and matrix builds
Cheat Sheet
Build Pipelines
Stages:
Build -> Test -> Package -> Deploy
GitHub Actions:
- actions/cache for dependencies
- actions/upload-artifact for outputs
- matrix for parallel builds
GitLab CI:
- stages for ordering
- services for dependencies
- artifacts for passing outputs
Jenkins:
- Declarative pipeline
- parallel stages
- post actions
Optimization:
- Cache vendor/ and composer
- Shallow clone
- Parallel execution
- Matrix builds