Skip to content

Commit aed9d19

Browse files
committed
Add CI/CD
1 parent 36aae13 commit aed9d19

File tree

3 files changed

+184
-1
lines changed

3 files changed

+184
-1
lines changed

.github/workflows/branch.yml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: Symfony deployment
2+
3+
on:
4+
push:
5+
# branches: [ "alpha" ]
6+
branches: [ "feature/upgrade_symfony" ]
7+
pull_request:
8+
# branches: [ "alpha" ]
9+
branches: [ "feature/upgrade_symfony" ]
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
validate:
16+
name: Symfony (PHP ${{ matrix.php-versions }})
17+
runs-on: ubuntu-latest
18+
19+
# Docs: https://docs.github.com/en/actions/using-containerized-services
20+
services:
21+
mysql:
22+
image: mysql:9.3
23+
env:
24+
MYSQL_ALLOW_EMPTY_PASSWORD: false
25+
MYSQL_ROOT_PASSWORD: symfony
26+
MYSQL_DATABASE: symfony
27+
ports:
28+
- 3306/tcp
29+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
php-versions: ['8.4']
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v4
37+
38+
# Docs: https://github.com/shivammathur/setup-php
39+
- name: Setup PHP
40+
uses: shivammathur/setup-php@v2
41+
with:
42+
php-version: ${{ matrix.php-versions }}
43+
tools: phpunit-bridge
44+
extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite, mysql
45+
coverage: xdebug
46+
47+
# Local MySQL service in GitHub hosted environments is disabled by default.
48+
# If you are using it instead of service containers, make sure you start it.
49+
# - name: Start mysql service
50+
# run: sudo systemctl start mysql.service
51+
52+
- name: Copy .env.dist
53+
run: php -r "file_exists('.env.local') || copy('.env.dist', '.env.local');"
54+
- name: Get composer cache directory
55+
id: composer-cache
56+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
57+
- name: Cache composer dependencies
58+
uses: actions/cache@v4
59+
with:
60+
path: ${{ steps.composer-cache.outputs.dir }}
61+
# Use composer.json for key, if composer.lock is not committed.
62+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
63+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
64+
restore-keys: ${{ runner.os }}-composer-
65+
- name: Install Composer dependencies
66+
if: steps.composer-cache.outputs.cache-hit != 'true'
67+
run: composer install --no-progress --prefer-dist --optimize-autoloader
68+
- name: Run Migration
69+
run: |
70+
composer require --dev symfony/orm-pack
71+
php bin/console doctrine:schema:update --complete --force || echo "No migrations found or schema update failed"
72+
php bin/console doctrine:migrations:migrate || echo "No migrations found or migration failed"
73+
env:
74+
DATABASE_URL: mysql://root:symfony@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/symfony
75+
- name: Install PHPUnit
76+
run: composer require --dev phpunit/phpunit && vendor/bin/phpunit --coverage-text
77+
deploy:
78+
name: Deploy image
79+
runs-on: ubuntu-latest
80+
needs: validate
81+
steps:
82+
- uses: actions/checkout@v4
83+
- name: Login to registry
84+
uses: docker/login-action@v3
85+
with:
86+
registry: ${{ secrets.DOCKER_REGISTRY }}
87+
username: ${{ secrets.DOCKER_LOGIN }}
88+
password: ${{ secrets.DOCKER_PASSWD }}
89+
- name: Build and push Docker image
90+
uses: docker/build-push-action@v5
91+
with:
92+
context: .
93+
push: true
94+
tags: ${{ secrets.DOCKER_REGISTRY }}/api-php:${{ github.ref_name }}
95+
- name: Configure SSH
96+
run: |
97+
mkdir -p ~/.ssh/
98+
echo "$SSH_KEY" > ~/.ssh/staging.key
99+
chmod 600 ~/.ssh/staging.key
100+
cat >>~/.ssh/config <<END
101+
Host staging
102+
HostName $SSH_HOST
103+
User $SSH_USER
104+
Port $SSH_PORT
105+
IdentityFile ~/.ssh/staging.key
106+
StrictHostKeyChecking no
107+
END
108+
env:
109+
SSH_USER: ${{ secrets.SSH_USER }}
110+
SSH_KEY: ${{ secrets.SSH_KEY }}
111+
SSH_HOST: ${{ secrets.SSH_HOST }}
112+
SSH_PORT: ${{ secrets.SSH_PORT }}
113+
DOMAIN_URL: ${{ secrets.DOMAIN_URL }}
114+
- name: Update alpha environment & run migrations
115+
run: |
116+
if [[ "$DOCKER_BRANCH" == "master" ]]; then
117+
export HOSTNAME="api.$DOMAIN_URL"
118+
elif [[ "$DOCKER_BRANCH" == "preprod" ]]; then
119+
export HOSTNAME="api.$DOCKER_BRANCH.$DOMAIN_URL"
120+
else
121+
export HOSTNAME="api.$DOCKER_BRANCH.$DOMAIN_URL"
122+
fi
123+
ssh staging 'export DOCKER_BRANCH=${{ github.ref_name }} DOCKER_REGISTRY=${{ secrets.DOCKER_REGISTRY }}
124+
cd /var/docker/api/${DOCKER_BRANCH}
125+
git pull origin ${{ github.ref_name }}
126+
docker compose --file docker-compose-branch.yml pull
127+
docker login -u ${{ secrets.DOCKER_LOGIN }} -p ${{ secrets.DOCKER_PASSWD }} ${{ secrets.DOCKER_REGISTRY }}
128+
docker compose --project-name ${{ github.ref_name }}-api_php --file docker-compose-branch.yml up -d
129+
docker compose --file docker-compose-branch.yml exec symfony php bin/console doctrine:migrations:migrate --no-interaction'
130+
# Ajout de l'exécution des migrations Doctrine sur le VPS

docker-compose-branch.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
services:
2+
symfony:
3+
image: ${DOCKER_REGISTRY}/api-php:${DOCKER_BRANCH}
4+
container_name: ${DOCKER_BRANCH}-api_php-sf
5+
build:
6+
context: ./
7+
networks:
8+
- api-${DOCKER_BRANCH}
9+
- traefik-realt
10+
# volumes:
11+
# - ./logs/symfony:/var/www/html/var/log:cached
12+
# - ./logs/nginx:/var/log/nginx:cached
13+
labels:
14+
- "traefik.enable=true"
15+
- "traefik.http.routers.api-${DOCKER_BRANCH}.rule=Host(`${HOSTNAME}`)"
16+
- "traefik.http.services.api-${DOCKER_BRANCH}.loadbalancer.server.port=80"
17+
restart: always
18+
19+
db:
20+
image: mysql:9.3
21+
container_name: ${DOCKER_BRANCH}-api_php-db
22+
networks:
23+
- api-${DOCKER_BRANCH}
24+
environment:
25+
- "MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}"
26+
- "MYSQL_USER=${MYSQL_USER}"
27+
- "MYSQL_PASSWORD=${MYSQL_PASSWORD}"
28+
- "MYSQL_DATABASE=${MYSQL_DATABASE}"
29+
restart: always
30+
31+
redis:
32+
image: redis:7-alpine
33+
container_name: api-redis
34+
# ports:
35+
# - "6379:6379"
36+
networks:
37+
- api-php
38+
restart: unless-stopped
39+
command: ["redis-server", "--appendonly", "yes"]
40+
41+
adminer-branch:
42+
image: adminer
43+
container_name: ${DOCKER_BRANCH}-api_php-adminer
44+
ports:
45+
- "18080:8080"
46+
networks:
47+
- api-${DOCKER_BRANCH}
48+
restart: always
49+
50+
networks:
51+
api-${DOCKER_BRANCH}:
52+
traefik-realt:
53+
external: true
54+

docker-compose.local.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ services:
3838
restart: unless-stopped
3939
command: ["redis-server", "--appendonly", "yes"]
4040

41-
4241
adminer:
4342
image: adminer
4443
platform: linux/amd64

0 commit comments

Comments
 (0)