|
| 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 |
0 commit comments