Skip to content

Merge pull request #1 from SX110903/secure-app-frontend #6

Merge pull request #1 from SX110903/secure-app-frontend

Merge pull request #1 from SX110903/secure-app-frontend #6

Workflow file for this run

name: CI/CD Pipeline
on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
- develop
env:
DOCKER_REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# Job 1: Tests y Validación
test:
name: Tests y Validación
runs-on: ubuntu-latest
steps:
- name: Checkout código
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: pdo, pdo_mysql, opcache
- name: Validar sintaxis PHP
run: |
find src -name "*.php" -exec php -l {} \; | grep -v "No syntax errors"
- name: Verificar configuración
run: |
if [ ! -f "config.php" ]; then
echo "Error: config.php no encontrado"
exit 1
fi
# Job 2: Build Docker Image
build:
name: Build Docker Image
runs-on: ubuntu-latest
needs: test
steps:
- name: Checkout código
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
- name: Build y Push Docker Image
uses: docker/build-push-action@v4
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
target: production
# Job 3: Deploy a Staging (solo branch develop)
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/develop' && github.event_name == 'push'
environment:
name: staging
url: https://staging.yourdomain.com
steps:
- name: Checkout código
uses: actions/checkout@v3
- name: Deploy vía SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.STAGING_HOST }}
username: ${{ secrets.STAGING_USER }}
key: ${{ secrets.STAGING_SSH_KEY }}
script: |
cd /var/www/secure-app-staging
git pull origin develop
docker-compose down
docker-compose pull
docker-compose up -d
docker-compose exec -T app php --version
# Job 4: Deploy a Production (solo branch main)
deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment:
name: production
url: https://yourdomain.com
steps:
- name: Checkout código
uses: actions/checkout@v3
- name: Deploy vía SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PRODUCTION_HOST }}
username: ${{ secrets.PRODUCTION_USER }}
key: ${{ secrets.PRODUCTION_SSH_KEY }}
script: |
cd /var/www/secure-app
git pull origin main
docker-compose down
docker-compose --profile production pull
docker-compose --profile production up -d
docker-compose exec -T app php --version
- name: Health Check
run: |
sleep 30
curl -f https://yourdomain.com/api.php?path=health || exit 1
# Job 5: Security Scan
security-scan:
name: Security Scan
runs-on: ubuntu-latest
needs: test
steps:
- name: Checkout código
uses: actions/checkout@v3
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy results to GitHub Security
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'